import { TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API } from '@/modules/feeds//constants';
import {
  AUTO_CHAPTERS_SUPPORTED_LANGUAGES,
  DIALOG_METADATA,
  ERROR_AUTO_CHAPTERS,
  ERROR_CHAT_GPT_DESCRIPTION,
  ERROR_HIGHLIGHTS_MODEL_NOT_SUPPORTED,
  ERROR_MULTI_LANGUAGE_SUBTITLES,
  ERROR_SPEECH_TO_TEXT_LANGUAGE_NOT_SUPPORTED,
  ERROR_TOPICS,
  HIGHLIGHTS_SUPPORTED_LANGUAGES,
  TOPICS_SUPPORTED_LANGUAGES,
} from '@/modules/feeds/Editor/components/Dialogs/ConfigurationErrorDialog/constants';
import { SPEECH_TO_TEXT_ASSEMBLY_AI_VALUE, SPEECH_TO_TEXT_DISABLED_VALUE } from '@/modules/feeds/Editor/constants';

import { PlatformModel } from '@/modules/feeds/Editor/types';

import {
  langSelector,
  metadataSpeechToTextLanguagesSelector,
  platformModelsSelector,
  speechToTextProviderSelector,
  typeSelector,
} from '@/modules/feeds/Editor/redux/selectors';

import { StoreType } from '@/modules/@common/store/store';

type PropType = {
  inputLanguage?: string | undefined;
  inputSpeechToTextProvider?: string | undefined;
  inputModels?: PlatformModel[] | undefined;
};

type CodeType = keyof typeof DIALOG_METADATA;

export type ConfigurationErrorOutputType = {
  code: CodeType;
  lang: string;
  speechProvider: string;
} | null;

export default function setupGetConfigurationErrorCode(store: StoreType) {
  return ({ inputLanguage, inputSpeechToTextProvider, inputModels }: PropType): ConfigurationErrorOutputType => {
    const state$ = store.getState();

    // type
    const type = typeSelector(state$);
    if ([TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API].includes(type)) {
      return null;
    }
    // options
    const speechToTextLanguages = metadataSpeechToTextLanguagesSelector(state$) || [];

    // value
    const lang = inputLanguage || langSelector(state$);
    const speechProvider = inputSpeechToTextProvider || speechToTextProviderSelector(state$);
    const models = inputModels || platformModelsSelector(state$);

    // error handle logic
    const makeError = (code: CodeType): { code: CodeType; lang: string; speechProvider: string } => ({
      code,
      lang,
      speechProvider,
    });
    const checkModel = (modelName: string) => models.find((m) => m.model === modelName && m.enabled);
    const errors: { code: CodeType; validate: () => boolean }[] = [
      {
        code: ERROR_SPEECH_TO_TEXT_LANGUAGE_NOT_SUPPORTED,
        validate: () => {
          const isSupportedBySpeechToTextProvider = speechToTextLanguages
            .find((speech) => speech.value === speechProvider)
            ?.languages.includes(lang);
          return speechProvider !== SPEECH_TO_TEXT_DISABLED_VALUE && !isSupportedBySpeechToTextProvider;
        },
      },
      {
        code: ERROR_HIGHLIGHTS_MODEL_NOT_SUPPORTED,
        validate: () => {
          const isHighlightsModel = checkModel('HIGHLIGHTS');
          return !!(
            isHighlightsModel &&
            (!HIGHLIGHTS_SUPPORTED_LANGUAGES.includes(lang) || speechProvider === SPEECH_TO_TEXT_DISABLED_VALUE)
          );
        },
      },
      {
        code: ERROR_MULTI_LANGUAGE_SUBTITLES,
        validate: () => {
          const isMultiLanguageSubtitlesModel = checkModel('MULTI_LANGUAGE_SUBTITLES');
          return !!(isMultiLanguageSubtitlesModel && speechProvider === SPEECH_TO_TEXT_DISABLED_VALUE);
        },
      },
      {
        code: ERROR_CHAT_GPT_DESCRIPTION,
        validate: () => {
          const isChatGptDescriptionModel = checkModel('CHAT_GPT_DESCRIPTION');
          return !!(isChatGptDescriptionModel && speechProvider === SPEECH_TO_TEXT_DISABLED_VALUE);
        },
      },
      {
        code: ERROR_AUTO_CHAPTERS,
        validate: () => {
          const isAutoChapterModel = checkModel('AUTO_CHAPTERS');
          return !!(
            isAutoChapterModel &&
            (!AUTO_CHAPTERS_SUPPORTED_LANGUAGES.includes(lang) || speechProvider !== SPEECH_TO_TEXT_ASSEMBLY_AI_VALUE)
          );
        },
      },
      {
        code: ERROR_TOPICS,
        validate: () => {
          const isAutoChapterModel = checkModel('TOPICS');
          return !!(
            isAutoChapterModel &&
            (!TOPICS_SUPPORTED_LANGUAGES.includes(lang) || speechProvider !== SPEECH_TO_TEXT_ASSEMBLY_AI_VALUE)
          );
        },
      },
    ];

    const error = errors.find((r) => r.validate());
    if (error) return makeError(error.code);

    return null;
  };
}
