"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 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 {
    availableBoards = [],
    availableRates = [],
    boards = [],
    cancellations = [],
    features: {
      columnMode,
      hideRadio = false,
      hideStandaloneBoard = false,
      showOffersFirst = false,
      showRateIncluded = false
    } = {},
    id: idItem,
    priceFactor = 1,
    promocodeForAll,
    rates = [],
    resultPosition,
    selectedBoard = {},
    uniqueOption = false,
    onBoard,
    onCancellation,
    onCancellationInfo,
    onRate,
    onOfferInfo
  } = _ref;
  const {
    isMobile
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      finder: {
        promocode
      } = {}
    } = {}
  } = (0, _dataSources.useStore)();
  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,
    rateId,
    id: boardId
  } = selectedBoard;
  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;
    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,
        id,
        name,
        caption,
        price
      } = _ref3,
      cancellation = _objectWithoutProperties(_ref3, _excluded);
    return /*#__PURE__*/_react.default.createElement(_components.Option, {
      key: id,
      caption: caption,
      checked: id === cancellationId,
      hasBasePrice: !!cancellations.find(function () {
        let {
          price
        } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        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(_ref4 => {
    let {
      id,
      cancellationId,
      countdown,
      description,
      disabled,
      included,
      name,
      price,
      restrictions
    } = _ref4;
    const rate = rates.find(rate => rate.id === id);
    const [board] = availableBoards.filter(function () {
      let {
        rateId
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return rateId === id;
    });
    const {
      confidential,
      deals = [],
      discount: {
        breakdown = []
      } = {}
    } = board || {};
    const isPromocode = promocode && !promocodeForAll && breakdown.find(function () {
      let {
        type
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return type === _Rates.DISCOUNT_TYPE.PROMOTION || deals.length > 0 && confidential;
    });
    const {
      percentage: promoPercentage = 0
    } = breakdown.find(function () {
      let {
        type
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return type === _Rates.DISCOUNT_TYPE.PROMOTION;
    }) || {};
    const {
      percentage: offerPercentage = 0
    } = breakdown.find(function () {
      let {
        type
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return type === _Rates.DISCOUNT_TYPE.DEAL;
    }) || {};
    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 {
            rateId
          } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          return rateId === id;
        })[0],
        onOfferInfo: () => {
          handleCardInteraction('offer');
          onOfferInfo(_objectSpread(_objectSpread({}, rate), {}, {
            availableBoards
          }));
        }
      }),
      checked: id === rateId,
      disabled: disabled,
      hasBasePrice: !!rates.find(function () {
        let {
          price
        } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        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,
      id,
      increment,
      name,
      variants = []
    } = board;
    return /*#__PURE__*/_react.default.createElement(_components.Option, {
      key: id,
      checked: id === boardId,
      disabled: disabled,
      hasBasePrice: !!boards.find(function () {
        let {
          increment
        } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        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 {
          name
        } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        return name;
      }),
      values: variants.map(function () {
        let {
          value
        } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        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