"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useEditFlow = void 0;
var _react = require("react");
var _helpers = require("../../helpers");
const useEditFlow = _ref => {
  let {
    confirmAction,
    dataSource,
    modifyKey,
    setAction,
    setMenuVisible
  } = _ref;
  const [response, setResponse] = (0, _react.useState)();
  const [room, setRoom] = (0, _react.useState)();
  const [roomSelection, setRoomSelection] = (0, _react.useState)(false);
  const [visible, setVisible] = (0, _react.useState)(false);
  const getRoomSelectionState = (0, _react.useCallback)(() => {
    const {
      details,
      modify: {
        [modifyKey]: {
          rooms = []
        } = {}
      } = {}
    } = dataSource || {};
    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 {
      fromConfirm = false,
      room: selectedRoom
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    const {
      selectableRooms,
      showRoomSelection: hasRoomSelection
    } = getRoomSelectionState();
    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 {
      showRoomSelection
    } = getRoomSelectionState();
    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