"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ratesAddToCart = void 0;
var _locale = require("@mirai/locale");
var _helpers = require("./helpers");
var _Core = require("../../../../Core.constants");
var _helpers2 = require("../helpers");
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 ratesAddToCart = function () {
  var _occupation$rates, _rate$boardOptions, _itemRates$find;
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    _ref$instances = _ref.instances,
    instances = _ref$instances === void 0 ? [] : _ref$instances,
    _ref$response = _ref.response,
    _ref$response2 = _ref$response === void 0 ? {} : _ref$response,
    _ref$response2$board = _ref$response2.board,
    _ref$response2$board2 = _ref$response2$board === void 0 ? {} : _ref$response2$board,
    id = _ref$response2$board2.id,
    name = _ref$response2$board2.name,
    rateId = _ref$response2$board2.rateId,
    roomId = _ref$response2$board2.roomId,
    roomName = _ref$response2$board2.roomName,
    _ref$response2$cart = _ref$response2.cart,
    cart = _ref$response2$cart === void 0 ? [] : _ref$response2$cart,
    _ref$response2$items = _ref$response2.items,
    items = _ref$response2$items === void 0 ? [] : _ref$response2$items,
    _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,
    currency = _ref$store2.currency,
    _ref$store2$finder = _ref$store2.finder,
    _ref$store2$finder2 = _ref$store2$finder === void 0 ? {} : _ref$store2$finder,
    _ref$store2$finder2$c = _ref$store2$finder2.calendar,
    calendar = _ref$store2$finder2$c === void 0 ? [] : _ref$store2$finder2$c,
    _ref$store2$finder2$p = _ref$store2$finder2.place,
    _ref$store2$finder2$p2 = _ref$store2$finder2$p === void 0 ? {} : _ref$store2$finder2$p,
    _ref$store2$finder2$p3 = _ref$store2$finder2$p2.id,
    _ref$store2$finder2$p4 = _ref$store2$finder2$p3 === void 0 ? [] : _ref$store2$finder2$p3,
    _ref$store2$finder2$p5 = _slicedToArray(_ref$store2$finder2$p4, 1),
    hotelId = _ref$store2$finder2$p5[0],
    _ref$store2$finder2$p6 = _ref$store2$finder2.promocode,
    promocode = _ref$store2$finder2$p6 === void 0 ? '' : _ref$store2$finder2$p6,
    instanceId = _ref$store2.id,
    type = _ref$store2.type;
  const hotelInfo = tracking.hotelInfo,
    _tracking$rates = tracking.rates,
    rates = _tracking$rates === void 0 ? [] : _tracking$rates;
  const room = rates.find(function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      id = _ref2.id;
    return id === roomId;
  });
  const occupation = (0, _helpers2.getFirstOccupationOption)({
    rates,
    roomId
  });
  const rate = occupation === null || occupation === void 0 || (_occupation$rates = occupation.rates) === null || _occupation$rates === void 0 ? void 0 : _occupation$rates[rateId];
  const _ref3 = items.find(function () {
      let item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return item.id === roomId;
    }) || {},
    _ref3$rates = _ref3.rates,
    itemRates = _ref3$rates === void 0 ? [] : _ref3$rates;
  const _ref4 = (rate === null || rate === void 0 || (_rate$boardOptions = rate.boardOptions) === null || _rate$boardOptions === void 0 ? void 0 : _rate$boardOptions[id]) || {},
    boardGroupName = _ref4.boardGroupName,
    totalBasePrice = _ref4.totalBasePrice;
  const _dateDiff = (0, _locale.dateDiff)(...calendar),
    days = _dateDiff.days;
  const value = {
    coupon: promocode,
    currency,
    items: [(0, _helpers.parseRate)({
      hotelId: type === _Core.TYPE.HOTEL ? instanceId : hotelId,
      hotelInfo,
      id,
      promocode,
      rate: {
        boardName: boardGroupName || name,
        rateName: (rate === null || rate === void 0 ? void 0 : rate.name) || ((_itemRates$find = itemRates.find(function () {
          let _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
            id = _ref5.id;
          return id === rateId;
        })) === null || _itemRates$find === void 0 ? void 0 : _itemRates$find.name),
        roomTypeName: (room === null || room === void 0 ? void 0 : room.name) || roomName,
        totalPriceWithoutTaxes: totalBasePrice / days
      }
    })],
    send_to: instances.map(_ref6 => {
      let idAnalytics = _ref6.idAnalytics;
      return idAnalytics;
    }),
    value: cart.reduce(function (value) {
      let _ref7 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
        _ref7$basePrice = _ref7.basePrice,
        basePrice = _ref7$basePrice === void 0 ? 0 : _ref7$basePrice;
      return value + basePrice;
    }, 0)
  };
  window.gtag('event', 'add_to_cart', value);
  return value;
};
exports.ratesAddToCart = ratesAddToCart;
//# sourceMappingURL=ratesAddToCart.js.map