import { constructContinueTx, constructContractRef, constructTake, Context, passCwebFrom } from '@coinweb/contract-kit';
import { getCallParameters, getContractIssuer, getMethodArguments } from 'lib/onchain';

import { constructMutexBlockUnlockClaimStore, constructMutexLockClaimKey } from '../claims';
import { lockFee } from '../settings';
import { MutexUnlockArgs } from '../types';

import { lockMethodName } from './names';

export const mutexUnlock = (context: Context) => {
  const { availableCweb } = getCallParameters(context);
  const issuer = getContractIssuer(context);

  const [lockId, timestamp] = getMethodArguments<MutexUnlockArgs>(context);

  return [
    constructContinueTx(
      context,
      [
        passCwebFrom(issuer, availableCweb),
        constructTake(constructMutexLockClaimKey(lockId, timestamp)),
        constructMutexBlockUnlockClaimStore(lockId),
      ],
      [
        {
          callInfo: {
            ref: constructContractRef(issuer, []),
            methodInfo: {
              methodName: lockMethodName,
              methodArgs: [],
            },
            contractInfo: {
              providedCweb: lockFee,
              authenticated: null,
            },
            contractArgs: [],
          },
        },
      ]
    ),
  ];
};
