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

import { AUTOMATIC_OPTIMIZATION } from '../../constants';

import getCurrentAndFuturePrice from '../../helpers/getCurrentAndFuturePrice';
import * as selectors from '../selectors';
import { initializeSupplyFormDataAction, setAutomaticOptimizationTabAction, setSupplyTabAction } from '../slices';

export default (action$, state$) =>
  action$.pipe(
    ofType(initializeSupplyFormDataAction.type),
    switchMap(() => {
      const setSupply = {};
      let setAutomaticOptimization = {};

      const state = state$.value;
      const info = selectors.infoSelector(state);
      const pageConfig = selectors.pageConfigSelector(state);
      const isDuplicate = pageConfig?.isDuplicate;

      if (info) {
        setSupply.name = isDuplicate ? `Copy_of_${info.name}` : info.name;
        setSupply.source = info?.source;
        setSupply.platformId = info?.platformId || '';
        setSupply.publisherDemandOnly = info.publisherDemand;
        setSupply.format = info.format;
        setSupply.displayType = info.displayType ?? null;

        if (info.pricing) {
          const pricing = info.pricing.map((price, index) => ({
            id: index,
            model: price.model,
            payment: price.value,
            startDate: price.startDate,
            endDate: price.endDate,
          }));
          setSupply.pricing = isDuplicate ? getCurrentAndFuturePrice(pricing) : pricing;
        }

        if (info.expenses) {
          const expenses =
            info.expenses?.map((price, index) => ({
              id: index,
              value: price.value,
              startDate: price.startDate,
              endDate: price.endDate,
              updatedBy: price.updatedBy,
            })) ?? [];
          setSupply.expenses = isDuplicate ? getCurrentAndFuturePrice(expenses) : expenses;
        }

        if (info.floorPrice) {
          setSupply.floorPrice = info.floorPrice;
        }

        if (info.adServerUrl) {
          setSupply.adServerUrl = info.adServerUrl;
        }

        if (info.waterfallNote?.length) {
          setSupply.waterfallNote = info.waterfallNote;
        }

        if (info.automaticFloorPrice) {
          setSupply.automaticFloorPrice = true;
          setSupply.fillRateThreshold = info.automaticFloorPrice.fillRateThreshold;
          setSupply.minimumFloor = info.automaticFloorPrice.minimumFloor;
          setSupply.maximumFloor = info.automaticFloorPrice.maximumFloor;
        } else {
          setSupply.automaticFloorPrice = false;
        }

        if (info.automaticOptimization) {
          setAutomaticOptimization = {
            status: AUTOMATIC_OPTIMIZATION.enabled,
            ...info.automaticOptimization,
          };
        } else {
          setAutomaticOptimization = {
            status: AUTOMATIC_OPTIMIZATION.disabled,
          };
        }
      }

      return concat(of(setSupplyTabAction(setSupply)), of(setAutomaticOptimizationTabAction(setAutomaticOptimization)));
    }),
  );
