"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");
const ratesAddToCart = function () {
  var _occupation$rates, _rate$boardOptions, _itemRates$find;
  let {
    instances = [],
    response: {
      board: {
        id,
        name,
        rateId,
        roomId,
        roomName
      } = {},
      cart = [],
      items = [],
      tracking = {}
    } = {},
    store: {
      currency,
      finder: {
        calendar = [],
        place: {
          id: [hotelId] = []
        } = {},
        promocode = ''
      } = {},
      id: instanceId,
      type
    } = {}
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  const {
    hotelInfo,
    rates = []
  } = tracking;
  const room = rates.find(function () {
    let {
      id
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return id === roomId;
  });
  const occupation = (0, _helpers2.getFirstOccupationOption)({
    rates,
    roomId
  });
  const rate = occupation === null || occupation === void 0 ? void 0 : (_occupation$rates = occupation.rates) === null || _occupation$rates === void 0 ? void 0 : _occupation$rates[rateId];
  const {
    rates: itemRates = []
  } = items.find(function () {
    let item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return item.id === roomId;
  }) || {};
  const {
    boardGroupName,
    totalBasePrice
  } = (rate === null || rate === void 0 ? void 0 : (_rate$boardOptions = rate.boardOptions) === null || _rate$boardOptions === void 0 ? void 0 : _rate$boardOptions[id]) || {};
  const {
    days
  } = (0, _locale.dateDiff)(...calendar);
  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 {
            id
          } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          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(_ref => {
      let {
        idAnalytics
      } = _ref;
      return idAnalytics;
    }),
    value: cart.reduce(function (value) {
      let {
        basePrice = 0
      } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      return value + basePrice;
    }, 0)
  };
  window.gtag('event', 'add_to_cart', value);
  return value;
};
exports.ratesAddToCart = ratesAddToCart;
//# sourceMappingURL=ratesAddToCart.js.map