import { defineComponent, ref, watch, onUnmounted, createBlock, openBlock, unref, withCtx, createVNode } from "vue";
import { useUIKit, Badge, IconChat } from "@tencentcloud/uikit-base-component-vue3";
import { useBarrageState } from "tuikit-atomicx-vue3/live";
import { useRoomState } from "tuikit-atomicx-vue3/room";
import IconButton from "../base/IconButton.vue2.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "BarrageButton",
  props: {
    isChatOpen: { type: Boolean, default: false }
  },
  emits: ["click-icon"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { t } = useUIKit();
    const { currentRoom } = useRoomState();
    const { messageList } = useBarrageState();
    const unreadCount = ref(0);
    watch(
      () => {
        var _a;
        return (_a = messageList.value) == null ? void 0 : _a.length;
      },
      (newLength, oldLength) => {
        if (!newLength || newLength === 0) {
          return;
        }
        if (!props.isChatOpen && newLength > oldLength) {
          unreadCount.value += newLength - oldLength;
        }
      }
    );
    watch(
      () => props.isChatOpen,
      (isOpen) => {
        if (isOpen) {
          unreadCount.value = 0;
        }
      }
    );
    watch(
      () => {
        var _a;
        return (_a = currentRoom.value) == null ? void 0 : _a.roomId;
      },
      (roomId) => {
        if (!roomId) {
          return;
        }
        unreadCount.value = 0;
      }
    );
    onUnmounted(() => {
    });
    const handleClick = () => {
      unreadCount.value = 0;
      emit("click-icon");
    };
    return (_ctx, _cache) => {
      return openBlock(), createBlock(IconButton, {
        title: unref(t)("Chat.Title"),
        onClickIcon: handleClick
      }, {
        default: withCtx(() => [
          createVNode(unref(Badge), {
            value: unreadCount.value,
            hidden: !unreadCount.value
          }, {
            default: withCtx(() => [
              createVNode(unref(IconChat), { size: 24 })
            ]),
            _: 1
          }, 8, ["value", "hidden"])
        ]),
        _: 1
      }, 8, ["title"]);
    };
  }
});
export {
  _sfc_main as default
};
