import { AUTOMATIC_OPTIMIZATION, DEMAND_TAG_FORMAT, SOURCE_TAG_OPTIONS } from '@/modules/marketplace/account/constants';

import * as selectors from '../redux/selectors';

export const createSupplyTagPricingInput = (pricing) =>
  pricing.map((item) => {
    let price = {
      startDate: item.startDate,
      endDate: item.endDate,
    };

    if (item.model) {
      price = {
        ...price,
        model: item.model,
      };
    }

    if (item.payment) {
      price = {
        ...price,
        value: item.payment,
      };
    } else {
      price = {
        ...price,
        value: item.value,
      };
    }

    return price;
  });

const createSupplyTagRequestBody = (state, isAdminMP = true) => {
  const pageConfig = selectors.pageConfigSelector(state);
  const info = selectors.infoSelector(state);
  const automaticOptimization = selectors.automaticOptimizationSelector(state);
  const supply = selectors.supplySelector(state);

  const { id, supplyAccount, site, isCreatingNew, isDuplicate } = pageConfig;

  const { status: optimizationStatus, errors, ...restOptimization } = automaticOptimization;

  if (!isAdminMP) {
    const body = {
      name: supply.name,
      accountId: supplyAccount.id,
      siteId: site.id,
      publisherDemand: supply.publisherDemandOnly,
    };

    if (id && !isCreatingNew) {
      body.id = id;
    }

    if (supplyAccount.accountType !== 'VAST' && info?.accountType !== 'VAST') {
      body.format = supply.format;
    }

    return body;
  }

  const body = {
    name: supply.name,
    accountId: supplyAccount.id,
    siteId: site.id,
    waterfallNote: supply.waterfallNote,
    publisherDemand: supply.publisherDemandOnly,
    tagSource:
      supply.source !== SOURCE_TAG_OPTIONS[0].value
        ? {
            source: supply.source,
            platformId: supply.platformId,
          }
        : {
            source: supply.source,
          },
  };

  if (supply.format !== DEMAND_TAG_FORMAT.sponsored) {
    body.floorPrice = {
      floor: +supply.floorPrice.floor,
      viewableFloor: +supply.floorPrice.viewableFloor,
      firstRequestFloor: +supply.floorPrice.firstRequestFloor,
    };
  }

  if (isCreatingNew || isDuplicate) {
    const pricing = createSupplyTagPricingInput(supply.pricing);
    body.pricing = isCreatingNew ? pricing[0] : pricing;

    if (supply.expenses?.length) {
      const expenses = createSupplyTagPricingInput(supply.expenses);
      body.expenses = isCreatingNew ? expenses[0] : expenses;
    }
  }

  if (supplyAccount.accountType !== 'VAST' && info?.accountType !== 'VAST') {
    body.format = supply.format;
  }

  if (supply.format === DEMAND_TAG_FORMAT.display) {
    body.displayType = supply.displayType;
  }

  if (id && !isCreatingNew) {
    body.id = id;
  }

  if (supply.automaticFloorPrice) {
    body.automaticFloorPrice = {
      fillRateThreshold: +supply.fillRateThreshold,
      minimumFloor: +supply.minimumFloor,
      maximumFloor: +supply.maximumFloor,
    };
  } else if (!supply.automaticFloorPrice && !isCreatingNew && !isDuplicate) {
    body.automaticFloorPrice = {};
  }

  if (optimizationStatus === AUTOMATIC_OPTIMIZATION.enabled) {
    body.automaticOptimization = {
      ...restOptimization,
    };
  } else if (optimizationStatus === AUTOMATIC_OPTIMIZATION.disabled && !isCreatingNew && !isDuplicate) {
    body.automaticOptimization = {};
  }

  return body;
};

export default createSupplyTagRequestBody;
