import { defineComponent as b, computed as i, ref as d, onMounted as U, onBeforeUnmount as x, watch as K, createElementBlock as V, openBlock as M, createElementVNode as O, createVNode as l, withCtx as q, unref as B } from "vue";
import { AVAILABLE_CLIENTS as P } from "@scalar/types/snippetz";
import { useToasts as w } from "@scalar/use-toasts";
import T from "../../../components/ViewLayout/ViewLayout.vue.js";
import I from "../../../components/ViewLayout/ViewLayoutContent.vue.js";
import { ERRORS as A } from "../../../libs/errors.js";
import { buildRequest as L } from "./helpers/build-request.js";
import { getSecuritySchemes as N } from "./helpers/build-request-security.js";
import { harToFetchRequest as H } from "./helpers/har-to-fetch-request.js";
import { harToFetchResponse as F } from "./helpers/har-to-fetch-response.js";
import { sendRequest as j } from "./helpers/send-request.js";
import { validatePathParameters as D } from "./helpers/validate-path-parameters.js";
import Q from "./components/Header.vue.js";
import { generateClientOptions as $ } from "../operation-code-sample/helpers/generate-client-options.js";
import { getSecurityRequirements as z } from "../../features/operation/helpers/get-security-requirements.js";
import { getSelectedSecurity as G } from "../../features/operation/helpers/get-selected-security.js";
import J from "../request-block/RequestBlock.vue.js";
import W from "../response-block/ResponseBlock.vue.js";
const X = { class: "bg-b-1 flex h-full flex-col" }, Y = { class: "lg:min-h-header flex w-full flex-wrap items-center justify-center p-2 lg:p-0" }, Z = {
  name: "OperationBlock"
}, fe = /* @__PURE__ */ b({
  ...Z,
  props: {
    eventBus: {},
    documentSecurity: {},
    documentSelectedSecurity: {},
    appVersion: {},
    globalCookies: { default: () => [] },
    path: {},
    method: {},
    httpClients: { default: () => P },
    history: { default: () => [] },
    layout: {},
    server: {},
    selectedClient: {},
    servers: {},
    serverMeta: {},
    hideClientButton: { type: Boolean },
    integration: {},
    documentUrl: {},
    source: {},
    operation: {},
    operationSelectedSecurity: {},
    exampleKey: {},
    authMeta: {},
    securitySchemes: {},
    plugins: { default: () => [] },
    environments: {},
    activeEnvironment: {},
    environment: {},
    proxyUrl: {}
  },
  setup(e) {
    const R = i(() => $(e.httpClients)), v = i(
      () => z(e.documentSecurity, e.operation.security)
    ), y = i(
      () => G(
        e.documentSelectedSecurity,
        e.operationSelectedSecurity,
        v.value
      )
    ), f = i(
      () => N(e.securitySchemes, y.value.selectedSchemes)
    ), { toast: c } = w(), S = d(null), a = d(null), u = d(null), s = () => S.value?.abort(A.REQUEST_ABORTED), h = async () => {
      if (D(
        e.operation.parameters ?? [],
        e.exampleKey
      ).ok === !1) {
        c("Path parameters must have values.", "error");
        return;
      }
      const [r, t] = L({
        environment: e.environment,
        exampleKey: e.exampleKey,
        globalCookies: e.globalCookies,
        method: e.method,
        operation: e.operation,
        path: e.path,
        selectedSecuritySchemes: f.value,
        server: e.server,
        proxyUrl: e.proxyUrl
      });
      if (r) {
        c(r.message, "error");
        return;
      }
      S.value = t.controller, a.value && "reader" in a.value && a.value.reader.cancel(), e.eventBus.emit("hooks:on:request:sent", {
        meta: {
          method: e.method,
          path: e.path,
          exampleKey: e.exampleKey
        }
      });
      const [m, n] = await j({
        isUsingProxy: t.isUsingProxy,
        operation: e.operation,
        plugins: e.plugins,
        request: t.request
      });
      if (e.eventBus.emit("hooks:on:request:complete", {
        payload: n ? {
          response: n.originalResponse,
          request: n.request.clone(),
          duration: n.response.duration,
          timestamp: n.timestamp
        } : void 0,
        meta: {
          method: e.method,
          path: e.path,
          exampleKey: e.exampleKey
        }
      }), m) {
        c(m.message, "error");
        return;
      }
      a.value = n.response, u.value = n.request;
    };
    U(() => {
      e.eventBus.on("operation:send:request:hotkey", h), e.eventBus.on("operation:cancel:request", s);
    }), x(() => {
      e.eventBus.off("operation:send:request:hotkey", h), e.eventBus.off("operation:cancel:request", s);
    });
    const g = i(
      () => e.history.map((o) => ({
        method: o.request.method,
        path: o.request.url,
        duration: o.time,
        status: o.response.status
      })).reverse()
    ), C = ({ index: o }) => {
      const r = (e.history.length ?? 0) - o - 1, t = e.history[r];
      if (!t)
        return;
      const m = () => e.eventBus.emit("ui:navigate", {
        page: "example",
        method: e.method,
        path: e.path,
        exampleName: "draft",
        callback: (n) => {
          if (n !== "success")
            return;
          const k = F({
            harResponse: t.response,
            url: t.request.url,
            method: e.method,
            path: e.path,
            duration: t.time
          }), E = H({
            harRequest: t.request
          });
          a.value = k, u.value = E;
        }
      });
      e.eventBus.emit("operation:reload:history", {
        meta: {
          path: e.path,
          method: e.method
        },
        index: r,
        callback: m
      });
    };
    return K([() => e.path, () => e.method, () => e.exampleKey], () => {
      a.value = null, u.value = null, s();
    }), x(() => {
      s();
    }), (o, r) => (M(), V("div", X, [
      O("div", Y, [
        l(Q, {
          activeEnvironment: e.activeEnvironment,
          documentUrl: e.documentUrl,
          environment: e.environment,
          environments: e.environments,
          eventBus: e.eventBus,
          hideClientButton: e.hideClientButton,
          history: g.value,
          integration: e.integration,
          layout: e.layout,
          method: e.method,
          path: e.path,
          server: e.server,
          serverMeta: e.serverMeta,
          servers: e.servers,
          source: e.source,
          onExecute: h,
          "onSelect:history:item": C
        }, null, 8, ["activeEnvironment", "documentUrl", "environment", "environments", "eventBus", "hideClientButton", "history", "integration", "layout", "method", "path", "server", "serverMeta", "servers", "source"])
      ]),
      l(T, { class: "border-t" }, {
        default: q(() => [
          l(I, { class: "flex-1" }, {
            default: q(() => [
              l(B(J), {
                authMeta: e.authMeta,
                clientOptions: R.value,
                environment: e.environment,
                eventBus: e.eventBus,
                exampleKey: e.exampleKey,
                globalCookies: e.globalCookies,
                layout: e.layout,
                method: e.method,
                operation: e.operation,
                path: e.path,
                plugins: e.plugins,
                proxyUrl: e.proxyUrl,
                securityRequirements: v.value,
                securitySchemes: e.securitySchemes,
                selectedClient: e.selectedClient,
                selectedSecurity: y.value,
                selectedSecuritySchemes: f.value,
                server: e.server
              }, null, 8, ["authMeta", "clientOptions", "environment", "eventBus", "exampleKey", "globalCookies", "layout", "method", "operation", "path", "plugins", "proxyUrl", "securityRequirements", "securitySchemes", "selectedClient", "selectedSecurity", "selectedSecuritySchemes", "server"]),
              l(B(W), {
                appVersion: e.appVersion,
                eventBus: e.eventBus,
                layout: e.layout,
                plugins: e.plugins,
                request: u.value,
                response: a.value,
                totalPerformedRequests: g.value.length
              }, null, 8, ["appVersion", "eventBus", "layout", "plugins", "request", "response", "totalPerformedRequests"])
            ]),
            _: 1
          })
        ]),
        _: 1
      })
    ]));
  }
});
export {
  fe as default
};
