import * as selectors from '../redux/selectors';
import { isNumber } from '@/modules/@common/helpers/number';

function getValueFromState(state$) {
  return {
    salesforceId: selectors.salesforceIdSelector(state$),
    name: selectors.nameSelector(state$),
    publisherDemand: selectors.publisherDemandSelector(state$),
    adServingFeeBusinessModel: selectors.adServingFeeBusinessModelSelector(state$),
    adServingFeeDisplay: selectors.adServingFeeDisplaySelector(state$),
    adServingFeeVideo: selectors.adServingFeeVideoSelector(state$),
    salesManager: selectors.salesManagerSelector(state$),
    accountManager: selectors.accountManagerSelector(state$),
    accountManagerEmail: selectors.accountManagerEmailSelector(state$),
    type: selectors.typeSelector(state$),
    avatarUrl: selectors.avatarUrlSelector(state$),
    avatar: selectors.avatarSelector(state$),
    customLogoUrl: selectors.customLogoUrlSelector(state$),
    customLoadingMessage: selectors.customLoadingMessageSelector(state$),
    customLoginPageUrl: selectors.customLoginPageUrlSelector(state$),
    subdomain: selectors.subdomainSelector(state$),
    publisherRevShare: selectors.publisherRevShareSelector(state$),
    expenses: selectors.expensesSelector(state$),
    usersLimit: selectors.usersLimitSelector(state$),
    featuresData: selectors.featuresDataSelector(state$),
    dashboardsGeneral: selectors.dashboardsGeneralSelector(state$),
    dashboardsCustom: selectors.dashboardsCustomSelector(state$),
    videoDuplicatesBy: selectors.videoDuplicatesBy(state$),
  };
}

function buildRequestBodyBase(stateValue) {
  const body = {
    type: stateValue.type,
    usersLimit: +stateValue.usersLimit,
    subdomain: stateValue.subdomain,
    publisherRevShare: isNumber(parseInt(stateValue.publisherRevShare, 10))
      ? parseInt(stateValue.publisherRevShare, 10)
      : null,
    expenses: isNumber(parseInt(stateValue.expenses, 10)) ? parseInt(stateValue.expenses, 10) : null,
    customLogoUrl: stateValue.customLogoUrl || null,
    customLoginPageUrl: stateValue.customLoginPageUrl || null,
    customLoadingMessage: stateValue.customLoadingMessage || null,
    accountsFeatures: Object.keys(stateValue.featuresData).map((key) => ({
      id: +key,
      value: Boolean(stateValue.featuresData[key]),
    })),
    accountDashboards: {},
    avatar: stateValue.avatar?.base64Url ? stateValue.avatar : null,
    adServingFeeBusinessModel: stateValue.adServingFeeBusinessModel,
    adServingFeeVideo: Number.isNaN(+stateValue.adServingFeeVideo) ? null : +stateValue.adServingFeeVideo,
    adServingFeeDisplay: Number.isNaN(+stateValue.adServingFeeDisplay) ? null : +stateValue.adServingFeeDisplay,
    videoDuplicatesBy: stateValue.videoDuplicatesBy || null,
  };

  if (stateValue.avatarUrl) {
    body.avatarUrl = stateValue.avatarUrl;
  }

  if (stateValue.dashboardsCustom?.length) {
    body.accountDashboards.custom = stateValue.dashboardsCustom.map((entity) => ({
      ...entity,
      enabled: Boolean(entity.enabled),
    }));
  }

  if (stateValue.dashboardsGeneral?.length) {
    body.accountDashboards.general = stateValue.dashboardsGeneral.map((entity) => ({
      ...entity,
      enabled: Boolean(entity.enabled),
    }));
  }

  return body;
}

export function buildUpdateRequestBody(state$) {
  const stateValue = getValueFromState(state$);
  return {
    ...buildRequestBodyBase(stateValue),
  };
}

export function buildCreateRequestBody(state$) {
  const stateValue = getValueFromState(state$);
  return {
    ...buildRequestBodyBase(stateValue),
    salesforceId: stateValue.salesforceId,
    publisherDemand: stateValue.publisherDemand,
  };
}
