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

import { MutexAccessResult, MutexAccessStatus } from '../types';

export const mutexAccessKey = 'mutex_access';

export const constructMutexAccessFirstPart = () => [mutexAccessKey];

export const constructMutexAccessClaimKey = (uniqueId: string) =>
  constructClaimKey(constructMutexAccessFirstPart(), [uniqueId]);

export const constructMutexAccessClaim = (uniqueId: string, status: MutexAccessStatus) =>
  constructClaim(constructMutexAccessClaimKey(uniqueId), { status } satisfies MutexAccessResult, '0x0');

export const constructMutexAccessClaimStore = (uniqueId: string, status: MutexAccessStatus): GStore<CwebStore> =>
  constructStore(constructMutexAccessClaim(uniqueId, status));

export const constructMutexAccessClaimTake = (uniqueId: string) =>
  constructTake(constructMutexAccessClaimKey(uniqueId));
