"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 {
    device: {
      mobile
    } = {},
    response: {
      tracking = {}
    } = {},
    store: {
      currency,
      hotel: {
        accommodationType,
        variants = {}
      } = {},
      language,
      session
    } = {}
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  // ! -- TODO: Try to get all data from store -------------------------------------------------------------------------
  const {
    idchain,
    idHotel: hotelId
  } = (0, _helpers.fetchUrlParams)(location.search);

  // ! -- TODO: use of tracking should be a temporary approach ---------------------------------------------------------
  const {
    bookingValues: {
      checkin,
      checkout,
      country,
      coupon: promocode = '',
      nights
    } = {},
    guestData: {
      country: nationality,
      email,
      firstName,
      lastName
    } = {},
    hotelInfo: {
      hotelCity,
      hotelProvince,
      currency: hotelCurrency = {},
      propertyName,
      propertyType
    } = {},
    idReservation: reservationCode,
    reservationDate,
    reservedExtras = {},
    reservedRooms = [],
    step,
    totalPriceCommission,
    totalExtrasTaxes,
    totalExtrasWithTaxes,
    totalExtrasWithoutTaxes,
    totalNumAdults,
    totalNumChildren,
    totalNumRooms,
    totalPriceWithTaxes,
    totalPriceWithTaxesWithoutExtras,
    totalPriceWithoutTaxes,
    totalPriceWithoutTaxesWithoutExtras,
    totalTaxes
  } = tracking;
  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,
    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