import { defineComponent as S, ref as g, onMounted as E, watch as B, onBeforeUnmount as b, createElementBlock as R, openBlock as k } from "vue";
import { useActiveEntities as q, useWorkspace as x } from "@scalar/api-client/store";
import { mutateSecuritySchemeDiff as y } from "@scalar/api-client/views/Request/libs";
import { getServersFromOpenApiDocument as D } from "@scalar/oas-utils/transforms";
import { watchDebounced as d } from "@vueuse/core";
import M from "microdiff";
import { useNavState as U } from "../../hooks/useNavState.js";
import { useApiClient as A } from "./useApiClient.js";
import { useExampleStore as C } from "../../legacy/stores/useExampleStore.js";
const j = /* @__PURE__ */ S({
  __name: "ApiClientModal",
  props: {
    configuration: {},
    dereferencedDocument: {}
  },
  setup(r) {
    const s = g(null), { client: c, init: v } = A(), { selectedExampleKey: p, operationId: f } = C(), a = q(), n = x(), { isIntersectionEnabled: m } = U(), h = () => ({
      name: "on-before-request",
      hooks: {
        onBeforeRequest: r.configuration.onBeforeRequest
      }
    });
    return E(() => {
      s.value && v({
        el: s.value,
        configuration: {
          ...r.configuration,
          // If the onBeforeRequest hook is configured, we add the plugin to the API client.
          plugins: r.configuration.onBeforeRequest ? [h] : []
        },
        store: n
      });
    }), d(
      () => r.dereferencedDocument,
      (e, i) => {
        var o, u;
        e && JSON.stringify(e) !== JSON.stringify(i || {}) && (a.activeCollection.value && ((o = c.value) == null || o.resetStore()), n.importSpecFile(void 0, "default", {
          dereferencedDocument: e,
          shouldLoad: !1,
          documentUrl: (u = r.configuration) == null ? void 0 : u.url,
          useCollectionSecurity: !0,
          ...r.configuration
        }));
      }
    ), d(
      () => r.configuration,
      (e, i) => {
        if (!i || !a.activeCollection.value)
          return;
        const o = a.activeCollection.value, u = M(i, e);
        if (!u.some(
          (t) => t.path[0] === "url" || t.path[0] === "content" || t.path[1] === "url" || t.path[1] === "content"
        )) {
          if (u.forEach((t) => {
            t.path[0] === "authentication" && y(t, a, n);
          }), e.servers || i.servers) {
            o.servers.forEach((l) => {
              n.serverMutators.delete(l, o.uid);
            });
            const t = D(
              e.servers ?? r.dereferencedDocument.servers,
              {
                baseServerURL: e.baseServerURL
              }
            );
            t.forEach((l) => {
              n.serverMutators.add(l, o.uid);
            }), t.length && n.collectionMutators.edit(
              o.uid,
              "selectedServerUid",
              t[t.length - 1].uid
            );
          }
        }
        m.value = !1, setTimeout(() => {
          m.value = !0;
        }, 1e3);
      },
      { deep: !0, debounce: 300 }
    ), B(p, (e) => {
      c.value && e && f.value && c.value.updateExample(e, f.value);
    }), b(() => {
      var e;
      return (e = c.value) == null ? void 0 : e.app.unmount();
    }), (e, i) => (k(), R("div", {
      ref_key: "el",
      ref: s
    }, null, 512));
  }
});
export {
  j as default
};
