import { defineComponent as H, ref as m, computed as C, watch as Q, onMounted as F, onBeforeUnmount as G, createElementBlock as K, openBlock as f, normalizeClass as N, unref as r, createBlock as U, createCommentVNode as O, createElementVNode as V, isRef as X, createVNode as Y } from "vue";
import { isDefined as Z } from "@scalar/helpers/array/is-defined";
import { safeJSON as ee } from "@scalar/object-utils/parse";
import { useToasts as te } from "@scalar/use-toasts";
import { RouterView as oe } from "vue-router";
import re from "../../components/Sidebar/SidebarToggle.vue.js";
import { useAnalytics as se } from "../../hooks/useAnalytics.js";
import { useClientConfig as ae } from "../../hooks/useClientConfig.js";
import { useSidebar as ne } from "../../hooks/useSidebar.js";
import { validateParameters as le } from "../../libs/validate-parameters.js";
import { useActiveEntities as ue } from "../../store/active-entities.js";
import { useOpenApiWatcher as ie } from "./hooks/useOpenApiWatcher.js";
import ce from "./RequestSidebar.vue.js";
import { useWorkspace as me } from "../../store/store.js";
import { useLayout as fe } from "../../hooks/useLayout.js";
import { usePluginManager as de } from "../../plugins/hooks/usePluginManager.js";
import { createRequestOperation as pe } from "../../libs/send-request/create-request-operation.js";
import { ERRORS as ve } from "../../libs/errors.js";
const Re = { class: "flex h-full" }, Se = { class: "flex h-full flex-1 flex-col" }, De = /* @__PURE__ */ H({
  __name: "RequestRoot",
  emits: ["newTab"],
  setup(qe) {
    const w = me(), { toast: u } = te(), { layout: d } = fe(), B = ae(), { isSidebarOpen: s } = ne(), A = se(), {
      activeCollection: n,
      activeExample: i,
      activeEnvironment: p,
      activeRequest: l,
      activeWorkspace: v,
      activeServer: T
    } = ue(), {
      cookies: P,
      requestHistory: D,
      showSidebar: R,
      securitySchemes: M,
      modalState: z,
      events: o
    } = w, I = de(), J = m(), S = m(), q = C(
      () => le(i.value ?? null)
    ), y = m(null), x = C(
      () => (n.value?.useCollectionSecurity ? n.value?.selectedSecuritySchemeUids : l.value?.selectedSecuritySchemeUids) ?? []
    ), h = async () => {
      if (!l.value || !i.value || !n.value)
        return;
      if (q.value.hasBlockingErrors) {
        u("Path parameters must have values.", "error"), o.requestStatus.emit("abort");
        return;
      }
      const e = typeof p.value == "object" ? p.value.value : "{}", t = ee.parse(e);
      t.error && console.error("INVALID ENVIRONMENT!");
      const a = t.error || typeof t.data != "object" ? {} : t.data ?? {}, j = v.value?.cookies.map(($) => P[$]).filter(Z) ?? [], L = n.value?.info?.title === "Drafts" ? void 0 : T.value, [g, k] = pe({
        request: l.value,
        example: i.value,
        selectedSecuritySchemeUids: x.value,
        proxyUrl: v.value?.proxyUrl ?? "",
        environment: a,
        globalCookies: j,
        status: o.requestStatus,
        securitySchemes: M,
        server: L,
        pluginManager: I
      });
      if (B.value?.onRequestSent?.(l.value.path ?? ""), g) {
        u(g.message, "error");
        return;
      }
      S.value = k.controller;
      const [E, _] = await k.sendRequest();
      y.value = _, E ? u(E.message, "error") : D.push(W(_));
    }, c = async () => S.value?.abort(ve.REQUEST_ABORTED);
    function b() {
      A?.capture("client-send-request");
    }
    Q(z, ({ open: e }) => {
      e || c();
    }), F(() => {
      o.executeRequest.on(h), o.executeRequest.on(b), o.cancelRequest.on(c);
    }), ie(), G(() => {
      o.executeRequest.off(h), o.executeRequest.off(b), o.cancelRequest.off(c);
    });
    const W = (e) => {
      try {
        return structuredClone(e);
      } catch {
        const t = { ...e };
        return e.response?.data && (e.response.data instanceof Blob || e.response.data instanceof ArrayBuffer ? t.response.data = e.response.data : t.response.data = JSON.parse(JSON.stringify(e.response.data))), t;
      }
    };
    return (e, t) => (f(), K("div", {
      ref_key: "_element",
      ref: J,
      class: N(["bg-b-1 relative z-0 flex h-full flex-1 flex-col overflow-hidden pt-0", {
        "!mr-0 !mb-0 !border-0": r(d) === "modal"
      }])
    }, [
      r(R) ? (f(), U(re, {
        key: 0,
        modelValue: r(s),
        "onUpdate:modelValue": t[0] || (t[0] = (a) => X(s) ? s.value = a : null),
        class: N(["absolute top-2 left-3 z-50", [
          { hidden: r(s) },
          { "xl:!flex": !r(s) },
          { "!flex": r(d) === "modal" }
        ]])
      }, null, 8, ["modelValue", "class"])) : O("", !0),
      V("div", Re, [
        r(R) ? (f(), U(ce, {
          key: 0,
          onNewTab: t[1] || (t[1] = (a) => e.$emit("newTab", a))
        })) : O("", !0),
        V("div", Se, [
          Y(r(oe), {
            invalidParams: q.value.invalidParams,
            requestResult: y.value,
            selectedSecuritySchemeUids: x.value
          }, null, 8, ["invalidParams", "requestResult", "selectedSecuritySchemeUids"])
        ])
      ])
    ], 2));
  }
});
export {
  De as default
};
