"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledModalComponents = require("../Modals/styledModalComponents");
var _StyledComponents = require("./StyledComponents");
var _Checkbox = _interopRequireDefault(require("./Checkbox"));
var _debounce = require("../../helpers/debounce");
var _StyledInputComponents = require("../styled/StyledInputComponents/StyledInputComponents");
var _useRoomState = require("../../hooks/useRoomState");
var _reactNative = require("react-native");
var _jsxRuntime = require("react/jsx-runtime");
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); }
const UsersList = ({
  style,
  selectedUsers,
  setSelectedUsers,
  headerElement,
  filter
}) => {
  const {
    usersSet
  } = (0, _useRoomState.useRoomState)();
  const [searchTerm, setSearchTerm] = (0, _react.useState)('');
  const [filteredUsers, setFilteredUsers] = (0, _react.useState)([]);
  const handleUserSelect = user => {
    setSelectedUsers(prev => {
      const isSelected = prev.some(u => u._id === user._id);
      return isSelected ? prev.filter(u => u._id !== user._id) : [...prev, user];
    });
  };
  const debouncedFilter = (0, _react.useMemo)(() => (0, _debounce.debounce)(term => {
    const lower = term.toLowerCase();
    const users = Object.values(usersSet).filter(user => `${user.firstName} ${user.lastName}`.toLowerCase().includes(lower));
    setFilteredUsers(users);
  }, 100), [usersSet]);
  (0, _react.useEffect)(() => {
    debouncedFilter(searchTerm);
  }, [searchTerm, debouncedFilter]);
  (0, _react.useEffect)(() => {
    setFilteredUsers(Object.values(usersSet));
  }, [usersSet]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: {
      maxHeight: '100px',
      ...style
    },
    children: [headerElement ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.ModalTitle, {
      children: "Select Users (max 20)"
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
      style: {
        fontSize: 14,
        fontWeight: '600'
      },
      children: "Select Users (max 20)"
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledInputComponents.StyledInput, {
      placeholder: "Search users...",
      value: searchTerm,
      onChangeText: setSearchTerm,
      style: {
        width: '100%'
      }
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledComponents.ScrollableContainer, {
      style: {
        ...style
      },
      children: filteredUsers.map(user => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_StyledComponents.UserItem, {
        onPress: () => handleUserSelect(user),
        style: {
          cursor: 'pointer'
        },
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Checkbox.default, {
          value: selectedUsers.some(u => u._id === user._id),
          onValueChange: () => handleUserSelect(user),
          disabled: selectedUsers.length === 20
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledComponents.Label, {
            children: `${user.firstName} ${user.lastName}`
          })
        })]
      }, user._id))
    })]
  });
};
var _default = exports.default = UsersList;
//# sourceMappingURL=UsersList.js.map