"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); }
const DEFAULT_WIDTH = 374;
const CardHotel = function () {
  let {
    board = {},
    cardImageHeight,
    features: {
      distance,
      location,
      url
    } = {},
    hasDiscountClub = false,
    id,
    images = [],
    name,
    nights = 1,
    price: propPrice,
    priceFactor = 1,
    stars,
    taxes,
    unavailability
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  const {
    isMobile,
    width
  } = (0, _ui.useDevice)();
  const {
    locale,
    translate
  } = (0, _locale.useLocale)();
  const ref = (0, _react.useRef)(null);
  const {
    value: {
      currency,
      finder,
      forwarder = {},
      hotel,
      id: hotelId,
      tags: {
        options: places = []
      } = {}
    } = {}
  } = (0, _dataSources.useStore)();
  const [busy, setBusy] = (0, _react.useState)(false);
  const [sliderWidth, setSliderWidth] = (0, _react.useState)(DEFAULT_WIDTH);
  (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 {
        value
      } = places.find(function () {
        let {
          isHotel,
          id: ids = []
        } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        return isHotel && ids.includes(id.toString());
      }) || {};
      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 {
    features = {}
  } = hotel || {};
  const {
    place: {
      id: [placeId] = [],
      isHotel,
      title: placeName
    } = {},
    promocode
  } = finder || {};
  const {
    name: hotelName
  } = (placeId ? isHotel ? features[placeId] : {
    name: placeName
  } : features[hotelId]) || {};
  const {
    confidential,
    deals = [],
    discount: {
      breakdown = []
    } = {},
    originalPrice = 0
  } = board;
  const price = propPrice / priceFactor;
  const showPricePerNight = priceFactor > 1;
  const currencyFormatProps = {
    currency,
    locale,
    maximumFractionDigits: 0,
    minimumFractionDigits: 0
  };
  const promocodeForAll = breakdown.find(_ref => {
    let {
      type
    } = _ref;
    return type === _Rates.DISCOUNT_TYPE.PROMOTION;
  }) || (breakdown.find(_ref2 => {
    let {
      type
    } = _ref2;
    return type === _Rates.DISCOUNT_TYPE.DEAL;
  }) || deals.length > 0) && confidential;
  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;
  }) || {};
  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