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