"use strict";

import React, { useState } from 'react';
import Button from '../../styled/Button';
import { useSelector } from 'react-redux';
import { getActiveRoom } from '../../../roomStore';
import { useXmppClient } from '../../../context/xmppProvider';
import { ActionButton, CloseButton, GroupContainer, ModalBackground, ModalContainer, ModalTitle } from '../styledModalComponents';
import InputWithLabel from '../../styled/StyledInput';
import { getRoomByName, postAddRoomMember } from '../../../networking/api-requests/rooms.api';
import { addRoomViaApi } from '../../../roomStore/roomsSlice';
import { createRoomFromApi } from '../../../helpers/createRoomFromApi';
import { useChatSettingState } from '../../../hooks/useChatSettingState';
import { useAppDispatch } from '../../../hooks/hooks';
import { Text } from 'react-native';
import { useToast } from '../../../context/ToastContext';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const AddMembersModal = () => {
  const {
    config
  } = useChatSettingState();
  const activeRoom = useSelector(state => getActiveRoom(state));
  const dispatch = useAppDispatch();
  const {
    client
  } = useXmppClient();
  const {
    showToast
  } = useToast();
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [userName, setUserName] = 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 postAddRoomMember({
        chatName: activeRoom.jid.split('@')[0],
        members: [userName]
      });
      handleCloseModal();
      await client?.inviteRoomRequestStanza(userName, activeRoom.jid);
      const room = await getRoomByName(activeRoom.jid);
      const createdRoom = createRoomFromApi(room, config?.xmppSettings?.conference);
      if (createdRoom && client) {
        dispatch(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__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(ActionButton, {
      variant: "filled",
      unstyled: true,
      onPress: handleOpenModal,
      children: "Add members"
    }), isModalOpen && /*#__PURE__*/_jsx(ModalBackground, {
      children: /*#__PURE__*/_jsxs(ModalContainer, {
        children: [/*#__PURE__*/_jsx(CloseButton, {
          onPress: handleCloseModal,
          children: /*#__PURE__*/_jsx(Text, {
            style: {
              fontSize: 24
            },
            children: "\xD7"
          })
        }), /*#__PURE__*/_jsx(ModalTitle, {
          children: "Add New Member"
        }), /*#__PURE__*/_jsx(GroupContainer, {
          style: {
            flexDirection: 'column',
            position: 'relative',
            width: '100%'
          },
          children: /*#__PURE__*/_jsx(InputWithLabel, {
            style: {
              flex: 1
            },
            id: "userName",
            value: userName,
            onChange: handleUserNameChange,
            placeholder: "Enter User Id"
          })
        }), /*#__PURE__*/_jsxs(GroupContainer, {
          children: [/*#__PURE__*/_jsx(Button, {
            onPress: handleCloseModal,
            text: 'Cancel',
            style: {
              width: '100%'
            },
            unstyled: true,
            variant: "outlined"
          }), /*#__PURE__*/_jsx(Button, {
            onPress: handleAddUser,
            text: 'Add',
            style: {
              width: '100%'
            },
            unstyled: true,
            variant: "filled"
          })]
        })]
      })
    })]
  });
};
export default AddMembersModal;
//# sourceMappingURL=AddMembersModal.js.map