import { defineComponent, ref, reactive, shallowRef, watchEffect, onUnmounted, withDirectives, openBlock, createElementBlock, createVNode, unref, withCtx, createElementVNode, normalizeClass, toDisplayString, createCommentVNode } from "vue";
import { useBasicStore } from "../../../stores/basic.mjs";
import { storeToRefs } from "pinia";
import { useI18n } from "../../../locales/index.mjs";
import TUIRoomEngine__default, { TUINetworkQuality, TUIRoomEvents } from "@tencentcloud/tuiroom-engine-js";
import useGetRoomEngine from "../../../hooks/useRoomEngine.mjs";
import IconButton from "../../common/base/IconButton.vue.mjs";
import vClickOutside from "../../../directives/vClickOutside.mjs";
import NetworkStabilityIcon from "../../common/icons/NetworkStabilityIcon.vue.mjs";
import NetworkFluctuationIcon from "../../common/icons/NetworkFluctuationIcon.vue.mjs";
import NetworkLagIcon from "../../common/icons/NetworkLagIcon.vue.mjs";
import NetworkDisconnectedIcon from "../../common/icons/NetworkDisconnectedIcon.vue.mjs";
import ArrowStrokeUpIcon from "../../common/icons/ArrowStrokeUpIcon.vue.mjs";
import { IconButtonLayout } from "../../../constants/room.mjs";
const _hoisted_1 = { class: "network-info-container" };
const _hoisted_2 = { class: "network-detail-item" };
const _hoisted_3 = { class: "network-detail-item" };
const _hoisted_4 = { class: "network-detail-packet" };
const _hoisted_5 = { class: "network-detail-packet-item" };
const _hoisted_6 = { class: "network-detail-packet-item" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "NetworkInfo",
  setup(__props) {
    const roomEngine = useGetRoomEngine();
    const { t } = useI18n();
    const basicStore = useBasicStore();
    const { networkInfo } = storeToRefs(basicStore);
    const networkBoard = ref();
    const showNetworkInfo = ref(false);
    const state = reactive({
      title: "",
      titleType: void 0,
      networkIcon: null
    });
    const qualityMap = {
      [TUINetworkQuality.kQualityExcellent]: {
        title: "Stability",
        titleType: "success",
        icon: shallowRef(NetworkStabilityIcon)
      },
      [TUINetworkQuality.kQualityPoor]: {
        title: "Fluctuation",
        titleType: "warning",
        icon: shallowRef(NetworkFluctuationIcon)
      },
      [TUINetworkQuality.kQualityVeryBad]: {
        title: "Lag",
        titleType: "danger",
        icon: shallowRef(NetworkLagIcon)
      },
      [TUINetworkQuality.kQualityDown]: {
        title: "Disconnected",
        titleType: "info",
        icon: shallowRef(NetworkDisconnectedIcon)
      }
    };
    watchEffect(() => {
      const quality = qualityMap[networkInfo.value.quality];
      if (quality) {
        state.title = quality.title;
        state.titleType = quality.titleType;
        state.networkIcon = quality.icon;
      }
    });
    function handleClickNetworkIcon() {
      showNetworkInfo.value = !showNetworkInfo.value;
    }
    function handleClickOutSide() {
      if (showNetworkInfo.value) {
        showNetworkInfo.value = false;
      }
    }
    async function onUserNetworkQualityChanged({
      userNetworkList
    }) {
      userNetworkList.forEach((userNetwork) => {
        basicStore.setNetworkInfo(userNetwork);
      });
    }
    TUIRoomEngine__default.once("ready", () => {
      var _a;
      (_a = roomEngine.instance) == null ? void 0 : _a.on(
        TUIRoomEvents.onUserNetworkQualityChanged,
        onUserNetworkQualityChanged
      );
    });
    onUnmounted(() => {
      var _a;
      (_a = roomEngine.instance) == null ? void 0 : _a.off(
        TUIRoomEvents.onUserNetworkQualityChanged,
        onUserNetworkQualityChanged
      );
    });
    return (_ctx, _cache) => {
      return withDirectives((openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(IconButton, {
          layout: unref(IconButtonLayout).HORIZONTAL,
          icon: state.networkIcon,
          onClickIcon: handleClickNetworkIcon
        }, {
          title: withCtx(() => [
            createElementVNode("span", {
              class: normalizeClass([`title-type-${state.titleType}`])
            }, toDisplayString(unref(t)(`${state.title}`)), 3)
          ]),
          _: 1
        }, 8, ["layout", "icon"]),
        showNetworkInfo.value ? (openBlock(), createElementBlock("div", {
          key: 0,
          ref_key: "networkBoard",
          ref: networkBoard,
          class: "network-info-board"
        }, [
          createElementVNode("div", _hoisted_2, [
            createElementVNode("span", null, toDisplayString(unref(t)("Latency")), 1),
            createElementVNode("span", {
              class: normalizeClass(["title-latency", `title-type-${state.titleType}`])
            }, toDisplayString(`${unref(networkInfo).delay} ms`), 3)
          ]),
          createElementVNode("div", _hoisted_3, [
            createElementVNode("span", null, toDisplayString(unref(t)("Packet loss")), 1),
            createElementVNode("div", _hoisted_4, [
              createElementVNode("div", _hoisted_5, [
                createVNode(ArrowStrokeUpIcon),
                createElementVNode("span", null, toDisplayString(`${unref(networkInfo).upLoss}%`), 1)
              ]),
              createElementVNode("div", _hoisted_6, [
                createVNode(ArrowStrokeUpIcon, { class: "arrow-down" }),
                createElementVNode("span", null, toDisplayString(`${unref(networkInfo).downLoss}%`), 1)
              ])
            ])
          ])
        ], 512)) : createCommentVNode("", true)
      ])), [
        [unref(vClickOutside), handleClickOutSide]
      ]);
    };
  }
});
export {
  _sfc_main as default
};
