"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ModalCalendar = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _services = require("@mirai/services");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _Core = require("../../../Core.constants");
var _shared__ = require("../../__shared__");
var _helpers = require("../../helpers");
var _Booking = require("../Booking.constants");
var _Booking2 = require("../Booking.l10n");
var style = _interopRequireWildcard(require("../Booking.module.css"));
const _excluded = ["dataSource", "onClose", "onConfirm"];
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 _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; }
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 ModalCalendar = _ref => {
  let _ref$dataSource = _ref.dataSource,
    _ref$dataSource2 = _ref$dataSource === void 0 ? {} : _ref$dataSource,
    id = _ref$dataSource2.id,
    onClose = _ref.onClose,
    _ref$onConfirm = _ref.onConfirm,
    onConfirm = _ref$onConfirm === void 0 ? () => {} : _ref$onConfirm,
    others = _objectWithoutProperties(_ref, _excluded);
  const _useLocale = (0, _locale.useLocale)(),
    dateFormat = _useLocale.dateFormat,
    locale = _useLocale.locale,
    translate = _useLocale.translate;
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value$calen = _useStore.value.calendar,
    _useStore$value$calen2 = _useStore$value$calen === void 0 ? {} : _useStore$value$calen,
    rangeMaxDays = _useStore$value$calen2.rangeMaxDays,
    to = _useStore$value$calen2.to;
  const _useState = (0, _react.useState)(),
    _useState2 = _slicedToArray(_useState, 2),
    busy = _useState2[0],
    setBusy = _useState2[1];
  const _useState3 = (0, _react.useState)([]),
    _useState4 = _slicedToArray(_useState3, 2),
    value = _useState4[0],
    setValue = _useState4[1];
  const handleChange = function () {
    let next = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    setValue((0, _helpers.valueFormat)(next));
  };
  const handleCheckAvailability = async () => {
    setBusy(true);
    const response = await _services.ServiceBooking.modification.date.quote({
      id,
      checkIn,
      checkOut
    }).catch(error => {
      const isWarning = (error === null || error === void 0 ? void 0 : error.code) === 404;
      _dataSources.Event.publish(_helpers.EVENT.NOTIFICATION, {
        error: !isWarning,
        warning: isWarning,
        defaultMessage: translate(isWarning ? _Booking2.L10N.NOTIFICATION_WARNING_NO_AVAILABILITY : _Booking2.L10N.NOTIFICATION_ERROR[_Core.ERROR.UNKNOWN], {
          checkIn: dateFormat((0, _locale.parseDate)(checkIn), _Booking.FORMAT_DATE),
          checkOut: dateFormat((0, _locale.parseDate)(checkOut), _Booking.FORMAT_DATE)
        })
      });
    });
    setBusy(false);
    setValue([]);
    if (response) {
      onClose();
      onConfirm(response);
    }
  };
  const _ref2 = value || [],
    _ref3 = _slicedToArray(_ref2, 2),
    checkIn = _ref3[0],
    checkOut = _ref3[1];
  return /*#__PURE__*/_react.default.createElement(_ui.Modal, _extends({}, others, {
    fit: true,
    title: translate(_Booking2.L10N.LABEL_WHEN),
    onClose: onClose
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.modalCalendar
  }, /*#__PURE__*/_react.default.createElement(_ui.Calendar, {
    locale,
    rangeMaxDays,
    to,
    value,
    format: _locale.DEFAULT_DATE_FORMAT,
    range: true,
    onChange: handleChange,
    className: style.calendar,
    testId: "booking-edit-calendar"
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.footer
  }, /*#__PURE__*/_react.default.createElement(_shared__.BookingDates, {
    from: checkIn ? (0, _locale.parseDate)(checkIn) : undefined,
    to: checkOut ? (0, _locale.parseDate)(checkOut) : undefined
  }), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    busy: busy,
    disabled: value.length !== 2 || !checkIn || !checkOut,
    wide: true,
    onPress: handleCheckAvailability,
    testId: "booking-check-button"
  }, translate(_Booking2.L10N.ACTION_CHECK_AVAILABILITY)))));
};
exports.ModalCalendar = ModalCalendar;
ModalCalendar.displayName = 'Mirai:Core:Booking:ModalCalendar';
ModalCalendar.propTypes = {
  dataSource: _propTypes.default.shape({
    id: _propTypes.default.string.isRequired
  }).isRequired,
  onClose: _propTypes.default.func,
  onConfirm: _propTypes.default.func
};
//# sourceMappingURL=Booking.ModalCalendar.js.map