import { crossLanguageOptions } from '@/components/cross-language-form-field';
import { LayoutRecognizeFormField } from '@/components/layout-recognize-form-field';
import {
  SelectWithSearch,
  SelectWithSearchFlagOptionType,
} from '@/components/originui/select-with-search';
import { RAGFlowFormItem } from '@/components/ragflow-form';
import { Switch } from '@/components/ui/switch';
import { upperCase, upperFirst } from 'lodash';
import { useTranslation } from 'react-i18next';
import {
  FileType,
  OutputFormatMap,
  SpreadsheetOutputFormat,
} from '../../constant/pipeline';
import { CommonProps } from './interface';
import { buildFieldNameWithPrefix } from './utils';

const UppercaseFields = [
  SpreadsheetOutputFormat.Html,
  SpreadsheetOutputFormat.Json,
];

function buildOutputOptionsFormatMap() {
  return Object.entries(OutputFormatMap).reduce<
    Record<string, SelectWithSearchFlagOptionType[]>
  >((pre, [key, value]) => {
    pre[key] = Object.values(value).map((v) => ({
      label: UppercaseFields.some((x) => x === v)
        ? upperCase(v)
        : upperFirst(v),
      value: v,
    }));
    return pre;
  }, {});
}

export type OutputFormatFormFieldProps = CommonProps & {
  fileType: FileType;
};

export function OutputFormatFormField({
  prefix,
  fileType,
}: OutputFormatFormFieldProps) {
  const { t } = useTranslation();
  return (
    <RAGFlowFormItem
      name={buildFieldNameWithPrefix(`output_format`, prefix)}
      label={t('flow.outputFormat')}
    >
      <SelectWithSearch
        options={buildOutputOptionsFormatMap()[fileType]}
      ></SelectWithSearch>
    </RAGFlowFormItem>
  );
}

export function ParserMethodFormField({
  prefix,
  optionsWithoutLLM,
}: CommonProps & { optionsWithoutLLM?: { value: string; label: string }[] }) {
  const { t } = useTranslation();
  return (
    <LayoutRecognizeFormField
      name={buildFieldNameWithPrefix(`parse_method`, prefix)}
      horizontal={false}
      optionsWithoutLLM={optionsWithoutLLM}
      label={t('flow.parserMethod')}
    ></LayoutRecognizeFormField>
  );
}

export function FlattenMediaToTextFormField({ prefix }: CommonProps) {
  const { t } = useTranslation();
  return (
    <RAGFlowFormItem
      name={buildFieldNameWithPrefix(`flatten_media_to_text`, prefix)}
      label={t('flow.flattenMediaToText')}
      tooltip={t('flow.flattenMediaToTextTip')}
      horizontal={true}
      labelClassName="w-full"
      valueClassName="w-8"
    >
      {(field) => (
        <Switch
          checked={field.value}
          onCheckedChange={(checked) => {
            field.onChange?.(checked);
          }}
        />
      )}
    </RAGFlowFormItem>
  );
}

export function TwoColumnCheckFormField({ prefix }: CommonProps) {
  const { t } = useTranslation();
  return (
    <RAGFlowFormItem
      name={buildFieldNameWithPrefix(`enable_multi_column`, prefix)}
      label={t('flow.enableMultiColumn')}
      horizontal={true}
      labelClassName="w-full"
      valueClassName="w-8"
      tooltip={t('flow.enableMultiColumnTip')}
    >
      {(field) => (
        <Switch
          checked={field.value}
          onCheckedChange={(checked) => {
            field.onChange?.(checked);
          }}
        />
      )}
    </RAGFlowFormItem>
  );
}

export function RmdirFormField({ prefix }: CommonProps) {
  const { t } = useTranslation();
  return (
    <RAGFlowFormItem
      name={buildFieldNameWithPrefix(`remove_toc`, prefix)}
      label={t('flow.removeToc')}
      horizontal={true}
      tooltip={t('flow.removeTocTip')}
      labelClassName="w-full"
      valueClassName="w-8"
    >
      {(field) => (
        <Switch
          checked={field.value}
          onCheckedChange={(checked) => {
            field.onChange?.(checked);
          }}
        />
      )}
    </RAGFlowFormItem>
  );
}

export function RemoveHeaderFooterFormField({ prefix }: CommonProps) {
  const { t } = useTranslation();
  return (
    <RAGFlowFormItem
      name={buildFieldNameWithPrefix(`remove_header_footer`, prefix)}
      label={t('flow.removeHeaderFooter')}
      horizontal={true}
      labelClassName="w-full"
      valueClassName="w-8"
    >
      {(field) => (
        <Switch
          checked={field.value}
          onCheckedChange={(checked) => {
            field.onChange?.(checked);
          }}
        />
      )}
    </RAGFlowFormItem>
  );
}

export function LanguageFormField({ prefix }: CommonProps) {
  const { t } = useTranslation();

  return (
    <RAGFlowFormItem
      name={buildFieldNameWithPrefix(`lang`, prefix)}
      label={t('flow.lang')}
    >
      {(field) => (
        <SelectWithSearch
          options={crossLanguageOptions}
          value={field.value}
          onChange={field.onChange}
        ></SelectWithSearch>
      )}
    </RAGFlowFormItem>
  );
}
