"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSelectedRoomData = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _react = require("react");
var _Occupation = require("../../../__shared__/Occupation/Occupation.constants");
var _helpers = require("../../helpers");
const EMPTY_ARRAY = [];
const useSelectedRoomData = function () {
  var _storeValue$occupatio;
  let {
    details = EMPTY_ARRAY,
    room,
    rooms = EMPTY_ARRAY
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: storeValue = {}
  } = (0, _dataSources.useStore)() || {};
  const configDataSource = Array.isArray(storeValue === null || storeValue === void 0 ? void 0 : (_storeValue$occupatio = storeValue.occupation) === null || _storeValue$occupatio === void 0 ? void 0 : _storeValue$occupatio.dataSource) ? storeValue.occupation.dataSource : _Occupation.DATASOURCE;
  const roomId = room === null || room === void 0 ? void 0 : room.id;
  const selectedRoom = (0, _react.useMemo)(() => {
    if (roomId === undefined || roomId === null) return undefined;
    return rooms.find(function () {
      let {
        id
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return id === roomId;
    });
  }, [roomId, rooms]);
  const roomDataSource = (0, _react.useMemo)(() => (0, _helpers.getRoomDataSource)({
    configDataSource,
    details,
    occupation: selectedRoom ? [selectedRoom] : EMPTY_ARRAY,
    translate
  }), [configDataSource, details, selectedRoom, translate]);
  const totalRooms = (0, _react.useMemo)(() => (0, _helpers.getDetailsByRoom)(details).length, [details]);
  const selectedRoomData = (0, _react.useMemo)(() => roomDataSource.find(function () {
    let {
      room: roomValue
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return (roomValue === null || roomValue === void 0 ? void 0 : roomValue.id) === (room === null || room === void 0 ? void 0 : room.id) && ((room === null || room === void 0 ? void 0 : room.index) === undefined || (room === null || room === void 0 ? void 0 : room.index) === null || (roomValue === null || roomValue === void 0 ? void 0 : roomValue.index) === (room === null || room === void 0 ? void 0 : room.index));
  }), [room, roomDataSource]);
  const showSelectedRoom = totalRooms > 1 && selectedRoomData !== undefined;
  return {
    configDataSource,
    roomDataSource,
    selectedRoom,
    selectedRoomData,
    showSelectedRoom
  };
};
exports.useSelectedRoomData = useSelectedRoomData;
//# sourceMappingURL=useSelectedRoomData.js.map