import { PageVisitCategory } from '@spiffy-ai/commerce-api-client';
import { PDPVariantInfo, PLPVariantInfo, PageVisitVariantInfo, VariantInfo } from '../variantInfo';

const isPDPVariantInfo = (variantInfo: object): variantInfo is PDPVariantInfo =>
  'variant' in variantInfo &&
  variantInfo.variant === 'pdp' &&
  'productId' in variantInfo &&
  typeof variantInfo.productId === 'string';

const isPLPVariantInfo = (variantInfo: object): variantInfo is PLPVariantInfo =>
  'variant' in variantInfo &&
  variantInfo.variant === 'plp' &&
  'plpId' in variantInfo &&
  typeof variantInfo.plpId === 'string';

const isPageVisitVariantInfo = (variantInfo: object): variantInfo is PageVisitVariantInfo =>
  'variant' in variantInfo &&
  variantInfo.variant === 'page_visit' &&
  'url' in variantInfo &&
  typeof variantInfo.url === 'string' &&
  'pageVisitCategory' in variantInfo &&
  Object.values(PageVisitCategory).includes(variantInfo.pageVisitCategory as PageVisitCategory);

/**
 * Type guard to check if an object is a valid VariantInfo.
 *
 * @param variantInfo - The unknown object to check.
 *
 * @returns True if the data is a valid VariantInfo object, false otherwise.
 */
export const isVariantInfo = (variantInfo: unknown): variantInfo is VariantInfo =>
  variantInfo != null &&
  typeof variantInfo === 'object' &&
  'variantId' in variantInfo &&
  (isPDPVariantInfo(variantInfo) ||
    isPLPVariantInfo(variantInfo) ||
    isPageVisitVariantInfo(variantInfo));
