"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.bookingResponse = void 0;
var _duetto = require("./duetto");
var _Core = require("../../../../Core.constants");
var _helpers = require("../../../../helpers");
var _helpers2 = require("../../helpers");
var _helpers3 = require("../helpers");
var _legacy = require("../helpers/legacy");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const ACCOMMODATION_TYPE_NAME = Object.keys(_Core.ACCOMMODATION_TYPE);
const bookingResponse = 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,
    _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$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, _helpers.fetchUrlParams)(location.search),
    idchain = _fetchUrlParams.idchain,
    hotelId = _fetchUrlParams.idHotel;

  // ! -- TODO: use of tracking should be a temporary approach ---------------------------------------------------------
  const _tracking$bookingValu = tracking.bookingValues,
    _tracking$bookingValu2 = _tracking$bookingValu === void 0 ? {} : _tracking$bookingValu,
    checkin = _tracking$bookingValu2.checkin,
    checkout = _tracking$bookingValu2.checkout,
    country = _tracking$bookingValu2.country,
    _tracking$bookingValu3 = _tracking$bookingValu2.coupon,
    promocode = _tracking$bookingValu3 === void 0 ? '' : _tracking$bookingValu3,
    nights = _tracking$bookingValu2.nights,
    _tracking$guestData = tracking.guestData,
    _tracking$guestData2 = _tracking$guestData === void 0 ? {} : _tracking$guestData,
    nationality = _tracking$guestData2.country,
    email = _tracking$guestData2.email,
    firstName = _tracking$guestData2.firstName,
    lastName = _tracking$guestData2.lastName,
    _tracking$hotelInfo = tracking.hotelInfo,
    _tracking$hotelInfo2 = _tracking$hotelInfo === void 0 ? {} : _tracking$hotelInfo,
    hotelCity = _tracking$hotelInfo2.hotelCity,
    hotelProvince = _tracking$hotelInfo2.hotelProvince,
    _tracking$hotelInfo2$ = _tracking$hotelInfo2.currency,
    hotelCurrency = _tracking$hotelInfo2$ === void 0 ? {} : _tracking$hotelInfo2$,
    propertyName = _tracking$hotelInfo2.propertyName,
    propertyType = _tracking$hotelInfo2.propertyType,
    reservationCode = tracking.idReservation,
    reservationDate = tracking.reservationDate,
    _tracking$reservedExt = tracking.reservedExtras,
    reservedExtras = _tracking$reservedExt === void 0 ? {} : _tracking$reservedExt,
    _tracking$reservedRoo = tracking.reservedRooms,
    reservedRooms = _tracking$reservedRoo === void 0 ? [] : _tracking$reservedRoo,
    step = tracking.step,
    totalPriceCommission = tracking.totalPriceCommission,
    totalExtrasTaxes = tracking.totalExtrasTaxes,
    totalExtrasWithTaxes = tracking.totalExtrasWithTaxes,
    totalExtrasWithoutTaxes = tracking.totalExtrasWithoutTaxes,
    totalNumAdults = tracking.totalNumAdults,
    totalNumChildren = tracking.totalNumChildren,
    totalNumRooms = tracking.totalNumRooms,
    totalPriceWithTaxes = tracking.totalPriceWithTaxes,
    totalPriceWithTaxesWithoutExtras = tracking.totalPriceWithTaxesWithoutExtras,
    totalPriceWithoutTaxes = tracking.totalPriceWithoutTaxes,
    totalPriceWithoutTaxesWithoutExtras = tracking.totalPriceWithoutTaxesWithoutExtras,
    totalTaxes = tracking.totalTaxes;
  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,
    city: hotelCity,
    cityZone: '',
    coupon: promocode,
    currency: hotelCurrency.code,
    device: mobile ? 'MOBILE' : 'DESKTOP_TABLET',
    email,
    emailSHA256: (0, _helpers3.SHA256)(email),
    event: 'init',
    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],
    name: firstName,
    nights,
    numAdults: totalNumAdults,
    numChildren: totalNumChildren,
    numRooms: totalNumRooms,
    products: {
      rooms,
      extras,
      checkin,
      checkout
    },
    propertyName,
    propertyType,
    reservationCode,
    reservationDate,
    state_provice: hotelProvince,
    step,
    surname: lastName,
    nationality,
    totalCommission: (0, _legacy.formatPrice)(totalPriceCommission, hotelCurrency),
    totalExtrasTaxes: (0, _legacy.formatPrice)(totalExtrasTaxes, hotelCurrency),
    totalExtrasWithoutTaxes: (0, _legacy.formatPrice)(totalExtrasWithoutTaxes, hotelCurrency),
    totalExtrasWithTaxes: (0, _legacy.formatPrice)(totalExtrasWithTaxes, hotelCurrency),
    totalPrice: (0, _legacy.formatPrice)(totalPriceWithTaxes, hotelCurrency),
    totalPriceWithoutTaxes: (0, _legacy.formatPrice)(totalPriceWithoutTaxes, hotelCurrency),
    totalPriceWithoutTaxesWithExtras: (0, _legacy.formatPrice)(totalPriceWithoutTaxes, hotelCurrency),
    totalPriceWithoutTaxesWithoutExtras: (0, _legacy.formatPrice)(totalPriceWithoutTaxesWithoutExtras, hotelCurrency),
    totalPriceWithTaxesWithExtras: (0, _legacy.formatPrice)(totalPriceWithTaxes, hotelCurrency),
    totalPriceWithTaxesWithoutExtras: (0, _legacy.formatPrice)(totalPriceWithTaxesWithoutExtras, hotelCurrency),
    totalTaxes: (0, _legacy.formatPrice)(totalTaxes, hotelCurrency),
    transactionAffiliation: propertyName,
    transactionId: "Reservation code ".concat(reservationCode),
    transactionProducts: [...reservedRooms.map(function () {
      let room = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return {
        sku: room.defaultMediumName,
        name: propertyName,
        category: room.defaultBoardDescription,
        price: (0, _legacy.formatPrice)(room.averagePriceWithoutTaxes, hotelCurrency),
        quantity: nights * room.numRoomsToReserve
      };
    }), ...Object.values(reservedExtras).map(extra => ({
      sku: extra.name,
      name: propertyName,
      category: 'Extra',
      price: (0, _legacy.formatPrice)(extra.unitaryPriceWithoutTaxes, hotelCurrency),
      quantity: extra.multiplierPrice
    }))],
    transactionShipping: parseFloat(totalPriceCommission),
    transactionTax: (0, _legacy.formatPrice)(totalTaxes, hotelCurrency),
    transactionTotal: (0, _legacy.formatPrice)(totalPriceWithoutTaxes, hotelCurrency),
    userCountry: country,
    userCurrency: currency,
    userLogged: !!session
  };
  (0, _helpers2.dataLayerPush)(dataLayer);
  (0, _duetto.bookingResponse)(_objectSpread({
    checkin,
    checkout,
    hotelCurrency,
    reservedRooms
  }, tracking));
  return dataLayer;
};
exports.bookingResponse = bookingResponse;
//# sourceMappingURL=bookingResponse.js.map