"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Rates = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireDefault(require("react"));
var _components = require("./components");
var _Item = require("./Item.l10n");
var style = _interopRequireWildcard(require("./Item.module.css"));
var _helpers = require("../../../helpers");
var _Rates = require("../../Rates.constants");
const _excluded = ["highlight", "id", "name", "caption", "price"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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; }
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 Rates = _ref => {
  let _ref$availableBoards = _ref.availableBoards,
    availableBoards = _ref$availableBoards === void 0 ? [] : _ref$availableBoards,
    _ref$availableRates = _ref.availableRates,
    availableRates = _ref$availableRates === void 0 ? [] : _ref$availableRates,
    _ref$boards = _ref.boards,
    boards = _ref$boards === void 0 ? [] : _ref$boards,
    _ref$cancellations = _ref.cancellations,
    cancellations = _ref$cancellations === void 0 ? [] : _ref$cancellations,
    _ref$features = _ref.features,
    _ref$features2 = _ref$features === void 0 ? {} : _ref$features,
    columnMode = _ref$features2.columnMode,
    _ref$features2$hideRa = _ref$features2.hideRadio,
    hideRadio = _ref$features2$hideRa === void 0 ? false : _ref$features2$hideRa,
    _ref$features2$hideSt = _ref$features2.hideStandaloneBoard,
    hideStandaloneBoard = _ref$features2$hideSt === void 0 ? false : _ref$features2$hideSt,
    _ref$features2$showOf = _ref$features2.showOffersFirst,
    showOffersFirst = _ref$features2$showOf === void 0 ? false : _ref$features2$showOf,
    _ref$features2$showRa = _ref$features2.showRateIncluded,
    showRateIncluded = _ref$features2$showRa === void 0 ? false : _ref$features2$showRa,
    idItem = _ref.id,
    _ref$priceFactor = _ref.priceFactor,
    priceFactor = _ref$priceFactor === void 0 ? 1 : _ref$priceFactor,
    promocodeForAll = _ref.promocodeForAll,
    _ref$rates = _ref.rates,
    rates = _ref$rates === void 0 ? [] : _ref$rates,
    resultPosition = _ref.resultPosition,
    _ref$selectedBoard = _ref.selectedBoard,
    selectedBoard = _ref$selectedBoard === void 0 ? {} : _ref$selectedBoard,
    _ref$uniqueOption = _ref.uniqueOption,
    uniqueOption = _ref$uniqueOption === void 0 ? false : _ref$uniqueOption,
    onBoard = _ref.onBoard,
    onCancellation = _ref.onCancellation,
    onCancellationInfo = _ref.onCancellationInfo,
    onRate = _ref.onRate,
    onOfferInfo = _ref.onOfferInfo;
  const _useDevice = (0, _ui.useDevice)(),
    isMobile = _useDevice.isMobile;
  const _useLocale = (0, _locale.useLocale)(),
    translate = _useLocale.translate;
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value2 = _useStore$value === void 0 ? {} : _useStore$value,
    _useStore$value2$find = _useStore$value2.finder,
    _useStore$value2$find2 = _useStore$value2$find === void 0 ? {} : _useStore$value2$find,
    promocode = _useStore$value2$find2.promocode;
  const handleCardInteraction = elementClicked => {
    _dataSources.Event.publish(_helpers.EVENT.METRICS, {
      id: _helpers.METRICS_EVENTS.RATES_INTERACTION,
      action: 'card',
      element_clicked: elementClicked,
      result_position: resultPosition
    });
  };
  const cancellationId = selectedBoard.cancellationId,
    rateId = selectedBoard.rateId,
    boardId = selectedBoard.id;
  const has = {
    boards: boards.length > (hideStandaloneBoard ? 1 : 0),
    cancellations: cancellations.length > 0,
    inlineBoards: hideRadio && boards.length > 1 && boards.length <= (columnMode ? 3 : 2),
    inlineCancellations: hideRadio && cancellations.length > 1 && cancellations.length <= (columnMode ? 3 : 2),
    rates: availableRates.length > 0
  };
  const cancellationsFiltered = cancellations.filter(_ref2 => {
    let id = _ref2.id;
    return !showOffersFirst || showOffersFirst && (id === cancellationId || !has.rates);
  });
  const renderCancellations = () => has.cancellations && /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: [style.offset, style.gap]
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_Item.L10N.LABEL_CANCELLATION_POLICY)), /*#__PURE__*/_react.default.createElement(has.inlineCancellations ? _ui.View : _react.default.Fragment, has.inlineCancellations ? {
    row: true,
    className: style.inlineOptions
  } : undefined, cancellationsFiltered.map(_ref3 => {
    let highlight = _ref3.highlight,
      id = _ref3.id,
      name = _ref3.name,
      caption = _ref3.caption,
      price = _ref3.price,
      cancellation = _objectWithoutProperties(_ref3, _excluded);
    return /*#__PURE__*/_react.default.createElement(_components.Option, {
      key: id,
      caption: caption,
      checked: id === cancellationId,
      hasBasePrice: !!cancellations.find(function () {
        let _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          price = _ref4.price;
        return !price;
      }),
      id: id,
      inline: has.inlineCancellations,
      name: "Cancellation ".concat(idItem),
      outlined: hideRadio,
      value: !showOffersFirst ? price / priceFactor : undefined,
      onPress: !uniqueOption ? () => onCancellation(id) : undefined
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true,
      medium: highlight,
      className: style.optionText
    }, highlight ? /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true,
      medium: highlight,
      className: (0, _ui.styles)(highlight && style.tag)
    }, name) : name, /*#__PURE__*/_react.default.createElement(_ui.Pressable, {
      onPress: () => {
        handleCardInteraction('cancellation_policy');
        onCancellationInfo(_objectSpread(_objectSpread({}, selectedBoard), {}, {
          cancellation
        }));
      }
    }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
      paragraph: true,
      value: _helpers.ICON.INFO
    }))));
  })));
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, !showOffersFirst && renderCancellations(), has.rates && /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: [style.offset, style.gap]
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_Item.L10N.LABEL_OFFER)), rates.map(_ref5 => {
    let id = _ref5.id,
      cancellationId = _ref5.cancellationId,
      countdown = _ref5.countdown,
      description = _ref5.description,
      disabled = _ref5.disabled,
      included = _ref5.included,
      name = _ref5.name,
      price = _ref5.price,
      restrictions = _ref5.restrictions;
    const rate = rates.find(rate => rate.id === id);
    const _availableBoards$filt = availableBoards.filter(function () {
        let _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          rateId = _ref6.rateId;
        return rateId === id;
      }),
      _availableBoards$filt2 = _slicedToArray(_availableBoards$filt, 1),
      board = _availableBoards$filt2[0];
    const _ref7 = board || {},
      confidential = _ref7.confidential,
      _ref7$deals = _ref7.deals,
      deals = _ref7$deals === void 0 ? [] : _ref7$deals,
      _ref7$discount = _ref7.discount,
      _ref7$discount2 = _ref7$discount === void 0 ? {} : _ref7$discount,
      _ref7$discount2$break = _ref7$discount2.breakdown,
      breakdown = _ref7$discount2$break === void 0 ? [] : _ref7$discount2$break;
    const isPromocode = promocode && !promocodeForAll && breakdown.find(function () {
      let _ref8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        type = _ref8.type;
      return type === _Rates.DISCOUNT_TYPE.PROMOTION || deals.length > 0 && confidential;
    });
    const _ref9 = breakdown.find(function () {
        let _ref0 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          type = _ref0.type;
        return type === _Rates.DISCOUNT_TYPE.PROMOTION;
      }) || {},
      _ref9$percentage = _ref9.percentage,
      promoPercentage = _ref9$percentage === void 0 ? 0 : _ref9$percentage;
    const _ref1 = breakdown.find(function () {
        let _ref10 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          type = _ref10.type;
        return type === _Rates.DISCOUNT_TYPE.DEAL;
      }) || {},
      _ref1$percentage = _ref1.percentage,
      offerPercentage = _ref1$percentage === void 0 ? 0 : _ref1$percentage;
    return /*#__PURE__*/_react.default.createElement(_components.Option, {
      key: id,
      caption: /*#__PURE__*/_react.default.createElement(_components.RateDetails, {
        countdown,
        included: showRateIncluded ? included : undefined,
        restrictions,
        selectedBoard: selectedBoard.rateId === id ? selectedBoard : availableBoards.filter(function () {
          let _ref11 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
            rateId = _ref11.rateId;
          return rateId === id;
        })[0],
        onOfferInfo: () => {
          handleCardInteraction('offer');
          onOfferInfo(_objectSpread(_objectSpread({}, rate), {}, {
            availableBoards
          }));
        }
      }),
      checked: id === rateId,
      disabled: disabled,
      hasBasePrice: !!rates.find(function () {
        let _ref12 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          price = _ref12.price;
        return !price;
      }),
      name: "Rate ".concat(idItem),
      outlined: hideRadio,
      value: price / priceFactor,
      onPress: !uniqueOption ? () => onRate({
        id,
        cancellationId
      }) : undefined
    }, rates.length > 0 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
      medium: hideRadio,
      action: true,
      className: style.optionText
    }, /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: style.tags
    }, offerPercentage > 0 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true,
      medium: true,
      className: style.tag
    }, translate(_Item.L10N.LABEL_DISCOUNT, {
      value: "".concat(Math.round(offerPercentage), "%")
    }))), name, (description || included.length > 0) && /*#__PURE__*/_react.default.createElement(_ui.Pressable, {
      onPress: () => {
        handleCardInteraction('offer');
        onOfferInfo(_objectSpread(_objectSpread({}, rate), {}, {
          availableBoards
        }));
      }
    }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
      paragraph: true,
      value: _helpers.ICON.INFO
    })), isPromocode && /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      small: true,
      className: (0, _ui.styles)(style.tag, style.highlight)
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      small: true,
      className: promocode.length > (isMobile ? _Rates.MAX_PROMOCODE.MOBILE : _Rates.MAX_PROMOCODE.DESKTOP) && style.truncate
    }, promocode.toUpperCase()), promoPercentage ? " -".concat(Math.round(promoPercentage), "%") : '')));
  })), showOffersFirst && renderCancellations(), has.boards && /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: (0, _ui.styles)(style.offset, style.gap)
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_Item.L10N.LABEL_ACCOMMODATION_PLAN)), /*#__PURE__*/_react.default.createElement(has.inlineBoards ? _ui.View : _react.default.Fragment, has.inlineBoards ? {
    row: true,
    className: style.inlineOptions
  } : undefined, boards.map(function () {
    let board = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    const disabled = board.disabled,
      id = board.id,
      increment = board.increment,
      name = board.name,
      _board$variants = board.variants,
      variants = _board$variants === void 0 ? [] : _board$variants;
    return /*#__PURE__*/_react.default.createElement(_components.Option, {
      key: id,
      checked: id === boardId,
      disabled: disabled,
      hasBasePrice: !!boards.find(function () {
        let _ref13 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          increment = _ref13.increment;
        return !increment;
      }),
      inline: has.inlineBoards,
      name: "Board ".concat(idItem),
      outlined: hideRadio,
      title: name,
      value: has.boards ? increment / priceFactor : undefined,
      onPress: !uniqueOption ? () => {
        handleCardInteraction('accommodation_plan');
        onBoard(board);
      } : undefined
    }, variants.length > 0 && /*#__PURE__*/_react.default.createElement(_components.TooltipDates, {
      captions: variants.map(function () {
        let _ref14 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          name = _ref14.name;
        return name;
      }),
      values: variants.map(function () {
        let _ref15 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          value = _ref15.value;
        return (0, _locale.parseDate)(value);
      })
    }));
  }))), /*#__PURE__*/_react.default.createElement(_ui.View, null));
};
exports.Rates = Rates;
Rates.propTypes = {
  availableBoards: _propTypes.default.arrayOf(_propTypes.default.any),
  availableRates: _propTypes.default.arrayOf(_propTypes.default.any),
  boards: _propTypes.default.arrayOf(_propTypes.default.any),
  cancellations: _propTypes.default.arrayOf(_propTypes.default.any),
  features: _propTypes.default.shape({
    columnMode: _propTypes.default.bool,
    hideStandaloneBoard: _propTypes.default.bool,
    showOffersFirst: _propTypes.default.bool,
    showRateIncluded: _propTypes.default.bool
  }),
  id: _propTypes.default.number,
  priceFactor: _propTypes.default.number,
  promocodeForAll: _propTypes.default.bool,
  rates: _propTypes.default.arrayOf(_propTypes.default.any),
  resultPosition: _propTypes.default.number,
  selectedBoard: _propTypes.default.shape({}),
  uniqueOption: _propTypes.default.bool,
  onBoard: _propTypes.default.func,
  onCancellation: _propTypes.default.func,
  onCancellationInfo: _propTypes.default.func,
  onChange: _propTypes.default.func,
  onOfferInfo: _propTypes.default.func,
  onRate: _propTypes.default.func
};
Rates.displayName = 'Mirai:Core:Rates:Item:Rates';
//# sourceMappingURL=Item.Rates.js.map