"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.parseItems = void 0;
var _locale = require("@mirai/locale");
var _parseRate = require("./parseRate");
var _Core = require("../../../../../Core.constants");
var _legacy = require("../../helpers/legacy");
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
const parseItems = function () {
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    _ref$finder = _ref.finder,
    _ref$finder2 = _ref$finder === void 0 ? {} : _ref$finder,
    _ref$finder2$calendar = _ref$finder2.calendar,
    calendar = _ref$finder2$calendar === void 0 ? [] : _ref$finder2$calendar,
    _ref$finder2$place = _ref$finder2.place,
    _ref$finder2$place2 = _ref$finder2$place === void 0 ? {} : _ref$finder2$place,
    _ref$finder2$place2$i = _ref$finder2$place2.id,
    _ref$finder2$place2$i2 = _ref$finder2$place2$i === void 0 ? [] : _ref$finder2$place2$i,
    _ref$finder2$place2$i3 = _slicedToArray(_ref$finder2$place2$i2, 1),
    hotelId = _ref$finder2$place2$i3[0],
    promocode = _ref$finder2.promocode,
    _ref$hotelInfo = _ref.hotelInfo,
    hotelInfo = _ref$hotelInfo === void 0 ? {} : _ref$hotelInfo,
    instanceId = _ref.id,
    _ref$reservedExtras = _ref.reservedExtras,
    reservedExtras = _ref$reservedExtras === void 0 ? {} : _ref$reservedExtras,
    _ref$reservedRooms = _ref.reservedRooms,
    reservedRooms = _ref$reservedRooms === void 0 ? [] : _ref$reservedRooms,
    _ref$tracking = _ref.tracking,
    _ref$tracking2 = _ref$tracking === void 0 ? {} : _ref$tracking,
    _ref$tracking2$bookin = _ref$tracking2.bookingValues,
    _ref$tracking2$bookin2 = _ref$tracking2$bookin === void 0 ? {} : _ref$tracking2$bookin,
    checkin = _ref$tracking2$bookin2.checkin,
    checkout = _ref$tracking2$bookin2.checkout,
    coupon = _ref$tracking2$bookin2.coupon,
    type = _ref.type;
  const hotelCurrency = hotelInfo.currency,
    propertyName = hotelInfo.propertyName;
  const _dateDiff = (0, _locale.dateDiff)(...(calendar.length ? calendar : [(0, _locale.parseDate)(checkin), (0, _locale.parseDate)(checkout)])),
    days = _dateDiff.days;
  const rooms = reservedRooms.map(function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      totalPriceWithoutTaxes = _ref2.averagePriceWithoutTaxes,
      boardName = _ref2.defaultBoardDescription,
      _ref2$numRoomsToReser = _ref2.numRoomsToReserve,
      numRoomsToReserve = _ref2$numRoomsToReser === void 0 ? 1 : _ref2$numRoomsToReser,
      id = _ref2.rateId,
      rateName = _ref2.rateName,
      roomTypeName = _ref2.roomTypeName;
    return (0, _parseRate.parseRate)({
      hotelId: type === _Core.TYPE.HOTEL ? instanceId : hotelId,
      hotelInfo,
      id,
      promocode: promocode || coupon || '',
      rate: {
        boardName,
        rateName,
        roomTypeName,
        totalPriceWithoutTaxes
      },
      quantity: numRoomsToReserve * days
    });
  });
  const extras = Object.keys(reservedExtras).map(key => reservedExtras[key]).map(_ref3 => {
    let id = _ref3.id,
      name = _ref3.name,
      multiplierPrice = _ref3.multiplierPrice,
      priceWithoutTaxes = _ref3.priceWithoutTaxes,
      type = _ref3.type;
    return {
      coupon: promocode || coupon || '',
      currency: hotelCurrency.code,
      item_brand: propertyName,
      item_category: 'Extra',
      item_category2: type,
      item_name: name,
      price: (0, _legacy.formatPrice)(priceWithoutTaxes, hotelCurrency),
      quantity: multiplierPrice,
      item_id: "".concat(id)
    };
  });
  return [...rooms, ...extras];
};
exports.parseItems = parseItems;
//# sourceMappingURL=parseItems.js.map