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

import { ResolvedOp } from '../../types';
import { context } from '../context';

export const resultKey = '_result';

export const constructResultClaimKey = (id: string) => constructClaimKey([resultKey], [id]);

export const constructResultClaim = (id: string, result: ResolvedOp[]) =>
  constructClaim(constructResultClaimKey(id), result, '0x0');

export const constructResultClaimStore = (id: string, result: ResolvedOp[]): GStore<CwebStore> =>
  constructStore(constructResultClaim(id, result));

export const constructResultClaimRead = (id: string) => constructRead(context.issuer, constructResultClaimKey(id));

export const constructResultClaimTake = (id: string) => constructTake(constructResultClaimKey(id));

export const constructResultBlockFilter = (id: string): BlockFilter => {
  const { first_part: first, second_part: second } = constructResultClaimKey(id);

  return {
    issuer: context.issuer,
    first,
    second,
  };
};
