"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 _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 {
      dataSource: {
        id
      } = {},
      onClose,
      onConfirm = () => {}
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    dateFormat,
    locale,
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      calendar: {
        rangeMaxDays,
        to
      } = {}
    }
  } = (0, _dataSources.useStore)();
  const [busy, setBusy] = (0, _react.useState)();
  const [value, setValue] = (0, _react.useState)([]);
  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 [checkIn, checkOut] = value || [];
  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