"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 _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    _ref$device = _ref.device,
    _ref$device2 = _ref$device === void 0 ? {} : _ref$device,
    mobile = _ref$device2.mobile,
    _ref$response = _ref.response,
    _ref$response2 = _ref$response === void 0 ? {} : _ref$response,
    infantAge = _ref$response2.infantAge,
    _ref$response2$tracki = _ref$response2.tracking,
    tracking = _ref$response2$tracki === void 0 ? {} : _ref$response2$tracki,
    _ref$store = _ref.store,
    _ref$store2 = _ref$store === void 0 ? {} : _ref$store,
    currency = _ref$store2.currency,
    _ref$store2$finder = _ref$store2.finder,
    _ref$store2$finder2 = _ref$store2$finder === void 0 ? {} : _ref$store2$finder,
    _ref$store2$finder2$c = _ref$store2$finder2.calendar,
    calendar = _ref$store2$finder2$c === void 0 ? [] : _ref$store2$finder2$c,
    _ref$store2$finder2$o = _ref$store2$finder2.occupation,
    occupation = _ref$store2$finder2$o === void 0 ? [] : _ref$store2$finder2$o,
    _ref$store2$finder2$p = _ref$store2$finder2.promocode,
    promocode = _ref$store2$finder2$p === void 0 ? '' : _ref$store2$finder2$p,
    _ref$store2$hotel = _ref$store2.hotel,
    _ref$store2$hotel2 = _ref$store2$hotel === void 0 ? {} : _ref$store2$hotel,
    accommodationType = _ref$store2$hotel2.accommodationType,
    _ref$store2$hotel2$va = _ref$store2$hotel2.variants,
    variants = _ref$store2$hotel2$va === void 0 ? {} : _ref$store2$hotel2$va,
    language = _ref$store2.language,
    session = _ref$store2.session;
  // ! -- TODO: Try to get all data from store -------------------------------------------------------------------------
  const _fetchUrlParams = (0, _helpers2.fetchUrlParams)(location.search),
    idchain = _fetchUrlParams.idchain,
    hotelId = _fetchUrlParams.idtokenprovider;

  // ! -- TODO: use of tracking should be a temporary approach ---------------------------------------------------------
  const _tracking$bookingValu = tracking.bookingValues,
    _tracking$bookingValu2 = _tracking$bookingValu === void 0 ? {} : _tracking$bookingValu,
    _tracking$bookingValu3 = _tracking$bookingValu2.userCurrency,
    _tracking$bookingValu4 = _tracking$bookingValu3 === void 0 ? {} : _tracking$bookingValu3,
    userCurrencyValue = _tracking$bookingValu4.value,
    country = tracking.country,
    _tracking$hotelInfo = tracking.hotelInfo,
    _tracking$hotelInfo2 = _tracking$hotelInfo === void 0 ? {} : _tracking$hotelInfo,
    _tracking$hotelInfo2$ = _tracking$hotelInfo2.currency,
    hotelCurrency = _tracking$hotelInfo2$ === void 0 ? {} : _tracking$hotelInfo2$,
    propertyName = _tracking$hotelInfo2.propertyName,
    propertyType = _tracking$hotelInfo2.propertyType,
    _tracking$reservedExt = tracking.reservedExtras,
    reservedExtras = _tracking$reservedExt === void 0 ? {} : _tracking$reservedExt,
    _tracking$reservedRoo = tracking.reservedRooms,
    reservedRooms = _tracking$reservedRoo === void 0 ? [] : _tracking$reservedRoo,
    totalExtrasTaxes = tracking.totalExtrasTaxes,
    totalExtrasWithTaxes = tracking.totalExtrasWithTaxes,
    totalExtrasWithoutTaxes = tracking.totalExtrasWithoutTaxes,
    totalPriceWithTaxes = tracking.totalPriceWithTaxes,
    totalPriceWithTaxesWithoutExtras = tracking.totalPriceWithTaxesWithoutExtras,
    totalPriceWithoutTaxes = tracking.totalPriceWithoutTaxes,
    totalPriceWithoutTaxesWithoutExtras = tracking.totalPriceWithoutTaxesWithoutExtras,
    totalTaxes = tracking.totalTaxes;
  const _getOccupationTotals = (0, _helpers.getOccupationTotals)({
      infantAge,
      occupation
    }),
    _getOccupationTotals$ = _getOccupationTotals.adults,
    adults = _getOccupationTotals$ === void 0 ? 0 : _getOccupationTotals$,
    _getOccupationTotals$2 = _getOccupationTotals.children,
    children = _getOccupationTotals$2 === void 0 ? 0 : _getOccupationTotals$2;
  const _dateDiff = (0, _locale.dateDiff)(...calendar),
    nights = _dateDiff.days;
  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 _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      averagePriceWithoutTaxes = _ref2.averagePriceWithoutTaxes,
      _ref2$daysPriceBoard = _ref2.daysPriceBoard,
      daysPriceBoard = _ref2$daysPriceBoard === void 0 ? [] : _ref2$daysPriceBoard,
      defaultBoardDescription = _ref2.defaultBoardDescription,
      numRoomsToReserve = _ref2.numRoomsToReserve,
      _ref2$occupation = _ref2.occupation,
      _ref2$occupation2 = _ref2$occupation === void 0 ? {} : _ref2$occupation,
      numAdults = _ref2$occupation2.numAdults,
      numBabies = _ref2$occupation2.numBabies,
      numChildren = _ref2$occupation2.totalChildren,
      rateId = _ref2.rateId,
      rateName = _ref2.rateName,
      roomTypeName = _ref2.roomTypeName;
    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 _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      amount = _ref3.amount,
      id = _ref3.id,
      name = _ref3.name,
      nightsToApply = _ref3.nightsToApply,
      totalPriceWithTaxes = _ref3.totalPriceWithTaxes,
      totalPriceWithoutTaxes = _ref3.totalPriceWithoutTaxes,
      unitaryPriceWithTaxes = _ref3.unitaryPriceWithTaxes,
      unitaryPriceWithoutTaxes = _ref3.unitaryPriceWithoutTaxes;
    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