import { defineComponent as y, ref as c, watch as _, nextTick as w, onMounted as h, onBeforeUnmount as k, createBlock as B, openBlock as l, withCtx as v, createElementVNode as s, createElementBlock as i, createCommentVNode as d, toDisplayString as p, unref as u, createVNode as b } from "vue";
import { useLoadingState as C, ScalarLoading as S } from "@scalar/components";
import L from "../../../components/ViewLayout/ViewLayoutCollapse.vue.js";
const T = { class: "flex w-full items-center justify-between" }, E = {
  key: 0,
  class: "mr-2 flex items-center gap-2"
}, N = {
  key: 0,
  class: "text-red bg-b-danger sticky top-0 border-b p-2"
}, V = {
  key: 1,
  class: "p-2"
}, z = /* @__PURE__ */ y({
  __name: "ResponseBodyStreaming",
  props: {
    reader: {}
  },
  setup(f) {
    const e = C(), o = c(""), n = c(null), m = new TextDecoder(), a = c(null), x = () => {
      a.value && (a.value.scrollTop = a.value.scrollHeight);
    };
    _(o, () => {
      w(x);
    });
    async function g() {
      try {
        for (; e.isLoading; ) {
          const { done: r, value: t } = await f.reader.read();
          if (r) {
            e.clear();
            break;
          }
          t && (o.value += m.decode(t, { stream: !0 }));
        }
      } catch (r) {
        console.error("Error reading stream:", r), e.clear(), n.value = r;
      } finally {
        o.value += m.decode();
      }
    }
    return h(() => {
      e.start(), g(), n.value = null;
    }), k(() => {
      f.reader.cancel(), e.clear();
    }), (r, t) => (l(), B(L, { class: "max-h-content overflow-y-hidden" }, {
      title: v(() => [
        s("div", T, [
          t[1] || (t[1] = s("div", null, "Body", -1)),
          u(e).isActive ? (l(), i("div", E, [
            b(u(S), {
              loader: u(e),
              size: "xs"
            }, null, 8, ["loader"]),
            t[0] || (t[0] = s("span", { class: "text-c-2" }, " Listening… ", -1))
          ])) : d("", !0)
        ])
      ]),
      default: v(() => [
        s("div", {
          ref_key: "contentContainer",
          ref: a,
          class: "text-xxs font-code h-full overflow-auto leading-6 whitespace-pre-wrap"
        }, [
          n.value ? (l(), i("div", N, p(n.value.message), 1)) : d("", !0),
          o.value ? (l(), i("div", V, p(o.value), 1)) : d("", !0)
        ], 512)
      ]),
      _: 1
    }));
  }
});
export {
  z as default
};
