"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Button = _interopRequireDefault(require("../../styled/Button"));
var _reactRedux = require("react-redux");
var _roomStore = require("../../../roomStore");
var _xmppProvider = require("../../../context/xmppProvider");
var _styledModalComponents = require("../styledModalComponents");
var _StyledInput = _interopRequireDefault(require("../../styled/StyledInput"));
var _rooms = require("../../../networking/api-requests/rooms.api");
var _roomsSlice = require("../../../roomStore/roomsSlice");
var _createRoomFromApi = require("../../../helpers/createRoomFromApi");
var _useChatSettingState = require("../../../hooks/useChatSettingState");
var _hooks = require("../../../hooks/hooks");
var _reactNative = require("react-native");
var _ToastContext = require("../../../context/ToastContext");
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 AddMembersModal = () => {
  const {
    config
  } = (0, _useChatSettingState.useChatSettingState)();
  const activeRoom = (0, _reactRedux.useSelector)(state => (0, _roomStore.getActiveRoom)(state));
  const dispatch = (0, _hooks.useAppDispatch)();
  const {
    client
  } = (0, _xmppProvider.useXmppClient)();
  const {
    showToast
  } = (0, _ToastContext.useToast)();
  const [isModalOpen, setIsModalOpen] = (0, _react.useState)(false);
  const [userName, setUserName] = (0, _react.useState)('');
  const handleOpenModal = () => setIsModalOpen(true);
  const handleCloseModal = () => setIsModalOpen(false);
  const validateRoomName = name => {
    if (name.trim().length < 3) {
      return 'Room name must be at least 3 characters.';
    }
    return '';
  };
  const handleAddUser = async () => {
    try {
      if (!activeRoom) {
        return;
      }
      await (0, _rooms.postAddRoomMember)({
        chatName: activeRoom.jid.split('@')[0],
        members: [userName]
      });
      handleCloseModal();
      await client?.inviteRoomRequestStanza(userName, activeRoom.jid);
      const room = await (0, _rooms.getRoomByName)(activeRoom.jid);
      const createdRoom = (0, _createRoomFromApi.createRoomFromApi)(room, config?.xmppSettings?.conference);
      if (createdRoom && client) {
        dispatch((0, _roomsSlice.addRoomViaApi)({
          room: createdRoom,
          xmpp: client
        }));
      }
      showToast({
        id: Date.now().toString(),
        title: 'Success',
        message: `${userName} has been added to the room.`,
        type: 'success'
      });
    } catch (error) {
      console.error('Failed to add user:', error);
      showToast({
        id: Date.now().toString(),
        title: 'Error',
        message: 'Failed to add user.',
        type: 'error'
      });
    }
  };
  const handleUserNameChange = text => {
    setUserName(text);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.ActionButton, {
      variant: "filled",
      unstyled: true,
      onPress: handleOpenModal,
      children: "Add members"
    }), isModalOpen && /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.ModalBackground, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_styledModalComponents.ModalContainer, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.CloseButton, {
          onPress: handleCloseModal,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: {
              fontSize: 24
            },
            children: "\xD7"
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.ModalTitle, {
          children: "Add New Member"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.GroupContainer, {
          style: {
            flexDirection: 'column',
            position: 'relative',
            width: '100%'
          },
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledInput.default, {
            style: {
              flex: 1
            },
            id: "userName",
            value: userName,
            onChange: handleUserNameChange,
            placeholder: "Enter User Id"
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_styledModalComponents.GroupContainer, {
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
            onPress: handleCloseModal,
            text: 'Cancel',
            style: {
              width: '100%'
            },
            unstyled: true,
            variant: "outlined"
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
            onPress: handleAddUser,
            text: 'Add',
            style: {
              width: '100%'
            },
            unstyled: true,
            variant: "filled"
          })]
        })]
      })
    })]
  });
};
var _default = exports.default = AddMembersModal;
//# sourceMappingURL=AddMembersModal.js.map