"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Button = _interopRequireDefault(require("../../styled/Button"));
var _icons = require("../../../assets/icons");
var _reactRedux = require("react-redux");
var _xmppProvider = require("../../../context/xmppProvider");
var _auth = require("../../../networking/api-requests/auth.api");
var _createRoomFromApi = require("../../../helpers/createRoomFromApi");
var _rooms = require("../../../networking/api-requests/rooms.api");
var _ModalContent = _interopRequireDefault(require("./ModalContent"));
var _styledModalComponents = require("../styledModalComponents");
var _roomsSlice = require("../../../roomStore/roomsSlice");
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 NewChatModal = () => {
  const config = (0, _reactRedux.useSelector)(state => state.chatSettingStore.config);
  const dispatch = (0, _reactRedux.useDispatch)();
  const {
    client
  } = (0, _xmppProvider.useXmppClient)();
  const [isModalOpen, setIsModalOpen] = (0, _react.useState)(false);
  const [activeTab, setActiveTab] = (0, _react.useState)('0');
  const [roomName, setRoomName] = (0, _react.useState)('');
  const [roomDescription, setRoomDescription] = (0, _react.useState)('');
  const [chatType, setChatType] = (0, _react.useState)({
    name: 'Public',
    id: 'public'
  });
  const [profileImage, setProfileImage] = (0, _react.useState)(null);
  const [errors, setErrors] = (0, _react.useState)({
    name: '',
    description: ''
  });
  const [selectedUsers, setSelectedUsers] = (0, _react.useState)([]);
  const options = [{
    name: 'Public',
    id: 'public'
  }, {
    name: 'Members-only',
    id: 'group'
  }];
  const isValid = (0, _react.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 (0, _auth.uploadFile)(mediaData);
      const location = uploadResult?.data?.results?.[0]?.location;
      const namesArray = selectedUsers.map(user => user.xmppUsername);
      const newChat = await (0, _rooms.postRoom)({
        title: roomName,
        description: roomDescription || 'No description',
        picture: location || '',
        type: chatType.id || 'public',
        members: namesArray
      });

      // Create room
      const normalizedChat = (0, _createRoomFromApi.createRoomFromApi)(newChat, config?.xmppSettings?.conference, namesArray.length);
      if (!normalizedChat) return;
      dispatch((0, _roomsSlice.addRoom)({
        roomData: normalizedChat
      }));
      dispatch((0, _roomsSlice.setCurrentRoom)({
        roomJID: normalizedChat.jid
      }));
      client?.presenceInRoomStanza(normalizedChat.jid);
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
      style: {
        padding: 8,
        borderRadius: 16,
        backgroundColor: 'transparent'
      },
      unstyled: true,
      EndIcon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.AddNewIcon, {
        color: config?.colors?.primary
      }),
      onPress: handleOpenModal
    }), isModalOpen && /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.ModalBackground, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ModalContent.default, {
        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
      })
    })]
  });
};
var _default = exports.default = NewChatModal;
//# sourceMappingURL=UpdNewChatModal.js.map