import {
  WidgetTextRequest as ApiWidgetTextRequest,
  WidgetType,
} from '@spiffy-ai/commerce-api-client';
import { WidgetTypeV3 } from 'src/contexts/typesV3';
import { WidgetTextRequest as CoreWidgetTextRequest } from '../models/api/widgetTextRequest';
import { coreContextToApiContext } from './coreContextToApiContext';

export const coreWidgetTypeToApiWidgetType = (coreWidgetType: WidgetTypeV3): WidgetType => {
  switch (coreWidgetType) {
    case WidgetTypeV3.ChatPreviewV3:
      return WidgetType.ChatPreviewV3;
    case WidgetTypeV3.SocialProofV3:
      return WidgetType.SocialProofV3;
    case WidgetTypeV3.TypingAnimationV3:
      return WidgetType.TypingAnimationV3;
    case WidgetTypeV3.PromptCarouselV3:
      return WidgetType.PromptCarouselV3;
    case WidgetTypeV3.TitledPromptCarouselV3:
      return WidgetType.TitledPromptCarouselV3;
    case WidgetTypeV3.ImagePromptCardV3:
    case WidgetTypeV3.ProductCardV3:
      return WidgetType.ImagePromptCardV3;
    case WidgetTypeV3.ImageBannerV3:
      return WidgetType.ImageBannerV3;
    case WidgetTypeV3.SingleImagePromptV3:
      return WidgetType.SingleImagePromptV3;
    case WidgetTypeV3.FloatingChatV3:
      return WidgetType.FloatingChatV3;
    case WidgetTypeV3.FloatingButtonV3:
      return WidgetType.FloatingButtonV3;
    case WidgetTypeV3.PromptButtonCarouselWithImageV3:
      return WidgetType.SingleImagePromptV3;
    case WidgetTypeV3.FullPageSalesAgentV3:
      return WidgetType.FullPageSalesAgentV3;
    default:
      throw new Error(`Unsupported widget type: ${coreWidgetType}`);
  }
};

export const coreWidgetTextRequestToApiRequest = (
  coreWidgetTextRequest: CoreWidgetTextRequest,
): ApiWidgetTextRequest => ({
  id: coreWidgetTextRequest.requestId,
  context: coreContextToApiContext(coreWidgetTextRequest.context),
  feature_flags: coreWidgetTextRequest.featureFlags,
  language: coreWidgetTextRequest.language,
  url: coreWidgetTextRequest.url,
  org_base_config_version: coreWidgetTextRequest.overrideConfigVersion,
  widget_type: coreWidgetTypeToApiWidgetType(coreWidgetTextRequest.widgetType),
});
