"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getCheckoutParams = void 0;
var _locale = require("@mirai/locale");
const _excluded = ["cart", "clubDiscount", "currency", "dataSource", "extras", "session", "urlParams"],
  _excluded2 = ["rates"];
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] : {},
    {
      cart = [],
      clubDiscount,
      currency,
      dataSource: {
        currency: hotelCurrency
      } = {},
      extras = [],
      session,
      // eslint-disable-next-line no-unused-vars
      urlParams: {
        rates
      } = {} // !TODO: find best solution for discard rates url param
    } = _ref,
    urlParams = _objectWithoutProperties(_ref.urlParams, _excluded2),
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    deals = [],
    hotelverse,
    items,
    totalPrice
  } = cart.reduce(function (result) {
    let {
      id,
      checkoutPrice = 0,
      deals = [],
      idHotelverse,
      roomNumber,
      subjectToAvailability
    } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    const idRR = id.replace(/--/g, '|');
    const pushed = result.deals.find(item => item.idRR === idRR);
    if (pushed) return result;
    const {
      length: amount
    } = cart.filter(item => item.id === id);
    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
  });
  const totalExtras = +(extras.filter(function () {
    let {
      included
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return !included;
  }).reduce(function (total) {
    let {
      value = 0
    } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    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(_ref2 => {
      let {
        id: idExtra,
        included = false,
        amount: numSelected = 0,
        nights: nightsToApply = 0
      } = _ref2;
      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