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

export const mutexBlockUnlockKey = '_mutex_block_unlock';

export const constructMutexBlockUnlockClaimKey = (uniqueId: string) =>
  constructClaimKey([mutexBlockUnlockKey], [uniqueId]);

export const constructMutexBlockUnlockClaim = (uniqueId: string) =>
  constructClaim(constructMutexBlockUnlockClaimKey(uniqueId), {}, '0x0');

export const constructMutexBlockUnlockClaimStore = (uniqueId: string): GStore<CwebStore> =>
  constructStore(constructMutexBlockUnlockClaim(uniqueId));

export const constructMutexBlockUnlockClaimTake = (uniqueId: string) =>
  constructTake(constructMutexBlockUnlockClaimKey(uniqueId));

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

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

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