import { defineComponent as f, createElementBlock as p, openBlock as C, Fragment as h, createVNode as r, unref as a, withCtx as l, createTextVNode as c, createElementVNode as y } from "vue";
import { useLoadingState as v, ScalarButton as I } from "@scalar/components";
import { useToasts as U } from "@scalar/use-toasts";
import { fetchOpenIDConnectDiscovery as w } from "../helpers/fetch-openid-connect-discovery.js";
import { openIDDiscoveryToFlows as g } from "../helpers/openid-discovery-to-flows.js";
import x from "./RequestAuthDataTableInput.vue.js";
import i from "../../../../components/DataTable/DataTableRow.vue.js";
const B = { class: "flex h-8 w-full items-center justify-end border-t" }, T = /* @__PURE__ */ f({
  __name: "OpenIDConnect",
  props: {
    environment: {},
    eventBus: {},
    getStaticBorderClass: { type: Function },
    name: {},
    proxyUrl: {},
    scheme: {}
  },
  setup(e) {
    const o = v(), { toast: s } = U(), d = async () => {
      if (o.isLoading || !e.scheme.openIdConnectUrl)
        return;
      o.start();
      const [n, t] = await w(
        e.scheme.openIdConnectUrl,
        e.proxyUrl
      );
      if (await o.clear(), n) {
        console.error(n), s(
          n?.message ?? "Failed to fetch OpenID Connect configuration",
          "error"
        );
        return;
      }
      const u = g(t);
      e.eventBus.emit("auth:update:security-scheme-secrets", {
        payload: u,
        name: e.name,
        overwrite: !0
      });
    }, m = (n) => e.eventBus.emit("auth:update:security-scheme", {
      payload: {
        type: "openIdConnect",
        openIdConnectUrl: n
      },
      name: e.name
    });
    return (n, t) => (C(), p(h, null, [
      r(a(i), null, {
        default: l(() => [
          r(x, {
            containerClass: e.getStaticBorderClass(),
            environment: e.environment,
            modelValue: e.scheme.openIdConnectUrl,
            placeholder: "https://example.com/.well-known/openid-configuration",
            required: "",
            "onUpdate:modelValue": m
          }, {
            default: l(() => [...t[0] || (t[0] = [
              c(" Discovery URL ", -1)
            ])]),
            _: 1
          }, 8, ["containerClass", "environment", "modelValue"])
        ]),
        _: 1
      }),
      r(a(i), { class: "min-w-full" }, {
        default: l(() => [
          y("div", B, [
            r(a(I), {
              class: "mr-0.75 p-0 px-2 py-0.5",
              disabled: !e.scheme.openIdConnectUrl,
              loader: a(o),
              size: "sm",
              variant: "outlined",
              onClick: d
            }, {
              default: l(() => [...t[1] || (t[1] = [
                c(" Fetch Configuration ", -1)
              ])]),
              _: 1
            }, 8, ["disabled", "loader"])
          ])
        ]),
        _: 1
      })
    ], 64));
  }
});
export {
  T as default
};
