"use strict";

import React, { useState, useMemo } from 'react';
import Button from '../../styled/Button';
import { AddNewIcon } from '../../../assets/icons';
import { useDispatch, useSelector } from 'react-redux';
import { useXmppClient } from '../../../context/xmppProvider';
import { uploadFile } from '../../../networking/api-requests/auth.api';
import { createRoomFromApi } from '../../../helpers/createRoomFromApi';
import { postRoom } from '../../../networking/api-requests/rooms.api';
import ModalContent from './ModalContent';
import { ModalBackground } from '../styledModalComponents';
import { addRoom, setCurrentRoom } from '../../../roomStore/roomsSlice';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const NewChatModal = () => {
  const config = useSelector(state => state.chatSettingStore.config);
  const dispatch = useDispatch();
  const {
    client
  } = useXmppClient();
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [activeTab, setActiveTab] = useState('0');
  const [roomName, setRoomName] = useState('');
  const [roomDescription, setRoomDescription] = useState('');
  const [chatType, setChatType] = useState({
    name: 'Public',
    id: 'public'
  });
  const [profileImage, setProfileImage] = useState(null);
  const [errors, setErrors] = useState({
    name: '',
    description: ''
  });
  const [selectedUsers, setSelectedUsers] = useState([]);
  const options = [{
    name: 'Public',
    id: 'public'
  }, {
    name: 'Members-only',
    id: 'group'
  }];
  const isValid = useMemo(() => roomName.length >= 3, [roomName]);
  const handleOpenModal = () => setIsModalOpen(true);
  const handleCloseModal = () => {
    setActiveTab('0');
    setIsModalOpen(false);
    setRoomName('');
    setSelectedUsers([]);
  };
  const validateRoomName = name => {
    if (name.trim().length < 3) {
      return 'Room name must be at least 3 characters.';
    }
    return '';
  };
  const handleRoomNameChange = e => {
    const name = e;
    setRoomName(name);
    setErrors(prevErrors => ({
      ...prevErrors,
      name: validateRoomName(name)
    }));
  };
  const handleCreateRoom = async () => {
    if (isValid) {
      let mediaData = new FormData();
      if (profileImage) mediaData.append('files', profileImage);
      const uploadResult = await uploadFile(mediaData);
      const location = uploadResult?.data?.results?.[0]?.location;
      const namesArray = selectedUsers.map(user => user.xmppUsername);
      const newChat = await postRoom({
        title: roomName,
        description: roomDescription || 'No description',
        picture: location || '',
        type: chatType.id || 'public',
        members: namesArray
      });

      // Create room
      const normalizedChat = createRoomFromApi(newChat, config?.xmppSettings?.conference, namesArray.length);
      if (!normalizedChat) return;
      dispatch(addRoom({
        roomData: normalizedChat
      }));
      dispatch(setCurrentRoom({
        roomJID: normalizedChat.jid
      }));
      client?.presenceInRoomStanza(normalizedChat.jid);
    }
  };
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(Button, {
      style: {
        padding: 8,
        borderRadius: 16,
        backgroundColor: 'transparent'
      },
      unstyled: true,
      EndIcon: /*#__PURE__*/_jsx(AddNewIcon, {
        color: config?.colors?.primary
      }),
      onPress: handleOpenModal
    }), isModalOpen && /*#__PURE__*/_jsx(ModalBackground, {
      children: /*#__PURE__*/_jsx(ModalContent, {
        activeTab: activeTab,
        roomName: roomName,
        roomDescription: roomDescription,
        chatType: chatType,
        profileImage: profileImage,
        setActiveTab: setActiveTab,
        handleRoomNameChange: handleRoomNameChange,
        setRoomDescription: setRoomDescription,
        setChatType: setChatType,
        setProfileImage: setProfileImage,
        selectedUsers: selectedUsers,
        setSelectedUsers: setSelectedUsers,
        handleCreateRoom: handleCreateRoom,
        handleCloseModal: handleCloseModal,
        errors: errors,
        setErrors: setErrors,
        options: options
      })
    })]
  });
};
export default NewChatModal;
//# sourceMappingURL=UpdNewChatModal.js.map