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

const createRequestBody = (state, isUpdate = false) => {
  const account = selectors.accountSelector(state);
  const accounts = selectors.accountsSelector(state);
  const cleanUrl = selectors.cleanUrlSelector(state);
  const cpm = parseFloat(selectors.cpmSelector(state));
  const ddSpecificPublisher = selectors.ddSpecificPublisherSelector(state);
  const defaultRule = selectors.defaultRuleSelector(state);
  const domains = selectors.domainsSelector(state);
  const firstLook = selectors.firstLookSelector(state);
  const includePublicContent = selectors.includePublicContentSelector(state);
  const monetization = selectors.monetizationSelector(state);
  const name = selectors.nameSelector(state);
  const pageRefresh = selectors.pageRefreshSelector(state);
  const publish = selectors.publishSelector(state);
  const refreshIntervalMinutes = parseInt(selectors.refreshIntervalMinutesSelector(state), 10);
  const refreshTimesLimit = parseInt(selectors.refreshTimesLimitSelector(state), 10);
  const selfService = selectors.selfServiceSelector(state);
  const templatePlayerLumN = selectors.templatePlayerLumNSelector(state);
  const templatePlayerLumNAmp = selectors.templatePlayerLumNAmpSelector(state);
  const templatePlayerLumX = selectors.templatePlayerLumXSelector(state);
  const templatePlayerLumXAmp = selectors.templatePlayerLumXAmpSelector(state);
  const templatePlayerVertical = selectors.templatePlayerVerticalSelector(state);
  const templatePlayerOutstream = selectors.templatePlayerOutstreamSelector(state);
  const mpSelfService = selectors.mpSelfServiceSelector(state);
  const demandAccountId = selectors.demandAccountIdSelector(state);

  const body = {
    accountId: parseInt(account.value, 10),
    accounts: accounts.map((entity) => ({
      id: entity.value,
      name: entity.label,
      salesforceId: entity.salesforceId,
    })),
    cleanUrl: cleanUrl.map((o, i) => ({ ...o, cleanOrder: i + 1 })),
    cpm,
    ddSpecificPublisher,
    defaultRule: {
      cleanOrder: 0,
      cleanUrl: name,
      ...defaultRule,
    },
    domains: domains.map((o) => o.label),
    firstLook,
    includePublicContent,
    monetization,
    name,
    pageRefresh,
    publish,
    refreshIntervalMinutes,
    refreshTimesLimit,
    selfService,
    mpSelfService,
    demandAccountId,
    templatePlayerLumN: templatePlayerLumN?.value ?? null,
    templatePlayerLumNAmp: templatePlayerLumNAmp?.value ?? null,
    templatePlayerLumX: templatePlayerLumX?.value ?? null,
    templatePlayerLumXAmp: templatePlayerLumXAmp?.value ?? null,
    templatePlayerVertical: templatePlayerVertical?.value ?? null,
    templatePlayerOutstream: templatePlayerOutstream?.value ?? null,
  };

  if (isUpdate) {
    body.id = selectors.idSelector(state);

    delete body.accountId;
  }

  return body;
};

export default createRequestBody;
