"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Calendar = 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 _RatesExplorer = require("./RatesExplorer.constants");
var _RatesExplorer2 = require("./RatesExplorer.l10n");
var style = _interopRequireWildcard(require("./RatesExplorer.module.css"));
var _ = require("../");
var _helpers2 = require("../../Finder/helpers");
var _helpers3 = require("../../helpers");
const _excluded = ["captions", "highlights", "minNightsWithDeals", "pricesInfo", "rangeMaxDays", "rangeMinDays", "visible", "visited", "onPress"],
  _excluded2 = ["locale"];
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 Calendar = _ref => {
  var _getDataSource, _getDataSource$places, _getDataSource$places2, _getDataSource$places3, _getDataSource2;
  let {
      captions,
      highlights = [],
      minNightsWithDeals,
      pricesInfo: {
        currency,
        guests: propGuests,
        nights,
        taxType
      } = {},
      rangeMaxDays,
      rangeMinDays,
      visible,
      visited,
      onPress
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    height,
    isMobile
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const _useStore = (0, _dataSources.useStore)(),
    {
      value: {
        locale
      }
    } = _useStore,
    store = _objectWithoutProperties(_useStore.value, _excluded2);
  const [focus, setFocus] = (0, _react.useState)();
  const [value, setValue] = (0, _react.useState)((0, _helpers3.valueFormat)(others.value));
  (0, _react.useEffect)(() => {
    setValue((0, _helpers3.valueFormat)(others.value));
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [others.value]);
  const [from, to] = (0, _helpers3.parseValue)(value);
  const {
    config: {
      hotels: dataSource,
      lodgingDenomination: individual
    } = {},
    hotel: {
      id
    } = {}
  } = store || {};
  const tagId = (_getDataSource = (0, _helpers2.getDataSource)(store)) === null || _getDataSource === void 0 ? void 0 : (_getDataSource$places = _getDataSource.places) === null || _getDataSource$places === void 0 ? void 0 : (_getDataSource$places2 = _getDataSource$places.options) === null || _getDataSource$places2 === void 0 ? void 0 : (_getDataSource$places3 = _getDataSource$places2.find(function () {
    let {
      id: ids = [],
      isHotel
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return isHotel && ids[0] === id;
  })) === null || _getDataSource$places3 === void 0 ? void 0 : _getDataSource$places3.value;
  const accommodationType = (0, _helpers3.getAccommodationType)({
    dataSource,
    hotels: (0, _helpers2.getHotels)((_getDataSource2 = (0, _helpers2.getDataSource)(store)) === null || _getDataSource2 === void 0 ? void 0 : _getDataSource2.placesSource, tagId),
    individual: individual && typeof individual === 'object' ? individual[id] : individual
  });
  const {
    adults = 0,
    children = 0,
    guests = 0
  } = (0, _helpers.getOccupation)(propGuests);
  return /*#__PURE__*/_react.default.createElement(isMobile ? _.Field : _react.Fragment, isMobile ? {
    caption: value.length ? (0, _helpers3.getCalendarCaption)((0, _helpers3.parseValue)(value), translate, _locale.dateFormat, locale) : undefined,
    error: visited && (!from || !to),
    icon: _helpers3.ICON.CALENDAR,
    label: translate(_RatesExplorer2.L10N.LABEL_WHEN),
    placeholder: (0, _helpers3.getCalendarCaption)(undefined, translate, _locale.dateFormat, locale),
    visible,
    onPress,
    className: style.fieldCalendar,
    testId: "".concat(others.testId, "-field")
  } : undefined, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.Calendar, _objectSpread(_objectSpread({}, others), {}, {
    captions,
    format: _helpers3.CALENDAR_FORMAT,
    from: (0, _helpers.calcFromDate)(highlights),
    highlights,
    l10n: {
      actionLoadMore: translate(_RatesExplorer2.L10N.ACTION_LOAD_MORE_DATES),
      captionType: translate(_RatesExplorer2.L10N.LABEL_PRICE)
    },
    locale,
    months: isMobile ? _helpers3.MONTHS_IN_MOBILE : undefined,
    range: true,
    rangeMaxDays,
    rangeMinDays,
    to: (0, _helpers.calcToDate)(highlights, rangeMaxDays),
    tooltips: (0, _helpers.calcTooltips)({
      focus,
      from,
      highlights,
      minNightsWithDeals,
      rangeMinDays,
      to,
      translate
    }),
    value,
    onFocus: setFocus,
    className: (0, _ui.styles)(style.content, height <= _RatesExplorer.DEVICE_XS_HEIGHT && style.calendarXS)
  })), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.calendarInfo
  }, !isMobile && /*#__PURE__*/_react.default.createElement(_.BookingDates, {
    focus,
    from,
    rangeMinDays,
    to
  }), currency && nights && taxType ? /*#__PURE__*/_react.default.createElement(_ui.Text, {
    small: true,
    className: style.notification
  }, translate(_RatesExplorer2.L10N.LABEL_INFO_PRICES_WITH_GUESTS, {
    accommodation: translate(_RatesExplorer2.L10N.LABEL_ACCOMMODATION_TYPE, {
      type: accommodationType
    }),
    currency,
    guests: translate(guests > 0 ? _RatesExplorer2.L10N.LABEL_OCCUPATION_TYPE_GUEST : _RatesExplorer2.L10N.LABEL_ADULTS_AND_CHILDREN, {
      adults,
      children,
      guests
    }),
    nights,
    taxType
  })) : null), isMobile && /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.footer
  }, /*#__PURE__*/_react.default.createElement(_.BookingDates, {
    focus,
    from,
    rangeMinDays,
    to
  }), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    disabled: !from || !to,
    onPress: onPress,
    testId: "".concat(others.testId, "-button-next")
  }, translate(_RatesExplorer2.L10N.ACTION_NEXT)))));
};
exports.Calendar = Calendar;
Calendar.displayName = 'Mirai:Core:RatesExplorer:Calendar';
Calendar.propTypes = {
  captions: _propTypes.default.shape({}),
  highlights: _propTypes.default.arrayOf(_propTypes.default.string),
  minNightsWithDeals: _propTypes.default.number,
  pricesInfo: _propTypes.default.shape({
    currency: _propTypes.default.string,
    guests: _propTypes.default.number,
    nights: _propTypes.default.number,
    taxType: _propTypes.default.number
  }),
  rangeMaxDays: _propTypes.default.number,
  rangeMinDays: _propTypes.default.number,
  visible: _propTypes.default.bool,
  visited: _propTypes.default.bool,
  onChange: _propTypes.default.func,
  onPress: _propTypes.default.func
};
//# sourceMappingURL=RatesExplorer.Calendar.js.map