import { ofType } from 'redux-observable';
import { concat, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';

import { getDefaultTempForm } from '../../helpers';
import { getConfigurationDataAction, setConfigurationDataAction } from '../slices';
import { gqlRequest } from '@/modules/@common/request';

const queryGQL = `
  query getModelsConfig {
    getModelsConfig {
       data {
          provider
          models {
            name
            value
          }
       }
    }  
    getDictionaryConfig {
       awsRecognitionModels {
          model
          category
          lowerBound
          maxResults
       }
       clarifaiModels {
          model
          category
          lowerBound
          maxResults
       }
       googleModels {
          model
          category
          lowerBound
          maxResults
       }
       weavoModels {
          model
          category
          lowerBound
          maxResults
       }
       dictionaryBrandSafetyCategories
       dictionaryIabClassificationCategories
       notifications {
          type
          state
          email {
            title
            message
            replyTo {
              address
              name
            }
          }
       }
       dictionaryBrandSafetySpeech {
          grouped {
            label
            value {
              bucket
              key
              url
            }
          }
       }
       dictionaryBrandSafetyText {
          grouped {
            label
            value {
              bucket
              key
              url
            }
          }
       }
       dictionaryBlacklistFile {
            bucket
            key
            url
       }
       dictionaryMapFile {
            bucket
            key
            url
       }
       dictionaryCategorizationFile {
            bucket
            key
            url
       }
       dictionaryFilterFile {
            bucket
            key
            url
       }
       dictionaryBrandSafetyFile {
            bucket
            key
            url
       }
       htmlParsingConfigFile {
            bucket
            key
            url
       }
       classificationIabWeightsFile {
            bucket
            key
            url
       }
       classificationIabCelebritiesMapFile {
          bucket
          key
          url
       }
       classificationIabMapFile {
          bucket
          key
          url
       }
       classificationKeywordsLowerbound
       classificationKeywordsCount
       classificationIabCount
       classificationIabLowerbound
       enableIabKeywordsMergingAlgorithm
       enableIabCategorizationAlgorithm
    }
  }
`;

export default (action$) =>
  action$.pipe(
    ofType(getConfigurationDataAction.type),
    switchMap(() => {
      const stream$ = gqlRequest({
        query: queryGQL,
      }).pipe(
        switchMap(({ data, errors }) => {
          const actions = [];

          if (!errors.length) {
            const providers = data.getModelsConfig?.data ?? [];

            const {
              awsRecognitionModels,
              clarifaiModels,
              googleModels,
              weavoModels,
              dictionaryBrandSafetyText,
              dictionaryBrandSafetySpeech,
            } = data.getDictionaryConfig;

            actions.push(
              of(
                setConfigurationDataAction({
                  ...data.getDictionaryConfig,
                  ...{
                    awsRecognitionModels: {
                      providerName: 'AMAZON',
                      formList: awsRecognitionModels.map((item) => ({
                        ...item,
                        formId: `${Math.random()}-${Math.random()}`,
                      })),
                      tempForm: getDefaultTempForm(providers, 'AMAZON'),
                    },
                    clarifaiModels: {
                      providerName: 'CLARIFAI',
                      formList: clarifaiModels.map((item) => ({
                        ...item,
                        formId: `${Math.random()}-${Math.random()}`,
                      })),
                      tempForm: getDefaultTempForm(providers, 'CLARIFAI'),
                    },
                    googleModels: {
                      providerName: 'GOOGLE',
                      formList: googleModels.map((item) => ({
                        ...item,
                        formId: `${Math.random()}-${Math.random()}`,
                      })),
                      tempForm: getDefaultTempForm(providers, 'GOOGLE'),
                    },
                    weavoModels: {
                      providerName: 'WEAVO',
                      formList: weavoModels.map((item) => ({
                        ...item,
                        formId: `${Math.random()}-${Math.random()}`,
                      })),
                      tempForm: getDefaultTempForm(providers, 'WEAVO'),
                    },
                    dictionaryBrandSafetyText: dictionaryBrandSafetyText.grouped,
                    dictionaryBrandSafetySpeech: dictionaryBrandSafetySpeech.grouped,
                  },
                  providers,
                }),
              ),
            );
          }

          return concat(...actions);
        }),
      );

      return concat(stream$);
    }),
  );
