import { DEMAND_TAG_PRICING_TYPE } from '@/modules/marketplace/account/constants';

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

import { createFeesListInput, createPricesListInput } from './createDemandTagRequestBody';

export const definePricingTableKey = (type) => {
  switch (type) {
    case DEMAND_TAG_PRICING_TYPE.price:
      return 'fixedRpmRateTable';

    case DEMAND_TAG_PRICING_TYPE.fee:
      return 'additionalFeesTable';

    case DEMAND_TAG_PRICING_TYPE.adServing:
      return 'adServingFeesTable';

    case DEMAND_TAG_PRICING_TYPE.adRequest:
      return 'adRequestFeesTable';

    default:
      return 'fixedRpmRateTable';
  }
};

const defineKey = (type) => {
  switch (type) {
    case DEMAND_TAG_PRICING_TYPE.price:
      return 'pricing';

    case DEMAND_TAG_PRICING_TYPE.fee:
      return 'fee';

    case DEMAND_TAG_PRICING_TYPE.adServing:
      return 'adServingFees';

    case DEMAND_TAG_PRICING_TYPE.adRequest:
      return 'adRequest';

    default:
      return 'pricing';
  }
};

const createDemandTagUpdatePriceRequestBody = (state, { type, data }) => {
  const settings = selectors.settingsSelector(state);

  const key = defineKey(type);
  const createInputFunction =
    type === DEMAND_TAG_PRICING_TYPE.price || type === DEMAND_TAG_PRICING_TYPE.adServing
      ? createPricesListInput
      : createFeesListInput;

  const body = {
    id: settings.uid,
    [key]: createInputFunction([data]),
  };

  return body;
};

export default createDemandTagUpdatePriceRequestBody;
