import {
  BlockFilter,
  constructBlock,
  constructClaim,
  constructClaimKey,
  constructStore,
  constructTake,
  ContractIssuer,
} from '@coinweb/contract-kit';

export const mutexBlockLockKey = '_mutex_block_lock';

export const constructMutexBlockLockClaimKey = (lockId: string) => constructClaimKey([mutexBlockLockKey], [lockId]);

export const constructMutexBlockLockClaim = ({ lockId }: { lockId: string }) =>
  constructClaim(constructMutexBlockLockClaimKey(lockId), {}, '0x0');

export const constructMutexBlockLockClaimStore = ({ lockId }: { lockId: string }) =>
  constructStore(constructMutexBlockLockClaim({ lockId }));

export const constructMutexBlockLockClaimTake = (lockId: string) =>
  constructTake(constructMutexBlockLockClaimKey(lockId));

export const constructMutexLockBlockFilter = (lockId: string, issuer: ContractIssuer): BlockFilter => {
  const { first_part: first, second_part: second } = constructMutexBlockLockClaimKey(lockId);

  return {
    issuer,
    first,
    second,
  };
};

export const constructMutexLockBlock = (lockId: string, issuer: ContractIssuer) => {
  return constructBlock([constructMutexLockBlockFilter(lockId, issuer)]);
};
