"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ratesResponse = void 0;
var _locale = require("@mirai/locale");
var _duetto = require("./duetto");
var _helpers = require("./helpers");
var _helpers2 = require("../../../../components/helpers");
var _helpers3 = require("../../../../components/Rates/helpers");
var _Core = require("../../../../Core.constants");
var _helpers4 = require("../../../../helpers");
var _helpers5 = require("../../helpers");
var _helpers6 = 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 ratesResponse = 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,
    calendarSuggestion = _ref$response2.calendar,
    currency = _ref$response2.currency,
    _ref$response2$finder = _ref$response2.finder,
    _ref$response2$finder2 = _ref$response2$finder === void 0 ? {} : _ref$response2$finder,
    _ref$response2$finder3 = _ref$response2$finder2.calendar,
    calendar = _ref$response2$finder3 === void 0 ? [] : _ref$response2$finder3,
    _ref$response2$finder4 = _ref$response2$finder2.occupation,
    occupation = _ref$response2$finder4 === void 0 ? [] : _ref$response2$finder4,
    _ref$response2$finder5 = _ref$response2$finder2.place,
    _ref$response2$finder6 = _ref$response2$finder5 === void 0 ? {} : _ref$response2$finder5,
    searchCategory = _ref$response2$finder6.title,
    _ref$response2$finder7 = _ref$response2$finder2.promocode,
    promocode = _ref$response2$finder7 === void 0 ? '' : _ref$response2$finder7,
    hotels = _ref$response2.hotels,
    infantAge = _ref$response2.infantAge,
    language = _ref$response2.language,
    session = _ref$response2.session,
    _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,
    _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$fe = _ref$store2$hotel2.features,
    features = _ref$store2$hotel2$fe === void 0 ? {} : _ref$store2$hotel2$fe,
    _ref$store2$hotel2$va = _ref$store2$hotel2.variants,
    variants = _ref$store2$hotel2$va === void 0 ? {} : _ref$store2$hotel2$va,
    id = _ref$store2.id,
    type = _ref$store2.type,
    urlParams = _ref$store2.urlParams;
  // ! -- TODO: Try to get all data from store -------------------------------------------------------------------------
  const _fetchUrlParams = (0, _helpers4.fetchUrlParams)(location.search),
    idchain = _fetchUrlParams.idchain,
    idoffers = _fetchUrlParams.idoffers,
    hotelId = _fetchUrlParams.idtokenprovider,
    roomTypeId = _fetchUrlParams.roomTypeId;

  // ! -- TODO: use of tracking should be a temporary approach ---------------------------------------------------------
  const 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,
    hotelInfoBAR = tracking.hotelInfoBAR,
    localTaxesIncluded = tracking.localTaxesIncluded,
    rates = tracking.rates,
    taxesIncluded = tracking.taxesIncluded,
    _tracking$userCurrenc = tracking.userCurrency,
    _tracking$userCurrenc2 = _tracking$userCurrenc === void 0 ? {} : _tracking$userCurrenc,
    userCurrencyValue = _tracking$userCurrenc2.value,
    _tracking$userLocatio = tracking.userLocation,
    userLocation = _tracking$userLocatio === void 0 ? {} : _tracking$userLocatio;
  const _getOccupationTotals = (0, _helpers2.getOccupationTotals)({
      infantAge,
      occupation
    }),
    _getOccupationTotals$ = _getOccupationTotals.adults,
    adults = _getOccupationTotals$ === void 0 ? 0 : _getOccupationTotals$,
    _getOccupationTotals$2 = _getOccupationTotals.babies,
    babies = _getOccupationTotals$2 === void 0 ? 0 : _getOccupationTotals$2,
    _getOccupationTotals$3 = _getOccupationTotals.children,
    children = _getOccupationTotals$3 === void 0 ? 0 : _getOccupationTotals$3;
  const _getRatesPriceRange = (0, _helpers6.getRatesPriceRange)(rates, hotelCurrency),
    minRate = _getRatesPriceRange.minRate,
    maxRate = _getRatesPriceRange.maxRate;
  const _dateDiff = (0, _locale.dateDiff)(...calendar),
    nights = _dateDiff.days;
  const _getFeatures = (0, _helpers3.getFeatures)(features, urlParams),
    _getFeatures$showPric = _getFeatures.showPricePerNight,
    showPricePerNight = _getFeatures$showPric === void 0 ? false : _getFeatures$showPric;
  const has = {
    hotelAvailability: () => rates,
    chainAvailability: () => (hotelInfoBAR === null || hotelInfoBAR === void 0 ? void 0 : hotelInfoBAR.length) > 0 && hotelInfoBAR[0].bestAvailableRate,
    availability: () => has.hotelAvailability() || has.chainAvailability()
  };
  const step = hotels && !calendarSuggestion && searchCategory ? 'CHAINRESULT' : 'BOOKINGSTEP1';
  const common = _objectSpread(_objectSpread(_objectSpread({}, !has.availability() ? {
    availability: 'no'
  } : undefined), {}, {
    chainId: idchain,
    checkin: (0, _locale.dateFormat)(calendar[0], {
      format: 'DD/MM/YYYY'
    }),
    checkout: (0, _locale.dateFormat)(calendar[1], {
      format: 'DD/MM/YYYY'
    }),
    coupon: promocode,
    device: mobile ? 'MOBILE' : 'DESKTOP_TABLET',
    home: document.location.hostname,
    hotelId,
    idOffer: idoffers ? parseInt(idoffers) : undefined,
    idRoom: roomTypeId ? parseInt(roomTypeId) : undefined,
    language,
    lodgingDenomination: ACCOMMODATION_TYPE_NAME[((_variants$hotelId = variants[hotelId]) === null || _variants$hotelId === void 0 ? void 0 : _variants$hotelId.accommodationType) || accommodationType],
    multiroom: occupation.length > 1,
    numAdults: adults,
    numChildren: babies + children,
    numRooms: occupation.length,
    pricePerNight: showPricePerNight,
    propertyName,
    propertyType
  }, !rates && hotelInfoBAR ? {
    searchCategory
  } : undefined), {}, {
    step,
    userCountry: country,
    userLogged: !!session
  });
  const dataLayer = has.hotelAvailability() ? _objectSpread(_objectSpread({}, common), {}, {
    currency: hotelCurrency.code,
    exchangeRate: userCurrencyValue && hotelCurrency.value && userCurrencyValue / hotelCurrency.value,
    highestRate: (0, _helpers.parseRate)(maxRate, hotelCurrency, nights),
    localTaxesIncluded,
    lowestRate: (0, _helpers.parseRate)(minRate, hotelCurrency, nights),
    nights,
    ratesByRoom: Object.values(rates).map(function () {
      let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        id = _ref2.id,
        name = _ref2.name,
        _ref2$occupationOptio = _ref2.occupationOptions,
        occupationOptions = _ref2$occupationOptio === void 0 ? {} : _ref2$occupationOptio;
      return {
        id,
        name,
        occupations: Object.values(occupationOptions).map(function () {
          let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
            _ref3$numAdults = _ref3.numAdults,
            numAdults = _ref3$numAdults === void 0 ? 0 : _ref3$numAdults,
            _ref3$numFirstRangeCh = _ref3.numFirstRangeChildren,
            numFirstRangeChildren = _ref3$numFirstRangeCh === void 0 ? 0 : _ref3$numFirstRangeCh,
            _ref3$numSecondRangeC = _ref3.numSecondRangeChildren,
            numSecondRangeChildren = _ref3$numSecondRangeC === void 0 ? 0 : _ref3$numSecondRangeC,
            id = _ref3.id,
            _ref3$rates = _ref3.rates,
            rates = _ref3$rates === void 0 ? {} : _ref3$rates;
          return {
            adults: numAdults,
            children: numFirstRangeChildren + numSecondRangeChildren,
            id,
            rates: Object.values(rates).map(function () {
              let _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
                _ref4$boardOptions = _ref4.boardOptions,
                boardOptions = _ref4$boardOptions === void 0 ? {} : _ref4$boardOptions,
                id = _ref4.id,
                name = _ref4.name;
              return {
                boards: Object.values(boardOptions).map(function () {
                  let _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
                    id = _ref5.id,
                    name = _ref5.name,
                    totalBasePrice = _ref5.totalBasePrice,
                    _ref5$totalLocalTaxes = _ref5.totalLocalTaxes,
                    totalLocalTaxes = _ref5$totalLocalTaxes === void 0 ? 0 : _ref5$totalLocalTaxes,
                    totalPriceWithTaxes = _ref5.totalPriceWithTaxes;
                  return {
                    id,
                    localTaxes: (0, _legacy.formatPrice)(totalLocalTaxes, hotelCurrency),
                    name,
                    netPrice: (0, _legacy.formatPrice)(totalBasePrice, hotelCurrency),
                    taxes: (0, _legacy.formatPrice)(totalPriceWithTaxes - totalBasePrice, hotelCurrency)
                  };
                }),
                id,
                name
              };
            })
          };
        })
      };
    }),
    taxesIncluded,
    userCurrency: currency,
    userLocation
  }) : has.chainAvailability() ? _objectSpread(_objectSpread({}, common), {}, {
    availability: 'yes',
    exchangeRate: userCurrencyValue && hotelInfoBAR[0].currency.value && userCurrencyValue / hotelInfoBAR[0].currency.value,
    hotelIds: hotelInfoBAR.map(function () {
      let _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        idTokenProvider = _ref6.idTokenProvider;
      return idTokenProvider;
    }).join(','),
    hotelNames: hotelInfoBAR.map(function () {
      let _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        hotelName = _ref7.hotelName;
      return hotelName;
    }).join(','),
    lowestRate: (0, _legacy.formatPrice)(hotelInfoBAR[0].bestAvailableRate.totalPrice.internalWithTaxes / nights, hotelInfoBAR[0].currency),
    userLocation
  }) : _objectSpread(_objectSpread({}, common), {}, {
    cause: (hotelInfoBAR === null || hotelInfoBAR === void 0 ? void 0 : hotelInfoBAR.length) > 0 ? hotelInfoBAR[0].noAvailableReason : undefined
  });
  (0, _helpers5.dataLayerPush)((0, _helpers.getInitEvent)({
    mobile,
    hotel: {
      accommodationType,
      id: hotelId,
      variants
    },
    id,
    step,
    type
  }));
  (0, _helpers5.dataLayerPush)({
    event: has.availability() ? 'mirai.Availability' : 'mirai.noAvailability',
    'mirai.data': dataLayer
  });
  (0, _duetto.ratesResponse)(_objectSpread(_objectSpread({}, common), {}, {
    hotelCurrency,
    rates
  }));
  return dataLayer;
};
exports.ratesResponse = ratesResponse;
//# sourceMappingURL=ratesResponse.js.map