"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Unavailability = 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 _helpers = require("./helpers");
var _Rates = require("./Rates.constants");
var _Rates2 = require("./Rates.l10n");
var style = _interopRequireWildcard(require("./Rates.module.css"));
var _shared__ = require("../__shared__");
var _helpers2 = require("../helpers");
const _excluded = ["price", "taxes"];
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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
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; }
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 Unavailability = _ref => {
  let {
    calendar = {},
    hotel: {
      email,
      phone
    } = {},
    hotels = [],
    nights = 1,
    priceFactor,
    suggestions: {
      calendar: calendarSuggestions,
      description,
      occupation: occupationSuggestions
    } = {},
    taxesType,
    onMapView
  } = _ref;
  const {
    isMobile,
    width
  } = (0, _ui.useDevice)();
  const {
    locale,
    translate
  } = (0, _locale.useLocale)();
  const {
    value: store
  } = (0, _dataSources.useStore)();
  (0, _react.useEffect)(() => {
    _dataSources.Event.publish(_helpers2.EVENT.FINDER, {
      calendar
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  const {
    currency,
    finder = {},
    hotel: {
      accommodationType: hotelAccommodationType,
      variants = {}
    } = {}
  } = store;
  const cardImageHeight = (0, _helpers.getCardImageHeight)();
  const handleCalendar = (from, to) => {
    _dataSources.Event.publish(_helpers2.EVENT.FINDER, {
      finder: _objectSpread(_objectSpread({}, finder), {}, {
        calendar: [from, to]
      })
    });
  };
  const handleOccupation = function () {
    let rooms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    _dataSources.Event.publish(_helpers2.EVENT.FINDER, {
      finder: _objectSpread(_objectSpread({}, finder), {}, {
        occupation: rooms.map(function () {
          let {
            occupation
          } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          return occupation;
        })
      })
    });
  };
  const handleFinder = () => {
    _dataSources.Event.publish(_helpers2.EVENT.FINDER, occupationSuggestions ? {
      focus: 'occupation'
    } : {
      calendar,
      focus: 'calendar'
    });
  };
  const renderPrice = function () {
    let {
      price: propPrice,
      onPress
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    const price = propPrice / priceFactor;
    return /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: style.price
    }, /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      small: true
    }, translate(_Rates2.L10N.LABEL_FROM)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      headline: true,
      level: price >= 10000 ? 3 : 2
    }, (0, _locale.currencyFormat)({
      currency,
      locale,
      maximumFractionDigits: 0,
      minimumFractionDigits: 0,
      value: price
    })), !!taxesType && /*#__PURE__*/_react.default.createElement(_ui.Text, {
      light: true,
      small: true
    }, translate((0, _helpers.getLabelTaxes)(taxesType)))), /*#__PURE__*/_react.default.createElement(_ui.Button, {
      onPress: onPress
    }, translate(_Rates2.L10N.ACTION_VIEW_MORE)));
  };
  const renderContactInfo = () => /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    className: style.contactInfo
  }, translate(_Rates2.L10N.LABEL_CONTACT_INFO, {
    email: /*#__PURE__*/_react.default.createElement(_ui.Action, {
      href: "mailto:".concat(email),
      target: "_blank",
      underline: true,
      className: style.action
    }, email),
    phone: /*#__PURE__*/_react.default.createElement(_ui.Action, {
      href: "tel:".concat(phone),
      target: "_blank",
      underline: true,
      className: style.action
    }, phone)
  }));
  const {
    place: {
      id: [id] = []
    } = {}
  } = finder;
  const {
    accommodationType = hotelAccommodationType
  } = variants[id] || {};
  const hasSuggestions = (calendarSuggestions === null || calendarSuggestions === void 0 ? void 0 : calendarSuggestions.length) > 0 || (occupationSuggestions === null || occupationSuggestions === void 0 ? void 0 : occupationSuggestions.length) > 0;
  return /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.unavailability
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: !isMobile,
    className: style.banner
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    accent: true,
    value: occupationSuggestions ? _helpers2.ICON.PERSON_OFF : _helpers2.ICON.CALENDAR_BUSY,
    className: style.icon
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    wide: true,
    className: style.texts
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    accent: true,
    bold: true,
    headline: true,
    className: style.title
  }, occupationSuggestions ? translate(_Rates2.L10N.LABEL_NO_AVAILABILITY_OCCUPATION) : description || translate(_Rates2.L10N.LABEL_NO_AVAILABILITY_CALENDAR)), !isMobile && email && phone && renderContactInfo()), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    onPress: handleFinder,
    className: style.button
  }, translate(_Rates2.L10N.ACTION_REFINE_SEARCH)), isMobile && renderContactInfo()), hasSuggestions && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true
  }, translate(_Rates2.L10N.LABEL_AVAILABLE_ALTERNATIVES)), (calendarSuggestions === null || calendarSuggestions === void 0 ? void 0 : calendarSuggestions.length) > 0 && /*#__PURE__*/_react.default.createElement(_shared__.Cards, null, calendarSuggestions.map(function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      {
        price = 0,
        taxes
      } = _ref2,
      others = _objectWithoutProperties(_ref2, _excluded);
    let index = arguments.length > 1 ? arguments[1] : undefined;
    const from = (0, _locale.parseDate)(others === null || others === void 0 ? void 0 : others.from);
    const to = (0, _locale.parseDate)(others === null || others === void 0 ? void 0 : others.to);
    return /*#__PURE__*/_react.default.createElement(_shared__.Card, {
      key: "calendar:".concat(index)
    }, /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.card
    }, /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      headline: true
    }, (0, _locale.dateFormat)(from, _objectSpread({
      locale
    }, _Rates.DATE_FORMAT))), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      className: style.anchorDates
    }, "\u2014"), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      headline: true
    }, (0, _locale.dateFormat)(to || focus, _objectSpread({
      locale
    }, _Rates.DATE_FORMAT)))), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true
    }, "".concat((0, _locale.dateDiff)(from, to).days, " ").concat(translate(_Rates2.L10N.LABEL_NIGHTS)))), renderPrice({
      price,
      taxes,
      onPress: () => handleCalendar(from, to)
    })));
  })), (occupationSuggestions === null || occupationSuggestions === void 0 ? void 0 : occupationSuggestions.length) > 0 && /*#__PURE__*/_react.default.createElement(_shared__.Cards, null, occupationSuggestions.map((_ref3, index) => {
    let {
      rooms = [],
      price = 0,
      taxes
    } = _ref3;
    return /*#__PURE__*/_react.default.createElement(_shared__.Card, {
      key: "rooms:".concat(index)
    }, /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.card
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      capitalize: true,
      headline: true
    }, "".concat(rooms.length, " ").concat((0, _helpers2.getAccommodationLabel)({
      plural: rooms.length > 1,
      type: accommodationType
    }, translate))), /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.distribution
    }, rooms.map(function () {
      let {
        name,
        occupation = []
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      let roomIndex = arguments.length > 1 ? arguments[1] : undefined;
      return /*#__PURE__*/_react.default.createElement(_ui.View, {
        key: "occupation:".concat(index, ":room:").concat(roomIndex),
        className: style.room
      }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
        action: true,
        capitalize: true
      }, name), occupation.map(function () {
        var _item$ages;
        let item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        let itemIndex = arguments.length > 1 ? arguments[1] : undefined;
        return /*#__PURE__*/_react.default.createElement(_ui.Text, {
          key: "occupation:".concat(index, ":room:").concat(roomIndex, ":").concat(itemIndex),
          light: true,
          small: true
        }, "".concat((0, _helpers2.getOccupationLabel)(_objectSpread(_objectSpread({}, item), {}, {
          ages: []
        }), translate), " ").concat(item.type === _helpers2.OCCUPATION_TYPE.CHILD ? "(".concat(translate(_Rates2.L10N.LABEL_YEARS_OLD, {
          amount: (_item$ages = item.ages) === null || _item$ages === void 0 ? void 0 : _item$ages.sort().join('-')
        }), ")") : ''));
      }));
    })), renderPrice({
      price,
      taxes,
      onPress: () => handleOccupation(rooms)
    })));
  })), hasSuggestions && hotels.length > 0 && /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: !isMobile,
    className: style.spaceBetween
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true
  }, translate(_Rates2.L10N.LABEL_ALTERNATIVE_HOTEL_SUGGESTIONS, {
    value: hotels.length
  })), hotels.length > 1 && /*#__PURE__*/_react.default.createElement(_ui.Button, {
    secondary: true,
    wide: isMobile,
    onPress: () => onMapView(hotels)
  }, translate(_Rates2.L10N.ACTION_SHOW_ON_MAP))), /*#__PURE__*/_react.default.createElement(_shared__.Cards, null, hotels.map((item, index) => /*#__PURE__*/_react.default.createElement(_components.CardHotel, _extends({
    key: "".concat(item.index || index, ":").concat(width)
  }, item, {
    cardImageHeight: cardImageHeight,
    nights: nights,
    priceFactor: priceFactor,
    unavailability: true
  })))));
};
exports.Unavailability = Unavailability;
Unavailability.displayName = 'Mirai:Core:Rates:Unavailability';
Unavailability.propTypes = {
  calendar: _propTypes.default.shape({}),
  hotel: _propTypes.default.shape({
    email: _propTypes.default.string,
    phone: _propTypes.default.string
  }),
  hotels: _propTypes.default.arrayOf(_propTypes.default.shape({})),
  nights: _propTypes.default.number,
  priceFactor: _propTypes.default.number,
  suggestions: _propTypes.default.shape({
    calendar: _propTypes.default.arrayOf(_propTypes.default.shape({})),
    occupation: _propTypes.default.arrayOf(_propTypes.default.shape({}))
  }),
  taxesType: _propTypes.default.number,
  onMapView: _propTypes.default.func
};
//# sourceMappingURL=Rates.Unavailability.js.map