"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 _getFirstOccupationOp, _getFirstOccupationOp2, _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] : {};
  // ! -- TODO: use of tracking should be a temporary approach ---------------------------------------------------------
  const {
    hotelInfo,
    rates = []
  } = tracking;
  const {
    rates: itemRates = []
  } = items.find(function () {
    let item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return item.id === roomId;
  }) || {};
  const {
    totalBasePrice
  } = ((_getFirstOccupationOp = (0, _helpers2.getFirstOccupationOption)({
    rates,
    roomId
  })) === null || _getFirstOccupationOp === void 0 ? void 0 : (_getFirstOccupationOp2 = _getFirstOccupationOp.rates[rateId]) === null || _getFirstOccupationOp2 === void 0 ? void 0 : _getFirstOccupationOp2.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: name,
        rateName: (_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: 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