import {
  constructClaim,
  constructClaimKey,
  constructContinueTx,
  constructContractIssuer,
  constructContractRef,
  constructStore,
  Context,
  extractContractInfo,
  getContractId,
} from '@coinweb/contract-kit';

export const mutexLock = (context: Context) => {
  const { providedCweb } = extractContractInfo(context.tx);
  const issuer = constructContractIssuer(getContractId(context.tx));

  return [
    constructContinueTx(
      context,
      [
        constructStore(
          constructClaim(constructClaimKey('mutex', context.call.txid.slice(0, 8)), { word: 'CLASH' }, '0x0')
        ),
      ],
      [
        {
          callInfo: {
            ref: constructContractRef(issuer, []),
            methodInfo: {
              methodName: '_unlock',
              methodArgs: [],
            },
            contractInfo: {
              providedCweb: providedCweb ? providedCweb - 1000n : 1000n,
              authenticated: null,
            },
            contractArgs: [],
          },
        },
      ]
    ),
  ];
};
