import { constructStore } from '@coinweb/contract-kit';

import { getTime } from '../../../../onchain';
import { createHistoryAccessClaim } from '../claims';
import { getMetadata } from '../metadata/metadata';

import { forceHistoryId } from './forceHistoryId';

export const constructCurrentHistoryAccess = (options?: { historyId?: string }) => {
  const { historyId: providedHistoryId } = options || {};

  const timestamp = getTime();

  const id = getMetadata()?.accessId;

  if (!id) {
    return [];
  }

  const historyId = providedHistoryId ?? forceHistoryId();

  return [
    constructStore(
      createHistoryAccessClaim({
        id,
        timestamp,
        historyId,
      })
    ),
  ];
};
