"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactRedux = require("react-redux");
var _roomStore = require("../../../roomStore");
var _styledModalComponents = require("../styledModalComponents");
var _Button = _interopRequireDefault(require("../../styled/Button"));
var _UsersList = _interopRequireDefault(require("../../UsersList/UsersList"));
var _Loader = _interopRequireDefault(require("../../styled/Loader"));
var _rooms = require("../../../networking/api-requests/rooms.api");
var _ToastContext = require("../../../context/ToastContext");
var _roomsSlice = require("../../../roomStore/roomsSlice");
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 SelectUsersModal = () => {
  const {
    showToast
  } = (0, _ToastContext.useToast)();
  const dispatch = (0, _reactRedux.useDispatch)();
  const [loading, setIsLoading] = (0, _react.useState)(false);
  const [isModalOpen, setIsModalOpen] = (0, _react.useState)(false);
  const [selectedUsers, setSelectedUsers] = (0, _react.useState)([]);
  const activeRoom = (0, _reactRedux.useSelector)(state => (0, _roomStore.getActiveRoom)(state));
  const handleOpenModal = () => setIsModalOpen(true);
  const handleCloseModal = () => {
    setIsModalOpen(false);
    setSelectedUsers([]);
  };
  const handleAdd = async () => {
    setIsLoading(true);
    const existingXmppUsernames = activeRoom?.members?.map(member => member.xmppUsername);
    const usersArray = selectedUsers.filter(user => !existingXmppUsernames?.includes(user.xmppUsername)).map(user => user.xmppUsername);
    try {
      const newMembers = await (0, _rooms.postAddRoomMember)({
        chatName: activeRoom?.jid?.split('@')[0] || '',
        members: usersArray
      });
      dispatch((0, _roomsSlice.updateRoom)({
        jid: activeRoom?.jid || '',
        updates: {
          members: [...newMembers, ...(activeRoom?.members || [])],
          usersCnt: (activeRoom?.members?.length || 0) + newMembers.length
        }
      }));
      showToast({
        id: Date.now().toString(),
        title: 'Success!',
        message: 'Room created succusfully!',
        type: 'success',
        duration: 3000
      });
    } catch (error) {
      console.error('Error adding users:', error);
      setIsLoading(false);
    }
    handleCloseModal();
    setIsLoading(false);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.ActionButton, {
      variant: "filled",
      unstyled: true,
      onPress: handleOpenModal,
      children: "Add more Users"
    }), isModalOpen && /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.ModalBackground, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_styledModalComponents.ModalContainer, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_styledModalComponents.CloseButton, {
          onPress: handleCloseModal,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: {
              fontSize: 24
            },
            children: "\xD7"
          }), "\xD7"]
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.GroupContainer, {
          style: {
            flexDirection: 'column',
            position: 'relative',
            width: '100%',
            minHeight: 400
          },
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_UsersList.default, {
            selectedUsers: selectedUsers,
            setSelectedUsers: setSelectedUsers,
            style: {
              minWidth: '100%',
              width: '100%',
              maxHeight: '340px'
            },
            headerElement: false
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_styledModalComponents.GroupContainer, {
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
            onPress: handleCloseModal,
            text: "Cancel",
            style: {
              width: '100%'
            },
            unstyled: true,
            variant: "outlined"
          }), loading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Loader.default, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
            onPress: handleAdd,
            text: "Add",
            style: {
              width: '100%'
            },
            unstyled: true,
            variant: "filled"
          })]
        })]
      })
    })]
  });
};
var _default = exports.default = SelectUsersModal;
//# sourceMappingURL=SelectUsersModal.js.map