"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getCheckoutParams = void 0;
var _locale = require("@mirai/locale");
const _excluded = ["rates"],
  _excluded2 = ["cart", "clubDiscount", "currency", "dataSource", "extras", "session", "urlParams"];
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); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
const getCheckoutParams = function () {
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    _ref$cart = _ref.cart,
    cart = _ref$cart === void 0 ? [] : _ref$cart,
    clubDiscount = _ref.clubDiscount,
    currency = _ref.currency,
    _ref$dataSource = _ref.dataSource,
    _ref$dataSource2 = _ref$dataSource === void 0 ? {} : _ref$dataSource,
    hotelCurrency = _ref$dataSource2.currency,
    _ref$extras = _ref.extras,
    extras = _ref$extras === void 0 ? [] : _ref$extras,
    session = _ref.session,
    _ref$urlParams = _ref.urlParams,
    _ref$urlParams2 = _ref$urlParams === void 0 ? {} : _ref$urlParams,
    rates = _ref$urlParams2.rates,
    urlParams = _objectWithoutProperties(_ref$urlParams2, _excluded),
    others = _objectWithoutProperties(_ref, _excluded2);
  const _cart$reduce = cart.reduce(function (result) {
      let _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
        id = _ref2.id,
        _ref2$checkoutPrice = _ref2.checkoutPrice,
        checkoutPrice = _ref2$checkoutPrice === void 0 ? 0 : _ref2$checkoutPrice,
        _ref2$deals = _ref2.deals,
        deals = _ref2$deals === void 0 ? [] : _ref2$deals,
        idHotelverse = _ref2.idHotelverse,
        roomNumber = _ref2.roomNumber,
        subjectToAvailability = _ref2.subjectToAvailability;
      const idRR = id.replace(/--/g, '|');
      const pushed = result.deals.find(item => item.idRR === idRR);
      if (pushed) return result;
      const _cart$filter = cart.filter(item => item.id === id),
        amount = _cart$filter.length;
      result.deals.push({
        idRR,
        offersApplied: deals.join()
      });
      roomNumber && result.hotelverse.push({
        idRR,
        idHotelverse,
        roomNumber: [roomNumber],
        subjectToAvailability
      });
      result.items += "".concat(result.items ? ',' : '').concat(idRR, ":").concat(amount);
      result.totalPrice += +(amount * checkoutPrice * (_locale.CURRENCIES_WITHOUT_DECIMALS.includes(hotelCurrency) ? 1 : 100)).toFixed();
      return result;
    }, {
      deals: [],
      hotelverse: [],
      items: '',
      totalPrice: 0
    }),
    _cart$reduce$deals = _cart$reduce.deals,
    deals = _cart$reduce$deals === void 0 ? [] : _cart$reduce$deals,
    hotelverse = _cart$reduce.hotelverse,
    items = _cart$reduce.items,
    totalPrice = _cart$reduce.totalPrice;
  const totalExtras = +(extras.filter(function () {
    let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      included = _ref3.included;
    return !included;
  }).reduce(function (total) {
    let _ref4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
      _ref4$value = _ref4.value,
      value = _ref4$value === void 0 ? 0 : _ref4$value;
    return total + value;
  }, 0) * (_locale.CURRENCIES_WITHOUT_DECIMALS.includes(hotelCurrency) ? 1 : 100)).toFixed();
  return _objectSpread(_objectSpread({}, urlParams), {}, {
    applyClubDiscount: !!session || clubDiscount,
    currency,
    extras: JSON.stringify(extras.map(_ref5 => {
      let idExtra = _ref5.id,
        _ref5$included = _ref5.included,
        included = _ref5$included === void 0 ? false : _ref5$included,
        _ref5$amount = _ref5.amount,
        numSelected = _ref5$amount === void 0 ? 0 : _ref5$amount,
        _ref5$nights = _ref5.nights,
        nightsToApply = _ref5$nights === void 0 ? 0 : _ref5$nights;
      return {
        idExtra,
        included,
        nightsToApply,
        numSelected
      };
    })),
    offersAppliedBySelectedRoom: JSON.stringify(deals),
    roomNumberBySelectedRoom: JSON.stringify(hotelverse),
    rooms: items,
    totalPrice: totalPrice + totalExtras
  }, others);
};
exports.getCheckoutParams = getCheckoutParams;
//# sourceMappingURL=getCheckoutParams.js.map