import { defineComponent, ref, computed, watch, createElementBlock, openBlock, createVNode, unref, withCtx, createElementVNode, normalizeClass, toDisplayString } from "vue";
import { useUIKit } from "@tencentcloud/uikit-base-component-vue3";
import { BarrageList, BarrageInput } from "tuikit-atomicx-vue3/live";
import { useRoomState, useRoomParticipantState } from "tuikit-atomicx-vue3/room";
const _hoisted_1 = { class: "room-chat" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "RoomBarrage",
  props: {
    isChatOpen: { type: Boolean, default: false }
  },
  setup(__props) {
    const props = __props;
    const barrageListRef = ref(null);
    const { t } = useUIKit();
    const { currentRoom } = useRoomState();
    const { localParticipant, adminList, participantList } = useRoomParticipantState();
    const isAdmin = (userId) => {
      var _a;
      return (_a = adminList.value) == null ? void 0 : _a.some((admin) => admin.userId === userId);
    };
    const isOwner = (userId) => {
      var _a, _b;
      return ((_b = (_a = currentRoom.value) == null ? void 0 : _a.roomOwner) == null ? void 0 : _b.userId) === userId;
    };
    const isParticipant = (userId) => {
      var _a;
      return (_a = participantList.value) == null ? void 0 : _a.some((participant) => participant.userId === userId);
    };
    const getRoleClass = (userId) => {
      if (isOwner(userId)) {
        return "user-badge-owner";
      }
      if (isAdmin(userId)) {
        return "user-badge-admin";
      }
      if (isParticipant(userId)) {
        return "user-badge-participant";
      }
      return "";
    };
    const getRoleLabel = (userId) => {
      if (isOwner(userId)) {
        return t("RoomBarrage.Host");
      }
      if (isAdmin(userId)) {
        return t("RoomBarrage.Admin");
      }
      if (isParticipant(userId)) {
        return "";
      }
      return "";
    };
    const placeholder = computed(
      () => {
        var _a;
        return ((_a = localParticipant.value) == null ? void 0 : _a.isMessageDisabled) ? t("RoomChat.disabled_placeholder") : t("RoomChat.input_placeholder");
      }
    );
    watch(() => props.isChatOpen, (newVal, oldVal) => {
      var _a;
      if (newVal && !oldVal && barrageListRef.value) {
        (_a = barrageListRef.value) == null ? void 0 : _a.scrollToBottom({ behavior: "smooth" });
      }
    });
    return (_ctx, _cache) => {
      var _a;
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(unref(BarrageList), {
          ref_key: "barrageListRef",
          ref: barrageListRef,
          class: "room-message-list"
        }, {
          "user-badge": withCtx(({ message }) => [
            createElementVNode("span", {
              class: normalizeClass(["user-badge", getRoleClass(message.sender.userId)])
            }, toDisplayString(getRoleLabel(message.sender.userId)), 3)
          ]),
          _: 1
        }, 512),
        createVNode(unref(BarrageInput), {
          class: "room-message-input",
          hideSendButton: "",
          placeholder: placeholder.value,
          disabled: (_a = unref(localParticipant)) == null ? void 0 : _a.isMessageDisabled
        }, null, 8, ["placeholder", "disabled"])
      ]);
    };
  }
});
export {
  _sfc_main as default
};
