import {
  ContractCall,
  constructContractIssuer,
  constructSelfRegisterKey,
  constructSingleReadClaim,
  prepareQueueContractCall,
} from '@coinweb/contract-kit';

import { toHex } from '../../../shared';

export const constructCall = ({
  contractId,
  methodName,
  methodArgs,
  cost,
  auth = true,
  withQueue = true,
}: {
  contractId: string;
  methodName: string;
  methodArgs: unknown[];
  cost: bigint;
  auth?: boolean;
  withQueue?: boolean;
}): ContractCall => {
  const issuer = constructContractIssuer(contractId);

  if (!withQueue) {
    const contractCall: ContractCall = {
      contract_input: {
        data: [methodName, ...methodArgs],
        cost: toHex(cost),
        authenticated: auth,
      },
      contract_ref: {
        explicit: [],
        stored: [constructSingleReadClaim(constructContractIssuer(contractId), constructSelfRegisterKey())],
      },
    };

    return contractCall;
  }

  return prepareQueueContractCall(issuer, { methodName, methodArgs }, cost, auth);
};
