import { ContractCallMetadataInterface, HistoryLink, NewMetadataInterface, TrackingMetadataInterface } from '../types';

export const constructMetadata = (
  data:
    | {
        accessId: string;
        historyLink?: HistoryLink;
      }
    | {
        trackingId: string;
        currentAccessId?: string;
        historyLink?: HistoryLink;
        historyOrder?: number;
      }
): ContractCallMetadataInterface => {
  if ('accessId' in data) {
    const metadata: NewMetadataInterface = {
      _newAccessId: data.accessId,
    };

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

    return {
      _metadata: metadata,
    };
  }

  const metadata: TrackingMetadataInterface = {
    _trackingId: data.trackingId,
  };

  if (data.currentAccessId) {
    metadata._currentAccessId = data.currentAccessId;
  }

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

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

  return {
    _metadata: metadata,
  };
};
