import { defineComponent, ref, onMounted, onUnmounted, openBlock, createElementBlock, normalizeClass, unref, createElementVNode, Fragment, renderList, createBlock, createCommentVNode } from "vue";
import "../../../../adapter-vue.mjs";
import { TUIStore, StoreName, TUIChatService } from "@tencentcloud/chat-uikit-engine";
import Icon from "../../../common/Icon.vue.mjs";
import faceIconLight from "../../../../assets/icon/face-light.svg.mjs";
import faceIconDark from "../../../../assets/icon/face-dark.svg.mjs";
import { EMOJI_TYPE } from "../../../../constant.mjs";
import { isPC, isUniFrameWork } from "../../../../utils/env.mjs";
import { isEnabledMessageReadReceiptGlobal } from "../../utils/utils.mjs";
import { EMOJI_GROUP_LIST, BASIC_EMOJI_URL_MAPPING, convertKeyToEmojiName } from "../../emoji-config/index.mjs";
import ChatConfig from "../../config.mjs";
const _hoisted_1 = ["onClick"];
const _hoisted_2 = ["src"];
const _hoisted_3 = ["src"];
const _hoisted_4 = ["src"];
const _hoisted_5 = { class: "emoji-picker-tab" };
const _hoisted_6 = ["onClick"];
const _hoisted_7 = ["src"];
const _hoisted_8 = ["src"];
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "emoji-picker-dialog",
  emits: ["insertEmoji", "onClose", "sendMessage"],
  setup(__props, { emit: __emit }) {
    var _a;
    const faceIcon = ChatConfig.getTheme() === "dark" ? faceIconDark : faceIconLight;
    const emits = __emit;
    const currentTabIndex = ref(0);
    const currentConversation = ref();
    const emojiPickerDialog = ref();
    const emojiPickerListRef = ref();
    const featureConfig = ChatConfig.getFeatureConfig();
    const list = ref(initEmojiList());
    const currentTabItem = ref(list == null ? void 0 : list.value[0]);
    const currentEmojiList = ref((_a = list == null ? void 0 : list.value[0]) == null ? void 0 : _a.list);
    onMounted(() => {
      TUIStore.watch(StoreName.CONV, {
        currentConversation: onCurrentConversationUpdate
      });
    });
    onUnmounted(() => {
      TUIStore.unwatch(StoreName.CONV, {
        currentConversation: onCurrentConversationUpdate
      });
    });
    const toggleEmojiTab = (index) => {
      var _a2;
      currentTabIndex.value = index;
      currentTabItem.value = list == null ? void 0 : list.value[index];
      currentEmojiList.value = (_a2 = list == null ? void 0 : list.value[index]) == null ? void 0 : _a2.list;
      if (!isUniFrameWork) {
        (emojiPickerListRef == null ? void 0 : emojiPickerListRef.value) && (emojiPickerListRef.value.scrollTop = 0);
      }
    };
    const select = (item, index) => {
      var _a2, _b, _c;
      const options = {
        emoji: { key: item, name: convertKeyToEmojiName(item) },
        type: (_a2 = currentTabItem == null ? void 0 : currentTabItem.value) == null ? void 0 : _a2.type
      };
      switch ((_b = currentTabItem == null ? void 0 : currentTabItem.value) == null ? void 0 : _b.type) {
        case EMOJI_TYPE.BASIC:
          options.url = ((_c = currentTabItem == null ? void 0 : currentTabItem.value) == null ? void 0 : _c.url) + BASIC_EMOJI_URL_MAPPING[item];
          if (isUniFrameWork) {
            uni.$emit("insert-emoji", options);
          } else {
            emits("insertEmoji", options);
          }
          break;
        case EMOJI_TYPE.BIG:
          sendFaceMessage(index, currentTabItem.value);
          break;
        case EMOJI_TYPE.CUSTOM:
          sendFaceMessage(index, currentTabItem.value);
          break;
      }
      isPC && emits("onClose");
    };
    const sendFaceMessage = (index, listItem) => {
      var _a2, _b, _c, _d, _e;
      const options = {
        to: ((_b = (_a2 = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _a2.groupProfile) == null ? void 0 : _b.groupID) || ((_d = (_c = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _c.userProfile) == null ? void 0 : _d.userID),
        conversationType: (_e = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _e.type,
        payload: {
          index: listItem.emojiGroupID,
          data: listItem.list[index]
        },
        needReadReceipt: isEnabledMessageReadReceiptGlobal()
      };
      TUIChatService.sendFaceMessage(options);
    };
    function sendMessage() {
      uni.$emit("send-message-in-emoji-picker");
    }
    function onCurrentConversationUpdate(conversation) {
      currentConversation.value = conversation;
    }
    function initEmojiList() {
      return EMOJI_GROUP_LIST.filter((item) => {
        if (item.type === EMOJI_TYPE.BASIC) {
          return featureConfig.InputEmoji;
        }
        if (item.type === EMOJI_TYPE.BIG) {
          return featureConfig.InputStickers;
        }
      });
    }
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        ref_key: "emojiPickerDialog",
        ref: emojiPickerDialog,
        class: normalizeClass({
          "emoji-picker": true,
          "emoji-picker-h5": !unref(isPC)
        })
      }, [
        createElementVNode("ul", {
          ref_key: "emojiPickerListRef",
          ref: emojiPickerListRef,
          class: normalizeClass(["emoji-picker-list", !unref(isPC) && "emoji-picker-h5-list"])
        }, [
          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(currentEmojiList), (childrenItem, childrenIndex) => {
            return openBlock(), createElementBlock("li", {
              key: childrenIndex,
              class: "emoji-picker-list-item",
              onClick: ($event) => select(childrenItem, childrenIndex)
            }, [
              unref(currentTabItem).type === unref(EMOJI_TYPE).BASIC ? (openBlock(), createElementBlock("img", {
                key: 0,
                class: "emoji",
                src: unref(currentTabItem).url + unref(BASIC_EMOJI_URL_MAPPING)[childrenItem]
              }, null, 8, _hoisted_2)) : unref(currentTabItem).type === unref(EMOJI_TYPE).BIG ? (openBlock(), createElementBlock("img", {
                key: 1,
                class: "emoji-big",
                src: unref(currentTabItem).url + childrenItem + "@2x.png"
              }, null, 8, _hoisted_3)) : (openBlock(), createElementBlock("img", {
                key: 2,
                class: "emoji-custom emoji-big",
                src: unref(currentTabItem).url + childrenItem
              }, null, 8, _hoisted_4))
            ], 8, _hoisted_1);
          }), 128))
        ], 2),
        createElementVNode("ul", _hoisted_5, [
          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(list), (item, index) => {
            return openBlock(), createElementBlock("li", {
              key: index,
              class: "emoji-picker-tab-item",
              onClick: ($event) => toggleEmojiTab(index)
            }, [
              item.type === unref(EMOJI_TYPE).BASIC ? (openBlock(), createBlock(Icon, {
                key: 0,
                class: "icon",
                file: unref(faceIcon)
              }, null, 8, ["file"])) : item.type === unref(EMOJI_TYPE).BIG ? (openBlock(), createElementBlock("img", {
                key: 1,
                class: "icon-big",
                src: item.url + item.list[0] + "@2x.png"
              }, null, 8, _hoisted_7)) : (openBlock(), createElementBlock("img", {
                key: 2,
                class: "icon-custom icon-big",
                src: item.url + item.list[0]
              }, null, 8, _hoisted_8))
            ], 8, _hoisted_6);
          }), 128)),
          unref(isUniFrameWork) ? (openBlock(), createElementBlock("li", {
            key: 0,
            class: "send-btn",
            onClick: sendMessage
          }, " 发送 ")) : createCommentVNode("", true)
        ])
      ], 2);
    };
  }
});
export {
  _sfc_main as default
};
