import { ClaimKey, CwebStore, GStore, GTake, PreparedOperation, ResolvedOperation } from '@coinweb/contract-kit';
import { CwebTake } from '@coinweb/contract-kit/dist/types/operations/take';

export type LockedKey = Omit<ClaimKey, 'second_part'> & Partial<Pick<ClaimKey, 'second_part'>>;

export type MutexLockState = {
  locked: boolean;
  keys: LockedKey[];
  processId: string;
};

export type MutexNotifyLockState = {
  timestamp: number;
  lockId: string;
};

export type MutexExecOpsResult = (
  | {
      ok: true;
      resolved: ResolvedOperation;
    }
  | {
      ok: false;
      error: string;
    }
)[];

export type MutexUnlockArgs = [lockId: string, timestamp: number, notify?: boolean];

export type MutexExecOpArgs = [ops: (GTake<CwebTake> | GStore<CwebStore>)[], processId: string, execId?: string];

export type MutexWaitNotifyArgs = [step: number, lockInfo: MutexNotifyLockState];

export type MutexNotifyLockArgs = [lockInfo: MutexNotifyLockState];

export type MutexPreReadTakeOpsArgs = [execId: string, unavailableIndexes: number[], ops: PreparedOperation[]];

export type MutexSaveExecOpResultArgs = [execId: string, unavailableIndexes: number[]];
