import {
  constructClaim,
  constructClaimKey,
  constructRangeRead,
  constructStore,
  constructTake,
  CwebStore,
  GRead,
  GStore,
} from '@coinweb/contract-kit';
import { CwebRead } from '@coinweb/contract-kit/dist/esm/operations/read';
import { toHex } from 'lib/shared';

import { context } from '../context';
import { uuid } from '../utils';

export const fundsKey = '_funds';

export const constructFundsClaimKeyFirstPart = (processId: string) => [fundsKey, processId];

export const constructFundsClaimKey = (processId: string, claimId?: string) =>
  constructClaimKey(constructFundsClaimKeyFirstPart(processId), [claimId ?? uuid()]);

export const constructFundsClaim = (processId: string, amount: bigint) =>
  constructClaim(constructFundsClaimKey(processId), {}, toHex(amount));

export const constructFundsClaimStore = (processId: string, amount: bigint): GStore<CwebStore> =>
  constructStore(constructFundsClaim(processId, amount));

export const constructFundsClaimRangRead = (processId: string): GRead<CwebRead> =>
  constructRangeRead(context.issuer, constructFundsClaimKeyFirstPart(processId), {}, 10000);

export const constructFundsClaimTakeOp = (processId: string, id: string) =>
  constructTake(constructFundsClaimKey(processId, id));
