import {
  BlockFilter,
  constructClaim,
  constructClaimKey,
  constructStore,
  CwebStore,
  GRead,
  GStore,
} from '@coinweb/contract-kit';
import { constructRead, CwebRead } from '@coinweb/contract-kit/dist/esm/operations/read';

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

export const constructResultClaimKey = (id: string) => constructClaimKey(['result'], [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): GRead<CwebRead> =>
  constructRead(context.issuer, constructResultClaimKey(id));

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

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