"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _StyledComponents = require("../styled/StyledComponents");
var _RoomList = _interopRequireDefault(require("./RoomList"));
var _ProfileImagePlaceholder = require("./ProfileImagePlaceholder");
var _Button = _interopRequireDefault(require("../styled/Button"));
var _icons = require("../../assets/icons");
var _reactRedux = require("react-redux");
var _Composing = _interopRequireDefault(require("../styled/StyledInputComponents/Composing"));
var _roomsSlice = require("../../roomStore/roomsSlice");
var _xmppProvider = require("../../context/xmppProvider");
var _chatSettingsSlice = require("../../roomStore/chatSettingsSlice");
var _MODAL_TYPES = require("../../helpers/constants/MODAL_TYPES");
var _MenuRoom = require("../MenuRoom/MenuRoom");
var _useRoomState = require("../../hooks/useRoomState");
var _useChatSettingState = require("../../hooks/useChatSettingState");
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); }
/** @format */

const ChatHeader = ({
  currentRoom,
  handleBackClick
}) => {
  const dispatch = (0, _reactRedux.useDispatch)();
  const {
    client
  } = (0, _xmppProvider.useXmppClient)();
  const {
    roomsList,
    activeRoomJID
  } = (0, _useRoomState.useRoomState)(currentRoom.jid);
  const roomState = (0, _useRoomState.useRoomState)(currentRoom.jid).room;
  const composing = roomState?.composing;
  const {
    config
  } = (0, _useChatSettingState.useChatSettingState)();
  const handleChangeChat = chat => {
    dispatch((0, _roomsSlice.setCurrentRoom)({
      roomJID: chat.jid
    }));
    dispatch((0, _roomsSlice.setIsLoading)({
      chatJID: chat.jid,
      loading: true
    }));
  };
  const handleLeaveClick = (0, _react.useCallback)(() => {
    client?.leaveTheRoomStanza(activeRoomJID);
    dispatch((0, _roomsSlice.deleteRoom)({
      jid: activeRoomJID
    }));
    const nextRoomJID = Object.keys(roomsList)[0] || null;
    if (nextRoomJID) {
      dispatch((0, _roomsSlice.setCurrentRoom)({
        roomJID: nextRoomJID
      }));
    }
  }, [activeRoomJID, roomsList, dispatch, client]);
  const handleHeaderChatMenu = () => {
    _reactNative.Keyboard.dismiss();
    config?.headerChatMenu && config?.headerChatMenu();
  };

  // Shared open-chat-info handler used by BOTH the outer touchable row
  // and the avatar's own press (the avatar otherwise absorbs taps without
  // bubbling — see the comment on ProfileImagePlaceholder below).
  const openChatInfo = (0, _react.useCallback)(() => dispatch((0, _chatSettingsSlice.setActiveModal)(_MODAL_TYPES.MODAL_TYPES.CHAT_PROFILE)), [dispatch]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_StyledComponents.ChatContainerHeader, {
      children: [handleBackClick && !config?.headerChatMenu ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.leftContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
          EndIcon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.BackIcon, {}),
          onPress: () => handleBackClick(false)
        })
      }) : !config?.disableChatHeaderBurgerMenuIcon ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.leftContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
          style: styles.menuButton,
          color: "black",
          unstyled: true,
          EndIcon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.BurgerMenuIcon, {
            color: config?.colors?.primary
          }),
          onPress: handleHeaderChatMenu
        })
      }) : null
      /* Don't reserve the 15%-wide leftContainer when both the back
       * button is absent AND the burger icon is hidden — the empty
       * slot used to steal width from the title row, clipping the chat
       * name. Customer-reported #15. The right action area still fills
       * its own slot, so the title now starts at the left edge as
       * intended by `disableChatHeaderBurgerMenuIcon`. */, /*#__PURE__*/(0, _jsxRuntime.jsxs)(_StyledComponents.CenterContainer, {
        rightSpace: config?.disableRoomConfig,
        leftSpace: !!config?.headerChatMenu,
        children: [config?.chatHeaderBurgerMenu && roomsList && /*#__PURE__*/(0, _jsxRuntime.jsx)(_RoomList.default, {
          chats: Object.values(roomsList),
          burgerMenu: true,
          onRoomClick: handleChangeChat
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_StyledComponents.ChatContainerHeaderBoxInfo, {
          onPress: openChatInfo
          // Gates the entry to the CHAT-INFO modal (room name, members,
          // settings) via the dedicated `disableChatInfo.disableChatHeaderMenu`
          // flag. Previously wired to `disableProfilesInteractions`, which
          // is for USER-profile popups (the in-bubble avatar tap, see
          // Message.tsx) — wrong semantic gate, and it meant a consumer
          // who wanted to hide user profiles also lost their entry to
          // the chat info screen.
          ,
          disabled: config?.disableChatInfo?.disableChatHeaderMenu,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_ProfileImagePlaceholder.ProfileImagePlaceholder, {
            name: currentRoom?.title || currentRoom?.name,
            size: 40,
            icon: currentRoom?.icon,
            active: !config?.disableChatInfo?.disableChatHeaderMenu,
            click: config?.disableChatInfo?.disableChatHeaderMenu ? undefined : {
              isClick: true,
              onPress: openChatInfo
            }
          }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_StyledComponents.ChatContainerHeaderInfo, {
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledComponents.ChatContainerHeaderLabel, {
              numberOfLines: 1,
              ellipsizeMode: "tail",
              children: currentRoom?.title || currentRoom?.name
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
              children: composing ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Composing.default, {
                usersTyping: currentRoom?.composingList
              }) : config?.disableUserCount ? undefined : /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledComponents.ChatContainerHeaderLabel, {
                style: styles.subLabel,
                children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
                  children: `${currentRoom?.usersCnt} users`
                })
              })
            })]
          })]
        })]
      }), !config?.disableRoomConfig ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.rightContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_MenuRoom.RoomMenu, {
          handleLeaveClick: handleLeaveClick
        })
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.rightContainer
      })]
    }), config?.chatHeaderAdditional?.enabled && config.chatHeaderAdditional.element()]
  });
};
const styles = _reactNative.StyleSheet.create({
  subLabel: {
    color: '#8C8C8C',
    fontSize: 14
  },
  menuButton: {
    padding: 8,
    borderRadius: 16,
    backgroundColor: 'transparent'
  },
  leftContainer: {
    alignItems: 'flex-start',
    width: '15%'
  },
  rightContainer: {
    alignItems: 'flex-end',
    width: '15%'
  }
});
var _default = exports.default = ChatHeader;
//# sourceMappingURL=ChatHeader.js.map