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