import {
  WidgetString as ApiWidgetString,
  WidgetTextRequest as ApiWidgetTextRequest,
  WidgetTextResponse as ApiWidgetTextResponse,
} from '@spiffy-ai/commerce-api-client';

import {
  WidgetString as CoreWidgetString,
  WidgetText as CoreWidgetText,
} from '../models/api/widgetText';

export const apiWidgetStringToCoreWidgetString = (
  apiWidgetString: ApiWidgetString,
): CoreWidgetString => ({
  id: apiWidgetString.string_id,
  value: apiWidgetString.text,
});

export const apiWidgetTextResponseToCoreWidgetText = (
  apiRequest: ApiWidgetTextRequest,
  apiWidgetTextResponse: ApiWidgetTextResponse,
): CoreWidgetText => ({
  requestId: apiWidgetTextResponse.request_id ?? 'unknown',
  responseId: apiWidgetTextResponse.id ?? 'unknown',
  language: apiRequest.language,
  values: Object.fromEntries(
    Object.entries(apiWidgetTextResponse.widget_text).map(([key, value]) => [
      key,
      value.map(apiWidgetStringToCoreWidgetString),
    ]),
  ),
});
