"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Summary = 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 _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 = ["availableRates", "features", "id", "isSoldOut", "multiRoom", "name", "nights", "priceFactor", "promocodeForAll", "resultPosition", "selected", "selectedBoard", "taxesType", "onSelect"];
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 Summary = _ref => {
  let {
      availableRates = [],
      features: {
        showPricePerNight = false
      } = {},
      id: roomId,
      isSoldOut = false,
      multiRoom = false,
      name: roomName,
      nights = 2,
      priceFactor = 1,
      promocodeForAll,
      resultPosition,
      selected = {},
      selectedBoard = {},
      taxesType,
      onSelect
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isMobile
  } = (0, _ui.useDevice)();
  const {
    currencyFormat,
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      currency,
      finder: {
        promocode
      } = {}
    } = {}
  } = (0, _dataSources.useStore)();
  const [busy, setBusy] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    Object.keys(selected).length === 0 && setBusy(false);
  }, [selected]);
  const {
    basePrice,
    cancellationId,
    checkoutPrice,
    confidential,
    dayPrice,
    deals,
    discount,
    fees,
    id,
    name,
    originalPrice = 0,
    price = 0,
    rateId,
    taxes,
    totalPriceRaw
  } = selectedBoard;
  const isSelected = selected.id === id;
  const handlePress = () => {
    if (!multiRoom) setBusy(true);
    onSelect(!isSelected ? {
      basePrice,
      cancellationId,
      checkoutPrice,
      confidential,
      deals,
      discount,
      fees,
      id,
      name,
      price,
      rateId,
      roomId,
      roomName,
      taxes,
      totalPriceRaw
    } : undefined);
  };
  const {
    breakdown = [],
    percentage: discountPercentage = 0
  } = discount || {};
  const {
    percentage: clubPercentage = 0
  } = breakdown.find(function () {
    let {
      type
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return type === _Rates.DISCOUNT_TYPE.CLUB;
  }) || {};
  const {
    percentage: promoPercentage = 0
  } = breakdown.find(function () {
    let {
      type
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return type === _Rates.DISCOUNT_TYPE.PROMOTION;
  }) || {};
  const clubOffer = (0, _helpers.getClubRate)({
    board: selectedBoard,
    rate: availableRates.find(_ref2 => {
      let {
        id
      } = _ref2;
      return rateId === id;
    })
  });
  const currencyFormatProps = {
    currency,
    maximumFractionDigits: 0,
    minimumFractionDigits: 0
  };
  const {
    testId
  } = others;
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: [style.offset, style.summary]
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.tags
  }, clubOffer && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    small: true,
    className: [style.tag, style.highlight]
  }, translate(_Item.L10N.LABEL_ONLY_MEMBERS_DISCOUNT, {
    value: !isSoldOut && clubPercentage > 0 ? " -".concat(Math.round(clubPercentage), "%") : ''
  }).trimEnd()), !isSoldOut && promocode && promocodeForAll && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    small: true,
    className: [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), "%") : '')), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: [style.gap, style.content, isSoldOut && style.end]
  }, !isSoldOut && /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.price
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true,
    level: multiRoom || price >= 10000 ? 3 : 2
  }, currencyFormat(_objectSpread(_objectSpread({}, currencyFormatProps), {}, {
    value: price / priceFactor
  }))), price !== originalPrice && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    light: true,
    className: style.lineThrough
  }, currencyFormat(_objectSpread(_objectSpread({}, currencyFormatProps), {}, {
    value: originalPrice / priceFactor
  })))), nights > 1 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    small: true,
    className: style.pricePer
  }, "".concat(currencyFormat(_objectSpread(_objectSpread({}, currencyFormatProps), {}, {
    value: showPricePerNight ? price : dayPrice
  })), " ").concat(translate(showPricePerNight ? _Item.L10N.LABEL_FOR_NIGHTS : _Item.L10N.LABEL_PER_NIGHT, {
    nights
  }))), !!taxesType && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, translate((0, _helpers.getLabelTaxes)(taxesType))), (discountPercentage > 0 || fees > 0 || taxes > 0) && /*#__PURE__*/_react.default.createElement(_components.PriceDetails, {
    board: selectedBoard,
    taxesType,
    resultPosition,
    className: style.priceDetails
  })), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    busy: busy,
    disabled: isSoldOut,
    large: isMobile,
    secondary: multiRoom && isSelected,
    onPress: handlePress,
    testId: testId ? "".concat(testId, "-select") : undefined
  }, translate(isSoldOut ? _Item.L10N.LABEL_NOT_AVAILABLE : multiRoom ? isSelected ? _Item.L10N.LABEL_DESELECT : _Item.L10N.LABEL_SELECT : _Item.L10N.ACTION_RESERVE)))));
};
exports.Summary = Summary;
Summary.displayName = 'Mirai:Core:Rates:Item:Summary';
Summary.propTypes = {
  availableRates: _propTypes.default.arrayOf(_propTypes.default.any),
  features: _propTypes.default.shape({
    showPricePerNight: _propTypes.default.bool
  }),
  id: _propTypes.default.number,
  isSoldOut: _propTypes.default.bool,
  multiRoom: _propTypes.default.bool,
  name: _propTypes.default.string,
  nights: _propTypes.default.number,
  priceFactor: _propTypes.default.number,
  promocodeForAll: _propTypes.default.bool,
  resultPosition: _propTypes.default.number,
  selected: _propTypes.default.shape({}),
  selectedBoard: _propTypes.default.shape({}),
  taxesType: _propTypes.default.number,
  onSelect: _propTypes.default.func
};
//# sourceMappingURL=Item.Summary.js.map