"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const TUIChatEngine = require("@tencentcloud/chat-uikit-engine");
const universalApi = require("@tencentcloud/universal-api");
const env = require("../../../../utils/env.js");
let currentConversationID = "";
let memberList = [];
let allMemberList = [];
let showMemberList = [];
let selectedIndex = 0;
let isGroup = false;
let command;
const all = {
  userID: TUIChatEngine.TYPES.MSG_AT_ALL,
  nick: "所有人",
  isAll: true,
  avatar: "https://web.sdk.qcloud.com/im/assets/images/at.svg"
};
TUIChatEngine.TUIStore.watch(TUIChatEngine.StoreName.CONV, {
  currentConversationID: (id) => {
    if (id !== currentConversationID) {
      currentConversationID = id;
      if (currentConversationID == null ? void 0 : currentConversationID.startsWith("GROUP")) {
        isGroup = true;
      } else {
        isGroup = false;
        memberList = [];
        allMemberList = [];
        showMemberList = [];
      }
    }
  }
});
TUIChatEngine.TUIStore.watch(TUIChatEngine.StoreName.CUSTOM, {
  memberList: (list) => {
    if (isGroup && Array.isArray(list)) {
      memberList = list;
      allMemberList = [all, ...memberList];
      showMemberList = allMemberList;
    }
  }
});
const MessageInputAtSuggestion = () => {
  return {
    allowedPrefixes: null,
    items: (props) => {
      if (!isGroup) {
        return;
      }
      const queryResult = allMemberList == null ? void 0 : allMemberList.filter(
        (item) => {
          var _a, _b, _c, _d, _e, _f;
          return ((_c = (_a = item == null ? void 0 : item.nick) == null ? void 0 : _a.toLowerCase()) == null ? void 0 : _c.startsWith((_b = props == null ? void 0 : props.query) == null ? void 0 : _b.toLowerCase())) || ((_f = (_d = item == null ? void 0 : item.userID) == null ? void 0 : _d.toLowerCase()) == null ? void 0 : _f.startsWith((_e = props == null ? void 0 : props.query) == null ? void 0 : _e.toLowerCase()));
        }
      );
      showMemberList = (queryResult == null ? void 0 : queryResult.length) ? queryResult : allMemberList;
      universalApi.TUIGlobal.setShowMemberList(showMemberList);
      return showMemberList;
    },
    render: () => {
      return {
        onStart: (props) => {
          if (!isGroup) {
            return;
          }
          universalApi.TUIGlobal.toggleAtList(true);
          if (!(props == null ? void 0 : props.clientRect)) {
            return;
          }
          const rect = props == null ? void 0 : props.clientRect();
          if ((rect == null ? void 0 : rect.left) && (rect == null ? void 0 : rect.top) && !env.isH5) {
            universalApi.TUIGlobal.handleAtListPosition({
              left: rect == null ? void 0 : rect.left,
              top: rect == null ? void 0 : rect.top
            });
          }
          command = props.command;
        },
        onUpdate(props) {
          if (!isGroup) {
            return;
          }
          if (!(props == null ? void 0 : props.clientRect)) {
            return;
          }
          const rect = props == null ? void 0 : props.clientRect();
          if ((rect == null ? void 0 : rect.left) && (rect == null ? void 0 : rect.top) && !env.isH5) {
            universalApi.TUIGlobal.handleAtListPosition({
              left: rect == null ? void 0 : rect.left,
              top: rect == null ? void 0 : rect.top
            });
          }
        },
        onKeyDown(props) {
          var _a, _b;
          if (!isGroup) {
            return;
          }
          if (props.event.key === "Enter") {
            (_a = props.event) == null ? void 0 : _a.stopPropagation();
            (_b = props.event) == null ? void 0 : _b.preventDefault();
          }
          if (props.event.key === "Escape") {
            universalApi.TUIGlobal.toggleAtList(false);
            showMemberList = allMemberList;
            return true;
          }
          if ((props == null ? void 0 : props.event.key) === "ArrowUp") {
            upHandler();
            return true;
          }
          if ((props == null ? void 0 : props.event.key) === "ArrowDown") {
            downHandler();
            return true;
          }
          if ((props == null ? void 0 : props.event.key) === "Enter") {
            enterHandler();
            return true;
          }
          return false;
        },
        onExit() {
          if (!isGroup) {
            return;
          }
          universalApi.TUIGlobal.toggleAtList(false);
          showMemberList = allMemberList;
          universalApi.TUIGlobal.handleAtListPosition({
            left: 0,
            top: 0
          });
        }
      };
    }
  };
};
const upHandler = () => {
  if (!(showMemberList == null ? void 0 : showMemberList.length)) return;
  selectedIndex = (selectedIndex + (showMemberList == null ? void 0 : showMemberList.length) - 1) % (showMemberList == null ? void 0 : showMemberList.length);
  universalApi.TUIGlobal.setCurrentSelectIndex(selectedIndex);
};
const downHandler = () => {
  if (!(showMemberList == null ? void 0 : showMemberList.length)) return;
  selectedIndex = (selectedIndex + 1) % (showMemberList == null ? void 0 : showMemberList.length);
  universalApi.TUIGlobal.setCurrentSelectIndex(selectedIndex);
};
const enterHandler = () => {
  selectItem(selectedIndex);
};
const selectItem = (index) => {
  if (!(showMemberList == null ? void 0 : showMemberList.length)) return;
  const item = showMemberList[index];
  if (item) {
    command && command({
      id: item == null ? void 0 : item.userID,
      label: (item == null ? void 0 : item.nick) || (item == null ? void 0 : item.userID)
    });
  }
};
universalApi.TUIGlobal.selectItem = selectItem;
const MessageInputAtSuggestion$1 = MessageInputAtSuggestion;
exports.default = MessageInputAtSuggestion$1;
