"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 _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,
  FEES_EXCLUDED,
  FEES_INCLUDED_TAXES_EXCLUDED,
  TAXES_EXCLUDED,
  TAXES_INCLUDED_FEES_EXCLUDED
} = _Core.TAXES_TYPE;
const PriceDetails = _ref => {
  let {
      board = {},
      cart = [],
      extras = [],
      resultPosition,
      taxesType
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    value: {
      currency,
      finder: {
        promocode
      } = {}
    } = {}
  } = (0, _dataSources.useStore)();
  const {
    currencyFormat,
    translate
  } = (0, _locale.useLocale)();
  const [timestamp, setTimestamp] = (0, _react.useState)(Date.now());
  (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 {
    basePrice = 0,
    discount: {
      breakdown = []
    } = {},
    fees: boardFees = 0,
    taxes: boardTaxes = 0,
    totalBaseExtras = 0
  } = cart.length === 0 ? board : (0, _helpers.getPrices)(_objectSpread({
    cart,
    extras
  }, others));
  const totalBreakdown = breakdown.reduce((total, _ref2) => {
    let {
      value
    } = _ref2;
    return total + value;
  }, 0);
  const total = basePrice + boardFees + boardTaxes + totalBaseExtras - totalBreakdown;
  const {
    confidential
  } = 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
  });
  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(_ref3 => {
      let {
        type,
        value
      } = _ref3;
      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