import { getDefaultDomain } from './getDefaultDomain';

function parseResponseToState(response) {
  const state = {
    id: response.id,
    // tab general
    account: {
      value: response.account.id,
      label: response.account.name,
      salesforceId: response.account.salesforceId,
    },
    name: response.name,
    domains: response.publisherDomains.map((o) => ({
      label: o.domain,
      isDisabled: !!o.players.length || o.domain === getDefaultDomain(),
    })),
    publish: response.publish,
    // syndicate content tab
    includePublicContent: response.includePublicContent,
    accounts: response.publisherContentOwners
      // distinct
      .reduce((acc, o) => {
        if (!o.account) {
          return acc;
        }

        const isExit = acc.some((oo) => oo?.account.id === o.account.id);
        if (!isExit) {
          acc.push(o);
        }

        return acc;
      }, [])
      .filter((o) => o.account && o.account.id !== response.account.id)
      .map((o) => ({
        value: o.account.id,
        label: o.account.name,
      })),
    // player self service tab
    selfService: response.selfService,
    templatePlayerLumX: response.templatePlayerLumX
      ? {
          value: response.templatePlayerLumX,
          label: response.templatePlayerLumXName || 'Need return name from API',
        }
      : null,
    templatePlayerLumXAmp: response.templatePlayerLumXAmp
      ? {
          value: response.templatePlayerLumXAmp,
          label: response.templatePlayerLumXAmpName || 'Need return name from API',
        }
      : null,
    templatePlayerLumN: response.templatePlayerLumN
      ? {
          value: response.templatePlayerLumN,
          label: response.templatePlayerLumNName || 'Need return name from API',
        }
      : null,
    templatePlayerLumNAmp: response.templatePlayerLumNAmp
      ? {
          value: response.templatePlayerLumNAmp,
          label: response.templatePlayerLumNAmpName || 'Need return name from API',
        }
      : null,
    templatePlayerVertical: response.templatePlayerVertical
      ? {
          value: response.templatePlayerVertical,
          label: response.templatePlayerVerticalName || 'Need return name from API',
        }
      : null,
    templatePlayerOutstream: response.templatePlayerOutstream
      ? {
          value: response.templatePlayerOutstream,
          label: response.templatePlayerOutstreamName || 'Need return name from API',
        }
      : null,
    monetization: response.monetization,
    firstLook: response.firstLook,
    cpm: response.cpm,
    // advanced tab
    pageRefresh: response.pageRefresh,
    refreshIntervalMinutes: response.refreshIntervalMinutes,
    refreshTimesLimit: response.refreshTimesLimit,
    ddSpecificPublisher: response.ddSpecificPublisher,
    defaultRule: response.defaultRule,
    cleanUrl: response.cleanUrl.sort((a, b) => (a.cleanOrder > b.cleanOrder ? 1 : -1)),
    mpSelfService: response.mpSelfService,
    demandAccountId: response.demandAccountId,
    demandAccount: response.demandAccount,
  };

  return state;
}

export default parseResponseToState;
