"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.checkoutResponse = void 0;
var _locale = require("@mirai/locale");
var _helpers = require("../../../../components/helpers");
var _Core = require("../../../../Core.constants");
var _helpers2 = require("../../../../helpers");
var _helpers3 = require("../../helpers");
var _legacy = require("../helpers/legacy");
const ACCOMMODATION_TYPE_NAME = Object.keys(_Core.ACCOMMODATION_TYPE);
const checkoutResponse = function () {
  var _variants$hotelId;
  let {
    device: {
      mobile
    } = {},
    response: {
      infantAge,
      tracking = {}
    } = {},
    store: {
      currency,
      finder: {
        calendar = [],
        occupation = [],
        promocode = ''
      } = {},
      hotel: {
        accommodationType,
        variants = {}
      } = {},
      language,
      session
    } = {}
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  // ! -- TODO: Try to get all data from store -------------------------------------------------------------------------
  const {
    idchain,
    idtokenprovider: hotelId
  } = (0, _helpers2.fetchUrlParams)(location.search);

  // ! -- TODO: use of tracking should be a temporary approach ---------------------------------------------------------
  const {
    bookingValues: {
      userCurrency: {
        value: userCurrencyValue
      } = {}
    } = {},
    country,
    hotelInfo: {
      currency: hotelCurrency = {},
      propertyName,
      propertyType
    } = {},
    reservedExtras = {},
    reservedRooms = [],
    totalExtrasTaxes,
    totalExtrasWithTaxes,
    totalExtrasWithoutTaxes,
    totalPriceWithTaxes,
    totalPriceWithTaxesWithoutExtras,
    totalPriceWithoutTaxes,
    totalPriceWithoutTaxesWithoutExtras,
    totalTaxes
  } = tracking;
  const {
    adults = 0,
    children = 0
  } = (0, _helpers.getOccupationTotals)({
    infantAge,
    occupation
  });
  const {
    days: nights
  } = (0, _locale.dateDiff)(...calendar);
  const checkin = (0, _locale.dateFormat)(calendar[0], {
    format: 'DD/MM/YYYY'
  });
  const checkout = (0, _locale.dateFormat)(calendar[1], {
    format: 'DD/MM/YYYY'
  });
  const rooms = reservedRooms.map(function () {
    let {
      averagePriceWithoutTaxes,
      daysPriceBoard = [],
      defaultBoardDescription,
      numRoomsToReserve,
      occupation: {
        numAdults,
        numBabies,
        totalChildren: numChildren
      } = {},
      rateId,
      rateName,
      roomTypeName
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    let priceWithTaxes = 0;
    let priceWithoutTaxes = 0;
    daysPriceBoard.forEach(day => {
      priceWithTaxes += day.priceWithTaxes;
      priceWithoutTaxes += day.priceWithoutTaxes;
    });
    return {
      boardName: defaultBoardDescription,
      numRooms: numRoomsToReserve,
      occupation: {
        numAdults,
        numChildren,
        numBabies
      },
      rateId,
      rateName,
      roomName: roomTypeName,
      priceWithTaxes,
      priceWithoutTaxes,
      unitaryPriceWithoutTaxes: (0, _legacy.formatPrice)(averagePriceWithoutTaxes, hotelCurrency)
    };
  });
  const extras = Object.values(reservedExtras).map(function () {
    let {
      amount,
      id,
      name,
      nightsToApply,
      totalPriceWithTaxes,
      totalPriceWithoutTaxes,
      unitaryPriceWithTaxes,
      unitaryPriceWithoutTaxes
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return {
      amount,
      extraId: id,
      extraName: name,
      nightsToApply,
      totalPriceWithTaxes,
      totalPriceWithoutTaxes,
      unitaryPriceWithTaxes,
      unitaryPriceWithoutTaxes
    };
  });
  const dataLayer = {
    chainId: idchain,
    checkin,
    checkout,
    coupon: promocode,
    currency: hotelCurrency.code,
    device: mobile ? 'MOBILE' : 'DESKTOP_TABLET',
    event: 'init',
    exchangeRate: userCurrencyValue && hotelCurrency.value && userCurrencyValue / hotelCurrency.value,
    home: document.location.hostname,
    hotelId,
    language,
    lodgingDenomination: ACCOMMODATION_TYPE_NAME[((_variants$hotelId = variants[hotelId]) === null || _variants$hotelId === void 0 ? void 0 : _variants$hotelId.accommodationType) || accommodationType],
    nights,
    numAdults: adults,
    numChildren: children,
    numRooms: occupation.length,
    products: {
      rooms,
      extras,
      checkin,
      checkout
    },
    propertyName,
    propertyType,
    step: 'CLIENTFORM',
    totalExtrasTaxes: (0, _legacy.formatPrice)(totalExtrasTaxes, hotelCurrency),
    totalExtrasWithTaxes: (0, _legacy.formatPrice)(totalExtrasWithTaxes, hotelCurrency),
    totalExtrasWithoutTaxes: (0, _legacy.formatPrice)(totalExtrasWithoutTaxes, hotelCurrency),
    totalPriceWithTaxesWithExtras: (0, _legacy.formatPrice)(totalPriceWithTaxes, hotelCurrency),
    totalPriceWithTaxesWithoutExtras: (0, _legacy.formatPrice)(totalPriceWithTaxesWithoutExtras, hotelCurrency),
    totalPriceWithoutTaxesWithExtras: (0, _legacy.formatPrice)(totalPriceWithoutTaxes, hotelCurrency),
    totalPriceWithoutTaxesWithoutExtras: (0, _legacy.formatPrice)(totalPriceWithoutTaxesWithoutExtras, hotelCurrency),
    totalTaxes: (0, _legacy.formatPrice)(totalTaxes, hotelCurrency),
    userCountry: country,
    userCurrency: currency,
    userLogged: !!session
  };
  (0, _helpers3.dataLayerPush)(dataLayer);
  return dataLayer;
};
exports.checkoutResponse = checkoutResponse;
//# sourceMappingURL=checkoutResponse.js.map