import initialValues from 'client/stores/CalculationStore/config/initialValues';
import { IQuote } from 'core/services/CrmService/types/entities';
import { ValuesNames } from 'core/types/Calculation/Store/values';

const mapKPtoValues: Partial<Record<keyof IQuote | string, ValuesNames>> = {
  evo_baseproductid: 'product',
  evo_client_typeid: 'clientType',
  evo_supplier_currency_price: 'leaseObjectPrice',
  evo_price_without_nds_supplier_currency: 'leaseObjectPriceWthtVAT',
  evo_nds_in_price_supplier_currency: 'VATInLeaseObjectPrice',
  evo_transactioncurrencyid: 'supplierCurrency',
  evo_discount_supplier_currency: 'supplierDiscountRub',
  evo_discount_perc: 'supplierDiscountPerc',
  evo_first_payment_perc: 'firstPaymentPerc',
  evo_last_payment_calc: 'lastPaymentRule',
  evo_last_payment_perc: 'lastPaymentPerc',
  evo_last_payment_rub: 'lastPaymentRub',
  // evo_balance_holder: 'balanceHolder',
  evo_graph_type: 'graphType',
  evo_payments_decrease_perc: 'parmentsDecreasePercent',
  evo_seasons_type: 'seasonType',
  evo_high_season: 'highSeasonStart',
  evo_comission_perc: 'comissionPerc',
  evo_comission_rub: 'comissionRub',
  evo_sale_bonus: 'saleBonus',
  // evo_period: 'leasingPeriod',
  evo_tarifid: 'tarif',
  // evo_rate: 'creditRate',
  evo_msfo_irr: 'IRR_Perc',
  evo_leasingobject_typeid: 'leaseObjectType',
  evo_delivery_time: 'deliveryTime',
  evo_brandid: 'brand',
  evo_modelid: 'model',
  evo_equipmentid: 'configuration',
  evo_year: 'leaseObjectYear',
  evo_engine_type: 'engineType',
  evo_category: 'leaseObjectCategory',
  evo_power: 'leaseObjectMotorPower',
  evo_engine_volume: 'engineVolume',
  evo_use_for: 'leaseObjectUseFor',
  evo_trailer: 'withTrailer',
  evo_leasingobject_used: 'leaseObjectUsed',
  evo_max_mass: 'maxMass',
  evo_seats: 'countSeats',
  evo_max_speed: 'maxSpeed',
  evo_supplier_accountid: 'dealer',
  // evo_dealer_person_accountid: 'dealerPerson',
  evo_dealer_reward_conditionid: 'dealerRewardCondition',
  // evo_dealer_reward_total: 'dealerRewardSumm',
  evo_dealer_broker_accountid: 'dealerBroker',
  evo_dealer_broker_reward_conditionid: 'dealerBrokerRewardCondition',
  // evo_dealer_broker_reward_total: 'dealerBrokerRewardSumm',
  // evo_agent_accountid: indAgent,
  // evo_agent_reward_conditionid: 'indAgentRewardCondition',
  // evo_agent_reward_total: indAgentRewardSumm,
  evo_double_agent_accountid: 'calcDoubleAgent',
  evo_double_agent_reward_conditionid: 'calcDoubleAgentRewardCondition',
  // evo_double_agent_reward_total: 'calcDoubleAgentRewardSumm',
  // evo_broker_accountid: 'calcBroker',
  // evo_broker_reward_conditionid: 'calcBrokerRewardCondition',
  // evo_broker_reward_total: 'calcBrokerRewardSum',
  // evo_fin_department_accountid: 'calcFinDepartment',
  // evo_fin_department_reward_conditionid: 'finDepartmentRewardCondtion',
  // evo_fin_department_reward_total: 'finDepartmentRewardSumm'
  evo_gps_brandid: 'GPSBrand',
  evo_gps_modelid: 'GPSModel',
  evo_ins_legal_form: 'infuranceOPF',
  evo_insurance_type: 'insKaskoType',
  evo_insurance_decentral: 'insDecentral',
  evo_franchise: 'insFranchise',
  evo_unlimit_drivers: 'insUnlimitDrivers',
  evo_age_drivers: 'insAgeDrivers',
  evo_exp_drivers: 'insExpDrivers',
  evo_last_payment_redemption: 'lastPaymentRedemption',
  evo_price_with_discount: 'priceWithDiscount',
  evo_cost_increace: 'costIncrease',
  evo_insurance: 'insurance',
  evo_registration_quote: 'registrationQuote',
  evo_card_quote: 'technicalCardQuote',
  evo_nsib_quote: 'NSIB',
  evo_contact_name: 'quoteName',
  evo_gender: 'quoteContactGender',
  evo_redemption_graph: 'quoteRedemptionGraph',
  evo_min_change_price: 'minPriceChange',
  evo_max_price_change: 'maxPriceChange',
  evo_importer_reward_perc: 'importerRewardPerc',
  evo_importer_reward_rub: 'importerRewardRub',
  // evo_req_telematic_accept: 'requirementTelematic',
  evo_mileage: 'mileage',
  evo_price_without_discount_quote: 'fullPriceWithDiscount',
  // evo_object_registration: 'objectRegistration',
  evo_registration_regionid: 'objectRegionRegistration',
  // evo_vehicle_tax_approved: vehicleTaxInYear,
  evo_category_tr: 'objectCategoryTax',
  evo_vehicle_type_tax: 'objectTypeTax',
  evo_pts_type: 'typePTS',
  evo_subsidyid: 'subsidy',
  evo_subsidy_summ: 'subsidySum',
  evo_program_import_subsidyid: 'importProgram',
  evo_equip_price: 'addEquipmentPrice',
  evo_vin: 'vin',
};

export function getValuesFromKP(quote: IQuote) {
  return Object.keys(mapKPtoValues).reduce((acc, kpProp) => {
    const valueName = mapKPtoValues[kpProp];
    if (valueName) {
      let value = quote[kpProp];
      if (value === undefined || value === null) {
        value = initialValues[valueName];
      }
      acc[valueName] = value;
    }
    return acc;
  }, {} as Partial<Record<ValuesNames, any>>);
}
