"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CardHotel = 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 _CardHotel = require("./CardHotel.l10n");
var style = _interopRequireWildcard(require("./CardHotel.module.css"));
var _helpers = require("./helpers");
var _helpers2 = require("../../../../helpers");
var _shared__ = require("../../../__shared__");
var _helpers3 = require("../../../helpers");
var _helpers4 = require("../../helpers");
var _Rates = require("../../Rates.constants");
var _components = require("../Item/components");
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; }
const DEFAULT_WIDTH = 374;
const CardHotel = function () {
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    _ref$board = _ref.board,
    board = _ref$board === void 0 ? {} : _ref$board,
    cardImageHeight = _ref.cardImageHeight,
    _ref$features = _ref.features,
    _ref$features2 = _ref$features === void 0 ? {} : _ref$features,
    distance = _ref$features2.distance,
    location = _ref$features2.location,
    url = _ref$features2.url,
    _ref$hasDiscountClub = _ref.hasDiscountClub,
    hasDiscountClub = _ref$hasDiscountClub === void 0 ? false : _ref$hasDiscountClub,
    id = _ref.id,
    _ref$images = _ref.images,
    images = _ref$images === void 0 ? [] : _ref$images,
    name = _ref.name,
    _ref$nights = _ref.nights,
    nights = _ref$nights === void 0 ? 1 : _ref$nights,
    propPrice = _ref.price,
    _ref$priceFactor = _ref.priceFactor,
    priceFactor = _ref$priceFactor === void 0 ? 1 : _ref$priceFactor,
    stars = _ref.stars,
    taxes = _ref.taxes,
    unavailability = _ref.unavailability;
  const _useDevice = (0, _ui.useDevice)(),
    isMobile = _useDevice.isMobile,
    width = _useDevice.width;
  const _useLocale = (0, _locale.useLocale)(),
    locale = _useLocale.locale,
    translate = _useLocale.translate;
  const ref = (0, _react.useRef)(null);
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value2 = _useStore$value === void 0 ? {} : _useStore$value,
    currency = _useStore$value2.currency,
    finder = _useStore$value2.finder,
    _useStore$value2$forw = _useStore$value2.forwarder,
    forwarder = _useStore$value2$forw === void 0 ? {} : _useStore$value2$forw,
    hotel = _useStore$value2.hotel,
    hotelId = _useStore$value2.id,
    _useStore$value2$tags = _useStore$value2.tags,
    _useStore$value2$tags2 = _useStore$value2$tags === void 0 ? {} : _useStore$value2$tags,
    _useStore$value2$tags3 = _useStore$value2$tags2.options,
    places = _useStore$value2$tags3 === void 0 ? [] : _useStore$value2$tags3;
  const _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    busy = _useState2[0],
    setBusy = _useState2[1];
  const _useState3 = (0, _react.useState)(DEFAULT_WIDTH),
    _useState4 = _slicedToArray(_useState3, 2),
    sliderWidth = _useState4[0],
    setSliderWidth = _useState4[1];
  (0, _react.useEffect)(() => {
    var _ref$current;
    if (ref.current) setSliderWidth(((_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.clientWidth) - 1);
  }, [width]);
  const handlePress = () => {
    setBusy(true);
    setTimeout(() => {
      const _ref2 = places.find(function () {
          let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
            isHotel = _ref3.isHotel,
            _ref3$id = _ref3.id,
            ids = _ref3$id === void 0 ? [] : _ref3$id;
          return isHotel && ids.includes(id.toString());
        }) || {},
        value = _ref2.value;
      setBusy(false);
      window.location.href = "".concat(forwarder.rates).concat((0, _helpers3.toUrlParams)(_objectSpread(_objectSpread(_objectSpread({}, (0, _helpers2.fetchUrlParams)(window.location.search)), (0, _helpers4.getFinderParams)(_objectSpread(_objectSpread(_objectSpread({}, finder), {
        place: {
          id: undefined,
          value
        }
      }), {}, {
        hotel
      }))), {}, {
        idtokenprovider: id
      })));
    }, _helpers3.SUBMIT_DELAY);
  };
  const caption = {
    action: true
  };
  const _ref4 = hotel || {},
    _ref4$features = _ref4.features,
    features = _ref4$features === void 0 ? {} : _ref4$features;
  const _ref5 = finder || {},
    _ref5$place = _ref5.place,
    _ref5$place2 = _ref5$place === void 0 ? {} : _ref5$place,
    _ref5$place2$id = _ref5$place2.id,
    _ref5$place2$id2 = _ref5$place2$id === void 0 ? [] : _ref5$place2$id,
    _ref5$place2$id3 = _slicedToArray(_ref5$place2$id2, 1),
    placeId = _ref5$place2$id3[0],
    isHotel = _ref5$place2.isHotel,
    placeName = _ref5$place2.title,
    promocode = _ref5.promocode;
  const _ref6 = (placeId ? isHotel ? features[placeId] : {
      name: placeName
    } : features[hotelId]) || {},
    hotelName = _ref6.name;
  const confidential = board.confidential,
    _board$deals = board.deals,
    deals = _board$deals === void 0 ? [] : _board$deals,
    _board$discount = board.discount,
    _board$discount2 = _board$discount === void 0 ? {} : _board$discount,
    _board$discount2$brea = _board$discount2.breakdown,
    breakdown = _board$discount2$brea === void 0 ? [] : _board$discount2$brea,
    _board$originalPrice = board.originalPrice,
    originalPrice = _board$originalPrice === void 0 ? 0 : _board$originalPrice;
  const price = propPrice / priceFactor;
  const showPricePerNight = priceFactor > 1;
  const currencyFormatProps = {
    currency,
    locale,
    maximumFractionDigits: 0,
    minimumFractionDigits: 0
  };
  const promocodeForAll = breakdown.find(_ref7 => {
    let type = _ref7.type;
    return type === _Rates.DISCOUNT_TYPE.PROMOTION;
  }) || (breakdown.find(_ref8 => {
    let type = _ref8.type;
    return type === _Rates.DISCOUNT_TYPE.DEAL;
  }) || 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.CLUB;
    }) || {},
    _ref9$percentage = _ref9.percentage,
    clubPercentage = _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.PROMOTION;
    }) || {},
    _ref1$percentage = _ref1.percentage,
    promoPercentage = _ref1$percentage === void 0 ? 0 : _ref1$percentage;
  return /*#__PURE__*/_react.default.createElement(_shared__.Card, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    ref: ref,
    className: style.card
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.preview
  }, /*#__PURE__*/_react.default.createElement(_components.Slider, {
    height: cardImageHeight,
    images: images,
    width: sliderWidth,
    className: style.slider
  })), distance && /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: [style.offset, style.spaceBetween]
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.row
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: _helpers3.ICON.LOCATION
  }), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    small: true
  }, translate(_CardHotel.L10N.LABEL_DISTANCE, {
    distance: Intl.NumberFormat(locale, {
      style: 'unit',
      unit: 'kilometer',
      minimumFractionDigits: 1,
      maximumFractionDigits: 1
    }).format(distance),
    name: hotelName
  })))), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.offset
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true
  }, name), stars > 0 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    headline: true
  }, Array.from({
    length: stars
  }, () => '★')), /*#__PURE__*/_react.default.createElement(_ui.Text, caption, location), url && /*#__PURE__*/_react.default.createElement(_ui.Action, {
    href: unavailability ? (0, _helpers.getHotelUrl)(url) : url,
    target: "_blank",
    underline: true
  }, translate(_CardHotel.L10N.ACTION_VIEW_HOTEL))), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: [style.offset, style.summary]
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.tags
  }, hasDiscountClub && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    small: true,
    className: style.tag
  }, translate(_CardHotel.L10N.LABEL_ONLY_MEMBERS_DISCOUNT, {
    value: clubPercentage > 0 ? " -".concat(Math.round(clubPercentage), "%") : ''
  }).trimEnd()), promocode && promocodeForAll && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    small: true,
    className: style.tag
  }, /*#__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]
  }, /*#__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: price >= 10000 ? 3 : 2
  }, (0, _locale.currencyFormat)(_objectSpread(_objectSpread({}, currencyFormatProps), {}, {
    value: price
  }))), originalPrice > 0 && price !== originalPrice && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    light: true,
    className: style.lineThrough
  }, (0, _locale.currencyFormat)(_objectSpread(_objectSpread({}, currencyFormatProps), {}, {
    value: originalPrice / priceFactor
  })))), nights > 1 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    small: true
  }, "".concat((0, _locale.currencyFormat)(_objectSpread(_objectSpread({}, currencyFormatProps), {}, {
    value: showPricePerNight ? propPrice : price / nights
  })), " ").concat(translate(showPricePerNight ? _CardHotel.L10N.LABEL_FOR_NIGHTS : _CardHotel.L10N.LABEL_PER_NIGHT, {
    nights
  }))), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, translate((0, _helpers4.getLabelTaxes)(taxes))), /*#__PURE__*/_react.default.createElement(_components.PriceDetails, {
    board: board
  })), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    busy: busy,
    onPress: handlePress
  }, translate(_CardHotel.L10N.ACTION_VIEW_MORE))))));
};
exports.CardHotel = CardHotel;
CardHotel.displayName = 'Mirai:Core:Rates:CardHotel';
CardHotel.propTypes = {
  board: _propTypes.default.shape({}),
  cardImageHeight: _propTypes.default.number,
  features: _propTypes.default.shape({
    distance: _propTypes.default.string,
    location: _propTypes.default.string,
    url: _propTypes.default.string
  }),
  hasDiscountClub: _propTypes.default.bool,
  id: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
  images: _propTypes.default.arrayOf(_propTypes.default.string),
  name: _propTypes.default.string,
  nights: _propTypes.default.number,
  price: _propTypes.default.number,
  priceFactor: _propTypes.default.number,
  stars: _propTypes.default.number,
  taxes: _propTypes.default.number,
  unavailability: _propTypes.default.bool
};
//# sourceMappingURL=CardHotel.js.map