"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Booking = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _services = require("@mirai/services");
var _ui = require("@mirai/ui");
var _react = _interopRequireWildcard(require("react"));
var _Booking = require("./Booking.constants");
var _Booking2 = require("./Booking.l10n");
var style = _interopRequireWildcard(require("./Booking.module.css"));
var _Booking3 = _interopRequireDefault(require("./Booking.skeleton"));
var _components = require("./components");
var _partials = require("./partials");
var _Booking4 = require("./partials/Booking.Payment");
var _hooks = require("./partials/hooks");
var _shared__ = require("../__shared__");
var _Header = require("../Header");
var _helpers = require("../helpers");
const _excluded = ["tracking"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 _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); }
function _objectDestructuringEmpty(t) { if (null == t) throw new TypeError("Cannot destructure " + t); }
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); }
const Booking = _ref => {
  var _dataSource$modify, _dataSource$modify$bo;
  let others = _extends({}, (_objectDestructuringEmpty(_ref), _ref));
  const {
    isDesktop,
    isMobile
  } = (0, _ui.useDevice)();
  const {
    locale,
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      hotel: {
        url
      } = {},
      skeleton = false,
      urlParams: {
        code: token,
        reservationCode: id,
        hasToTracking: tracking
      } = {}
    }
  } = (0, _dataSources.useStore)();
  const [action, setAction] = (0, _react.useState)();
  const [calendar, setCalendar] = (0, _react.useState)();
  const [dataSource, setDataSource] = (0, _react.useState)();
  const [expanded, setExpanded] = (0, _react.useState)(false);
  const [menuVisible, setMenuVisible] = (0, _react.useState)(false);
  const [responseCalendar, setResponseCalendar] = (0, _react.useState)();
  const [responseError, setResponseError] = (0, _react.useState)();
  const boardFlow = (0, _hooks.useEditFlow)({
    dataSource,
    modifyKey: 'board',
    confirmAction: _Booking.ACTION.EDIT_BOARD_CONFIRM,
    setAction,
    setMenuVisible
  });
  const occupationFlow = (0, _hooks.useEditFlow)({
    dataSource,
    modifyKey: 'occupation',
    confirmAction: _Booking.ACTION.EDIT_OCCUPATION_CONFIRM,
    setAction,
    setMenuVisible
  });
  (0, _react.useEffect)(() => {
    if (skeleton || !id || !token) return;
    (async () => {
      setDataSource(await _services.ServiceBooking.get({
        id,
        locale,
        token,
        tracking,
        url
      }).catch(error => setResponseError(error)));
      setAction();
    })();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [id, locale]);
  (0, _react.useEffect)(() => {
    if (!dataSource || !dataSource.tracking) return;
    const event = dataSource.state === _Booking.STATE.CANCELLED ? _helpers.EVENT.BOOKING_CANCEL : _helpers.EVENT.BOOKING_RESPONSE;
    _dataSources.Event.publish(event, {
      event,
      response: _objectSpread({}, dataSource)
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [dataSource === null || dataSource === void 0 ? void 0 : dataSource.state]);
  const handleChange = _ref2 => {
    let {
        response: {
          tracking
        } = {},
        state
      } = _ref2,
      response = _objectWithoutProperties(_ref2.response, _excluded);
    const isConfirmed = (response === null || response === void 0 ? void 0 : response.state) === _Booking.STATE.CONFIRMED;
    if (state || isConfirmed) {
      setDataSource(_objectSpread(_objectSpread({}, isConfirmed ? response : dataSource), {}, {
        tracking
      }, isConfirmed ? undefined : {
        state
      }));
    }
  };
  const handleConfirmCalendar = response => {
    setResponseCalendar(response);
    handleMenuPress(_Booking.ACTION.EDIT_DATE);
  };
  const handleMenuPress = action => {
    if (action === _Booking.ACTION.PRINT) {
      setExpanded(true);
      setTimeout(window.print, 10);
    } else if (action === _Booking.ACTION.SHOW_CALENDAR) {
      setCalendar(true);
    } else if (action === _Booking.ACTION.EDIT_BOARD) {
      boardFlow.open();
    } else if (action === _Booking.ACTION.EDIT_OCCUPATION) {
      occupationFlow.open();
    } else {
      setAction(action);
    }
  };
  const handleBackHome = () => {
    const {
      payload
    } = responseError;
    window.location.href = (payload === null || payload === void 0 ? void 0 : payload.hotelUrl) || window.location.origin;
  };
  const {
    payment,
    state
  } = dataSource || {};
  const response = dataSource !== undefined;
  const isCancelled = state === _Booking.STATE.CANCELLED;
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Header.Header, null), /*#__PURE__*/_react.default.createElement(_ui.View, {
    role: "booking",
    testId: others.testId,
    className: (0, _ui.styles)(style.container, others.className)
  }, /*#__PURE__*/_react.default.createElement(_ui.Progress, {
    indeterminate: true,
    visible: !response && !responseError,
    className: style.progress
  }), response ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: isDesktop,
    className: style.content
  }, /*#__PURE__*/_react.default.createElement(_ui.View, null, state !== _Booking.STATE.POS_ERROR && /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    error: isCancelled,
    info: state === _Booking.STATE.COMPLETED,
    success: state === _Booking.STATE.CONFIRMED,
    warning: state === _Booking.STATE.POSTPONED,
    wide: true
  }, translate(_Booking2.L10N.NOTIFICATION[state]), /*#__PURE__*/_react.default.createElement(_components.Hotelverse, dataSource)), payment && !isCancelled && /*#__PURE__*/_react.default.createElement(_Booking4.BookingPayment, {
    dataSource,
    onError: setResponseError
  }), /*#__PURE__*/_react.default.createElement(_shared__.BookingSummary, {
    box: !isDesktop,
    dataSource: _objectSpread(_objectSpread({}, dataSource), {}, {
      showBasePrice: true
    }),
    className: (0, _ui.styles)(isDesktop && style.print)
  }), /*#__PURE__*/_react.default.createElement(_partials.Information, {
    dataSource: dataSource
  }), /*#__PURE__*/_react.default.createElement(_shared__.BookingDetails, {
    dataSource,
    expanded,
    expanded: isMobile,
    className: style.box
  }), /*#__PURE__*/_react.default.createElement(_shared__.BookingTerms, {
    dataSource: dataSource,
    small: true,
    trackBookingInteraction: true
  })), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.side
  }, isDesktop && /*#__PURE__*/_react.default.createElement(_shared__.BookingSummary, {
    box: true,
    dataSource: _objectSpread(_objectSpread({}, dataSource), {}, {
      showBasePrice: true
    }),
    className: style.noPrint
  }), /*#__PURE__*/_react.default.createElement(_partials.Menu, {
    dataSource: dataSource,
    visible: menuVisible,
    onPress: handleMenuPress,
    onVisibleChange: setMenuVisible
  }))), /*#__PURE__*/_react.default.createElement(_partials.Modal, {
    action,
    dataSource: _objectSpread(_objectSpread({}, dataSource), {}, {
      token
    }),
    responseBoard: boardFlow.response,
    responseCalendar,
    responseOccupation: occupationFlow.response,
    onChange: handleChange,
    onClose: nextAction => {
      if (nextAction === _Booking.ACTION.EDIT_BOARD && action === _Booking.ACTION.EDIT_BOARD_CONFIRM) {
        var _boardFlow$response;
        setAction();
        boardFlow.open({
          fromConfirm: true,
          room: (_boardFlow$response = boardFlow.response) === null || _boardFlow$response === void 0 ? void 0 : _boardFlow$response.room
        });
        return;
      }
      if (nextAction === _Booking.ACTION.EDIT_OCCUPATION && action === _Booking.ACTION.EDIT_OCCUPATION_CONFIRM) {
        var _occupationFlow$respo;
        setAction();
        occupationFlow.open({
          fromConfirm: true,
          room: (_occupationFlow$respo = occupationFlow.response) === null || _occupationFlow$respo === void 0 ? void 0 : _occupationFlow$respo.room
        });
        return;
      }
      setAction();
      if (Object.values(_Booking.ACTION).includes(nextAction)) handleMenuPress(nextAction);
    }
  }), /*#__PURE__*/_react.default.createElement(_partials.ModalCalendar, {
    dataSource: dataSource,
    visible: calendar,
    onConfirm: handleConfirmCalendar,
    onClose: () => {
      setCalendar();
      setAction();
    }
  }), /*#__PURE__*/_react.default.createElement(_partials.ModalBoard, {
    dataSource: dataSource,
    room: boardFlow.room,
    visible: boardFlow.visible,
    onBack: boardFlow.back,
    onConfirm: boardFlow.confirm,
    onClose: boardFlow.close
  }), /*#__PURE__*/_react.default.createElement(_partials.ModalOccupation, {
    dataSource: dataSource,
    room: occupationFlow.room,
    visible: occupationFlow.visible,
    onBack: occupationFlow.back,
    onConfirm: occupationFlow.confirm,
    onClose: occupationFlow.close
  }), /*#__PURE__*/_react.default.createElement(_partials.ModalRoomSelection, {
    dataSource: dataSource,
    rooms: dataSource === null || dataSource === void 0 ? void 0 : (_dataSource$modify = dataSource.modify) === null || _dataSource$modify === void 0 ? void 0 : (_dataSource$modify$bo = _dataSource$modify.board) === null || _dataSource$modify$bo === void 0 ? void 0 : _dataSource$modify$bo.rooms,
    title: translate(_Booking2.L10N.ACTION_EDIT_BOARD),
    visible: boardFlow.roomSelection,
    onBack: boardFlow.backRoomSelection,
    onNext: boardFlow.selectRoom,
    onClose: boardFlow.closeRoomSelection,
    testId: "booking-edit-board"
  }), /*#__PURE__*/_react.default.createElement(_partials.ModalRoomSelection, {
    dataSource: dataSource,
    title: translate(_Booking2.L10N.ACTION_EDIT_OCCUPATION),
    visible: occupationFlow.roomSelection,
    onBack: occupationFlow.backRoomSelection,
    onNext: occupationFlow.selectRoom,
    onClose: occupationFlow.closeRoomSelection,
    testId: "booking-edit-occupation"
  })) : responseError ? /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    error: true,
    large: true,
    className: style.error
  }, translate(_Booking2.L10N.NOTIFICATION_ERROR[responseError.code] || _Booking2.L10N.NOTIFICATION_ERROR[_Booking.ERROR.GENERIC]), /*#__PURE__*/_react.default.createElement(_ui.Action, {
    paragraph: true,
    underline: true,
    wide: true,
    onPress: handleBackHome
  }, translate(_Booking2.L10N.ACTION_BACK_HOME))) : /*#__PURE__*/_react.default.createElement(_Booking3.default, null)), /*#__PURE__*/_react.default.createElement(_shared__.Footer, null));
};
exports.Booking = Booking;
Booking.displayName = 'Mirai:Core:Booking';
Booking.propTypes = {};
//# sourceMappingURL=Booking.js.map