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