"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Modal = 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 _Booking = require("./Booking.FormCancel");
var _Booking2 = require("./Booking.FormEdit");
var _Booking3 = require("./Booking.FormEditDate");
var _Booking4 = require("./Booking.FormResend");
var _Core = require("../../../Core.constants");
var _helpers = require("../../helpers");
var _Booking5 = require("../Booking.constants");
var _Booking6 = require("../Booking.l10n");
var style = _interopRequireWildcard(require("../Booking.module.css"));
const _excluded = ["id", "pinCode", "token"],
  _excluded2 = ["action", "dataSource", "onChange", "onClose"];
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 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 {
  CANCEL,
  EDIT,
  EDIT_DATE,
  RESEND
} = _Booking5.ACTION;
const Modal = _ref => {
  let {
      action,
      dataSource: {
        id,
        pinCode,
        token
      } = {},
      onChange = () => {},
      onClose
    } = _ref,
    dataSource = _objectWithoutProperties(_ref.dataSource, _excluded),
    others = _objectWithoutProperties(_ref, _excluded2);
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      urlParams: {
        idHotel
      } = {}
    }
  } = (0, _dataSources.useStore)();
  const [busy, setBusy] = (0, _react.useState)();
  const [error, setError] = (0, _react.useState)();
  const [form, setForm] = (0, _react.useState)({});
  const [visible, setVisible] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    if (action) return setVisible(true);
    setForm({});
  }, [action]);
  const handleChange = next => {
    setForm(_objectSpread(_objectSpread({}, form), next));
  };
  const handleError = error => {
    setError(error);
  };
  const handleSubmit = async () => {
    setBusy(true);
    const method = is.cancel ? 'cancel' : is.edit ? 'postpone' : is.editDate ? 'modify' : 'email';
    const props = _objectSpread({
      id,
      idHotel,
      pinCode,
      token
    }, form);
    const response = await _services.ServiceBooking[method](props).catch(error => {
      const isWarning = (error === null || error === void 0 ? void 0 : error.code) === 404 && is.editDate;
      _dataSources.Event.publish(_helpers.EVENT.NOTIFICATION, {
        error: !isWarning,
        warning: isWarning,
        defaultMessage: translate(isWarning ? _Booking6.L10N.NOTIFICATION_WARNING_NO_AVAILABILITY : _Booking6.L10N.NOTIFICATION_ERROR[_Core.ERROR.UNKNOWN], {
          checkIn: (0, _locale.dateFormat)((0, _locale.parseDate)(form === null || form === void 0 ? void 0 : form.checkIn), _Booking5.FORMAT_DATE),
          checkOut: (0, _locale.dateFormat)((0, _locale.parseDate)(form === null || form === void 0 ? void 0 : form.checkOut), _Booking5.FORMAT_DATE)
        })
      });
    });
    setBusy(false);
    if (response) {
      _dataSources.Event.publish(_helpers.EVENT.NOTIFICATION, _objectSpread({
        success: true
      }, is.cancel ? _Booking6.L10N.NOTIFICATION_CANCEL_BOOKING_SUCESS : is.edit ? _Booking6.L10N.NOTIFICATION_EDIT_BOOKING_SUCCESS : is.editDate ? _Booking6.L10N.NOTIFICATION_EDIT_DATE_SUCCESS : _Booking6.L10N.NOTIFICATION_EMAIL_SUCCESS));
    }
    handleClose();
    onChange({
      response,
      state: is.cancel ? _Booking5.STATE.CANCELLED : is.edit ? _Booking5.STATE.POSTPONED : undefined
    });
  };
  const handleClose = showCalendar => {
    const {
      motionExpand = '0.3s'
    } = _ui.Theme.get();
    setVisible(false);
    setTimeout(() => onClose(showCalendar === true ? _Booking5.ACTION.SHOW_CALENDAR : undefined), parseFloat(motionExpand.replace('s')) * 1000);
  };
  const is = {
    cancel: action === CANCEL,
    edit: action === EDIT,
    editDate: action === EDIT_DATE,
    resend: action === RESEND
  };
  return /*#__PURE__*/_react.default.createElement(_ui.Modal, _extends({}, others, {
    fit: is.editDate,
    title: translate(is.cancel ? _Booking6.L10N.ACTION_CANCEL_BOOKING : is.edit ? _Booking6.L10N.ACTION_EDIT_BOOKING : is.editDate ? _Booking6.L10N.ACTION_EDIT_DATE : _Booking6.L10N.ACTION_RESEND_BOOKING),
    preventDefault: false,
    visible: visible,
    onClose: handleClose
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: (0, _ui.styles)(style.modal, is.editDate && style.edit)
  }, /*#__PURE__*/_react.default.createElement(is.cancel ? _Booking.FormCancel : is.edit ? _Booking2.FormEdit : is.editDate ? _Booking3.FormEditDate : _Booking4.FormResend, {
    dataSource,
    form,
    responseCalendar: others.responseCalendar,
    onChange: handleChange,
    onError: handleError
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: (0, _ui.styles)(style.buttons, is.editDate && style.edit)
  }, /*#__PURE__*/_react.default.createElement(_ui.Button, {
    large: true,
    secondary: true,
    wide: true,
    onPress: () => handleClose(is.editDate),
    testId: "cancel-button"
  }, translate(_Booking6.L10N.ACTION_BACK)), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    busy: busy,
    disabled: is.resend && !!error,
    large: true,
    wide: true,
    onPress: handleSubmit,
    testId: "submit-button"
  }, is.cancel ? translate(_Booking6.L10N.ACTION_CANCEL) : is.edit ? translate(_Booking6.L10N.ACTION_POSTPONE) : is.editDate ? translate(_Booking6.L10N.ACTION_CONFIRM) : translate(_Booking6.L10N.ACTION_SEND)))));
};
exports.Modal = Modal;
Modal.displayName = 'Mirai:Core:Booking:Modal';
Modal.propTypes = {
  action: _propTypes.default.oneOf(Object.values(_Booking5.ACTION)),
  dataSource: _propTypes.default.shape({
    id: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
    pinCode: _propTypes.default.number,
    token: _propTypes.default.string
  }),
  onChange: _propTypes.default.func,
  onClose: _propTypes.default.func
};
//# sourceMappingURL=Booking.Modal.js.map