"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useEditFlow = void 0;
var _react = require("react");
var _helpers = require("../../helpers");
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; }
const useEditFlow = _ref => {
  let confirmAction = _ref.confirmAction,
    dataSource = _ref.dataSource,
    modifyKey = _ref.modifyKey,
    setAction = _ref.setAction,
    setMenuVisible = _ref.setMenuVisible;
  const _useState = (0, _react.useState)(),
    _useState2 = _slicedToArray(_useState, 2),
    response = _useState2[0],
    setResponse = _useState2[1];
  const _useState3 = (0, _react.useState)(),
    _useState4 = _slicedToArray(_useState3, 2),
    room = _useState4[0],
    setRoom = _useState4[1];
  const _useState5 = (0, _react.useState)(false),
    _useState6 = _slicedToArray(_useState5, 2),
    roomSelection = _useState6[0],
    setRoomSelection = _useState6[1];
  const _useState7 = (0, _react.useState)(false),
    _useState8 = _slicedToArray(_useState7, 2),
    visible = _useState8[0],
    setVisible = _useState8[1];
  const getRoomSelectionState = (0, _react.useCallback)(() => {
    const _ref2 = dataSource || {},
      details = _ref2.details,
      _ref2$modify = _ref2.modify,
      _ref2$modify2 = _ref2$modify === void 0 ? {} : _ref2$modify,
      _ref2$modify2$modifyK = _ref2$modify2[modifyKey],
      _ref2$modify2$modifyK2 = _ref2$modify2$modifyK === void 0 ? {} : _ref2$modify2$modifyK,
      _ref2$modify2$modifyK3 = _ref2$modify2$modifyK2.rooms,
      rooms = _ref2$modify2$modifyK3 === void 0 ? [] : _ref2$modify2$modifyK3;
    const selectableRooms = (0, _helpers.getRoomSelectionData)({
      details,
      occupation: rooms
    });
    const totalRooms = (0, _helpers.getDetailsByRoom)(details).length || selectableRooms.length;
    const hasPartialRoomSelection = totalRooms > 1 && selectableRooms.length < totalRooms;
    return {
      selectableRooms,
      showRoomSelection: selectableRooms.length > 1 || hasPartialRoomSelection
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [dataSource === null || dataSource === void 0 ? void 0 : dataSource.details, modifyKey]);
  const open = (0, _react.useCallback)(function () {
    let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref3$fromConfirm = _ref3.fromConfirm,
      fromConfirm = _ref3$fromConfirm === void 0 ? false : _ref3$fromConfirm,
      selectedRoom = _ref3.room;
    const _getRoomSelectionStat = getRoomSelectionState(),
      selectableRooms = _getRoomSelectionStat.selectableRooms,
      hasRoomSelection = _getRoomSelectionStat.showRoomSelection;
    const showRoomSelection = !fromConfirm && hasRoomSelection;
    const resolved = selectedRoom !== null && selectedRoom !== void 0 ? selectedRoom : !showRoomSelection && selectableRooms.length === 1 ? selectableRooms[0].room : undefined;
    setRoom(resolved);
    setRoomSelection(showRoomSelection);
    setVisible(!showRoomSelection);
  }, [getRoomSelectionState]);
  const confirm = (0, _react.useCallback)(res => {
    setResponse(res);
    setAction(confirmAction);
  }, [confirmAction, setAction]);
  const close = (0, _react.useCallback)(() => {
    setVisible(false);
    setRoom();
    setRoomSelection(false);
    setAction();
  }, [setAction]);
  const back = (0, _react.useCallback)(() => {
    const _getRoomSelectionStat2 = getRoomSelectionState(),
      showRoomSelection = _getRoomSelectionStat2.showRoomSelection;
    setVisible(false);
    if (showRoomSelection) {
      setRoomSelection(true);
      return;
    }
    setRoomSelection(false);
    setMenuVisible(true);
  }, [getRoomSelectionState, setMenuVisible]);
  const backRoomSelection = (0, _react.useCallback)(() => {
    setRoomSelection(false);
    setMenuVisible(true);
  }, [setMenuVisible]);
  const selectRoom = (0, _react.useCallback)(selectedRoom => {
    setRoom(selectedRoom);
    setRoomSelection(false);
    setVisible(true);
  }, []);
  const closeRoomSelection = (0, _react.useCallback)(() => {
    setRoom();
    setRoomSelection(false);
    setAction();
  }, [setAction]);
  return {
    back,
    backRoomSelection,
    close,
    closeRoomSelection,
    confirm,
    open,
    response,
    room,
    roomSelection,
    selectRoom,
    visible
  };
};
exports.useEditFlow = useEditFlow;
//# sourceMappingURL=useEditFlow.js.map