"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PriceDetails = 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 = _interopRequireWildcard(require("react"));
var _helpers = require("./helpers");
var style = _interopRequireWildcard(require("./PriceDetails.module.css"));
var _Core = require("../../../../../../Core.constants");
var _helpers2 = require("../../../../../helpers");
var _Rates = require("../../../../Rates.constants");
var _Item = require("../../Item.l10n");
const _excluded = ["board", "cart", "extras", "resultPosition", "taxesType"];
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 _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 _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 ALL_EXCLUDED = _Core.TAXES_TYPE.ALL_EXCLUDED,
  FEES_EXCLUDED = _Core.TAXES_TYPE.FEES_EXCLUDED,
  FEES_INCLUDED_TAXES_EXCLUDED = _Core.TAXES_TYPE.FEES_INCLUDED_TAXES_EXCLUDED,
  TAXES_EXCLUDED = _Core.TAXES_TYPE.TAXES_EXCLUDED,
  TAXES_INCLUDED_FEES_EXCLUDED = _Core.TAXES_TYPE.TAXES_INCLUDED_FEES_EXCLUDED;
const PriceDetails = _ref => {
  let _ref$board = _ref.board,
    board = _ref$board === void 0 ? {} : _ref$board,
    _ref$cart = _ref.cart,
    cart = _ref$cart === void 0 ? [] : _ref$cart,
    _ref$extras = _ref.extras,
    extras = _ref$extras === void 0 ? [] : _ref$extras,
    resultPosition = _ref.resultPosition,
    taxesType = _ref.taxesType,
    others = _objectWithoutProperties(_ref, _excluded);
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value2 = _useStore$value === void 0 ? {} : _useStore$value,
    currency = _useStore$value2.currency,
    _useStore$value2$find = _useStore$value2.finder,
    _useStore$value2$find2 = _useStore$value2$find === void 0 ? {} : _useStore$value2$find,
    promocode = _useStore$value2$find2.promocode;
  const _useLocale = (0, _locale.useLocale)(),
    currencyFormat = _useLocale.currencyFormat,
    translate = _useLocale.translate;
  const _useState = (0, _react.useState)(Date.now()),
    _useState2 = _slicedToArray(_useState, 2),
    timestamp = _useState2[0],
    setTimestamp = _useState2[1];
  (0, _react.useEffect)(() => {
    const callback = () => {
      setTimestamp(Date.now());
    };
    _dataSources.Event.subscribe(_helpers2.EVENT.OTA_PRICES_READY, callback);
    return () => {
      _dataSources.Event.unsubscribe(_helpers2.EVENT.OTA_PRICES_READY, callback);
    };
  }, []);
  const _ref2 = cart.length === 0 ? board : (0, _helpers.getPrices)(_objectSpread({
      cart,
      extras
    }, others)),
    _ref2$basePrice = _ref2.basePrice,
    basePrice = _ref2$basePrice === void 0 ? 0 : _ref2$basePrice,
    _ref2$discount = _ref2.discount,
    _ref2$discount2 = _ref2$discount === void 0 ? {} : _ref2$discount,
    _ref2$discount2$break = _ref2$discount2.breakdown,
    breakdown = _ref2$discount2$break === void 0 ? [] : _ref2$discount2$break,
    _ref2$fees = _ref2.fees,
    boardFees = _ref2$fees === void 0 ? 0 : _ref2$fees,
    _ref2$taxes = _ref2.taxes,
    boardTaxes = _ref2$taxes === void 0 ? 0 : _ref2$taxes,
    _ref2$totalBaseExtras = _ref2.totalBaseExtras,
    totalBaseExtras = _ref2$totalBaseExtras === void 0 ? 0 : _ref2$totalBaseExtras;
  const totalBreakdown = breakdown.reduce((total, _ref3) => {
    let value = _ref3.value;
    return total + value;
  }, 0);
  const total = basePrice + boardFees + boardTaxes + totalBaseExtras - totalBreakdown;
  const _ref4 = cart.length === 0 ? board : cart.reduce((total, current) => ({
      confidential: (current === null || current === void 0 ? void 0 : current.confidential) && (total === null || total === void 0 ? void 0 : total.confidential)
    }), {
      confidential: true
    }),
    confidential = _ref4.confidential;
  const renderSubtotal = () => (totalBreakdown > 0 || totalBaseExtras > 0) && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.anchor
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.item
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    bold: true
  }, translate(_Item.L10N.LABEL_SUBTOTAL)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    bold: true
  }, currencyFormat({
    currency,
    value: basePrice + totalBaseExtras - totalBreakdown + (taxesType === TAXES_INCLUDED_FEES_EXCLUDED ? boardTaxes : 0) + (taxesType === FEES_INCLUDED_TAXES_EXCLUDED ? boardFees : 0)
  }))));
  const renderTaxes = () => boardTaxes > 0 && /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.item
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, translate(_Item.L10N.LABEL_TAXES)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, currencyFormat({
    currency,
    value: boardTaxes
  })));
  const renderFees = () => boardFees > 0 && /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.item
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, translate(_Item.L10N.LABEL_FEES)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, currencyFormat({
    currency,
    value: boardFees
  })));
  const taxesTypes = {
    [ALL_EXCLUDED]: [renderSubtotal, renderTaxes, renderFees],
    [TAXES_EXCLUDED]: [renderSubtotal, renderTaxes, renderFees],
    [FEES_EXCLUDED]: [renderTaxes, renderSubtotal, renderFees],
    [TAXES_INCLUDED_FEES_EXCLUDED]: [renderTaxes, renderSubtotal, renderFees],
    [FEES_INCLUDED_TAXES_EXCLUDED]: [renderFees, renderSubtotal, renderTaxes]
  };
  return /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true
  }, /*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
    pressable: true,
    onPress: () => _dataSources.Event.publish(_helpers2.EVENT.METRICS, {
      id: _helpers2.METRICS_EVENTS.RATES_INTERACTION,
      action: 'card',
      element_clicked: 'price_details',
      result_position: resultPosition
    }),
    Template: () => /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.breakdown
    }, basePrice > 0 && /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: style.item
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true
    }, translate(_Item.L10N.LABEL_BASE_PRICE)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true
    }, currencyFormat({
      currency,
      value: basePrice
    }))), totalBaseExtras > 0 && /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: style.item
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true
    }, translate(_Item.L10N.LABEL_EXTRAS)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true
    }, currencyFormat({
      currency,
      value: totalBaseExtras
    }))), breakdown === null || breakdown === void 0 ? void 0 : breakdown.map(_ref5 => {
      let type = _ref5.type,
        value = _ref5.value;
      return /*#__PURE__*/_react.default.createElement(_ui.View, {
        row: true,
        key: type,
        className: style.item
      }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
        action: true
      }, translate(_Item.L10N.LABEL_DISCOUNT_TYPE[type]), promocode && (type === _Rates.DISCOUNT_TYPE.PROMOTION || type === _Rates.DISCOUNT_TYPE.DEAL && confidential) && " ".concat(promocode.toUpperCase())), /*#__PURE__*/_react.default.createElement(_ui.Text, {
        action: true
      }, currencyFormat({
        currency,
        value: -value
      })));
    }), (taxesTypes[taxesType] || [renderTaxes, renderFees]).map((render, index) => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
      key: index
    }, render())), /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.anchor
    }), /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: style.item
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true
    }, translate(_Item.L10N.LABEL_TOTAL)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true
    }, currencyFormat({
      currency,
      value: total
    })))),
    timestamp: timestamp,
    className: style.tooltip
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    accent: true,
    action: true,
    medium: true,
    className: others.className
  }, translate(_Item.L10N.LABEL_PRICE_DETAILS))));
};
exports.PriceDetails = PriceDetails;
PriceDetails.displayName = 'Mirai:Core:Rates:Item:PriceDetails';
PriceDetails.propTypes = {
  board: _propTypes.default.shape({
    discount: _propTypes.default.shape({
      breakdown: _propTypes.default.arrayOf(_propTypes.default.shape({
        type: _propTypes.default.number,
        value: _propTypes.default.number
      })),
      percentage: _propTypes.default.number
    }),
    originalPrice: _propTypes.default.number
  }),
  cart: _propTypes.default.arrayOf(_propTypes.default.shape({})),
  extras: _propTypes.default.arrayOf(_propTypes.default.shape({})),
  resultPosition: _propTypes.default.number,
  taxesType: _propTypes.default.number
};
//# sourceMappingURL=PriceDetails.js.map