import { ContractCallMetadataInterface, TrackingMetadataInterface } from '../../shared';
import { forceHistoryId } from '../features/forceHistoryId';

import { getMetadata } from './metadata';

export const createRawMetadata = (): ContractCallMetadataInterface | null => {
  const metadata = getMetadata();
  if (metadata?.trackingId) {
    const rawMetadataWithTrackingId: TrackingMetadataInterface = {
      _trackingId: metadata.trackingId,
    };

    if (metadata.historyOrder) {
      rawMetadataWithTrackingId._historyOrder = metadata.historyOrder;
    }

    if (metadata.historyLink) {
      rawMetadataWithTrackingId._historyLink = metadata.historyLink;
    }

    if (metadata.accessId) {
      rawMetadataWithTrackingId._currentAccessId = metadata.accessId;
    }

    return {
      _metadata: rawMetadataWithTrackingId,
    };
  }

  if (metadata?.accessId) {
    return {
      _metadata: {
        _trackingId: forceHistoryId(),
        _currentAccessId: metadata.accessId,
      },
    };
  }

  return null;
};
