import { defineComponent as O, computed as C, ref as P, watch as R, createElementBlock as k, openBlock as m, Fragment as p, createVNode as r, unref as o, withCtx as l, createTextVNode as u, createElementVNode as S, createBlock as v, createCommentVNode as f } from "vue";
import { useLoadingState as I, ScalarButton as V } from "@scalar/components";
import { pkceOptions as L } from "@scalar/oas-utils/entities/spec";
import { useToasts as F } from "@scalar/use-toasts";
import { getEnvironmentVariables as A } from "../../operation-block/helpers/get-environment-variables.js";
import E from "./OAuthScopesInput.vue.js";
/* empty css                      */
import { authorizeOauth2 as N } from "../helpers/oauth.js";
import c from "./RequestAuthDataTableInput.vue.js";
import i from "../../../../components/DataTable/DataTableRow.vue.js";
const j = { class: "flex h-8 items-center justify-end gap-2 border-t" }, Q = { class: "flex h-8 w-full items-center justify-end border-t" }, J = /* @__PURE__ */ O({
  __name: "OAuth2",
  props: {
    environment: {},
    flows: {},
    type: {},
    selectedScopes: {},
    scheme: {},
    server: {},
    proxyUrl: {},
    name: {},
    eventBus: {}
  },
  emits: ["update:selectedScopes"],
  setup(t, { emit: T }) {
    const g = T, x = I(), { toast: B } = F(), n = C(() => t.flows[t.type]), w = C(
      () => t.selectedScopes.filter((s) => s in (n.value.scopes ?? {}))
    ), y = (s) => {
      if (t.scheme.type === "openIdConnect")
        return d(s);
      t.eventBus.emit("auth:update:security-scheme", {
        payload: {
          type: t.scheme.type,
          flows: {
            [t.type]: s
          }
        },
        name: t.name
      });
    }, d = (s) => t.eventBus.emit("auth:update:security-scheme-secrets", {
      payload: {
        type: t.scheme.type,
        [t.type]: s
      },
      name: t.name
    }), b = () => t.eventBus.emit("auth:clear:security-scheme-secrets", {
      name: t.name
    }), U = P(!1);
    R(
      () => n.value["x-scalar-secret-redirect-uri"],
      (s) => {
        U.value || s || typeof window > "u" || !("x-scalar-secret-redirect-uri" in n.value) || (U.value = !0, d({
          "x-scalar-secret-redirect-uri": window.location.origin + window.location.pathname
        }));
      },
      { immediate: !0 }
    );
    const z = async () => {
      if (x.isLoading)
        return;
      x.start();
      const [s, e] = await N(
        t.flows,
        t.type,
        w.value,
        t.server,
        t.proxyUrl,
        A(t.environment)
      );
      await x.clear(), e?.accessToken ? d({
        "x-scalar-secret-token": e.accessToken,
        ...e.refreshToken ? { "x-scalar-secret-refresh-token": e.refreshToken } : {}
      }) : (console.error(s), B(s?.message ?? "Failed to authorize", "error"));
    }, h = (s) => y({
      "x-scalar-credentials-location": s === "body" ? "body" : "header"
    });
    return (s, e) => n.value["x-scalar-secret-token"] ? (m(), k(p, { key: 0 }, [
      r(o(i), null, {
        default: l(() => [
          r(c, {
            class: "border-r-transparent",
            environment: t.environment,
            modelValue: n.value["x-scalar-secret-token"],
            placeholder: "QUxMIFlPVVIgQkFTRSBBUkUgQkVMT05HIFRPIFVT",
            type: "password",
            "onUpdate:modelValue": e[0] || (e[0] = (a) => d({ "x-scalar-secret-token": a }))
          }, {
            default: l(() => [...e[12] || (e[12] = [
              u(" Access Token ", -1)
            ])]),
            _: 1
          }, 8, ["environment", "modelValue"])
        ]),
        _: 1
      }),
      r(o(i), { class: "min-w-full" }, {
        default: l(() => [
          S("div", j, [
            r(o(V), {
              class: "mr-1 p-0 px-2 py-0.5",
              loader: o(x),
              size: "sm",
              variant: "outlined",
              onClick: e[1] || (e[1] = () => d({
                "x-scalar-secret-token": "",
                "x-scalar-secret-refresh-token": ""
              }))
            }, {
              default: l(() => [...e[13] || (e[13] = [
                u(" Clear ", -1)
              ])]),
              _: 1
            }, 8, ["loader"])
          ])
        ]),
        _: 1
      })
    ], 64)) : (m(), k(p, { key: 1 }, [
      r(o(i), null, {
        default: l(() => [
          "authorizationUrl" in n.value ? (m(), v(c, {
            key: 0,
            containerClass: "border-r-0",
            environment: t.environment,
            modelValue: n.value["x-scalar-secret-auth-url"] ?? "",
            placeholder: "https://galaxy.scalar.com/authorize",
            "onUpdate:modelValue": e[2] || (e[2] = (a) => {
              d({ "x-scalar-secret-auth-url": a }), y({ authorizationUrl: a });
            })
          }, {
            default: l(() => [...e[14] || (e[14] = [
              u(" Auth URL ", -1)
            ])]),
            _: 1
          }, 8, ["environment", "modelValue"])) : f("", !0),
          "tokenUrl" in n.value ? (m(), v(c, {
            key: 1,
            environment: t.environment,
            modelValue: n.value["x-scalar-secret-token-url"] ?? "",
            placeholder: "https://galaxy.scalar.com/token",
            "onUpdate:modelValue": e[3] || (e[3] = (a) => {
              d({ "x-scalar-secret-token-url": a }), y({ tokenUrl: a });
            })
          }, {
            default: l(() => [...e[15] || (e[15] = [
              u(" Token URL ", -1)
            ])]),
            _: 1
          }, 8, ["environment", "modelValue"])) : f("", !0)
        ]),
        _: 1
      }),
      "x-scalar-secret-redirect-uri" in n.value ? (m(), v(o(i), { key: 0 }, {
        default: l(() => [
          r(c, {
            environment: t.environment,
            modelValue: n.value["x-scalar-secret-redirect-uri"],
            placeholder: "https://galaxy.scalar.com/callback",
            "onUpdate:modelValue": e[4] || (e[4] = (a) => {
              d({ "x-scalar-secret-redirect-uri": a });
            })
          }, {
            default: l(() => [...e[16] || (e[16] = [
              u(" Redirect URL ", -1)
            ])]),
            _: 1
          }, 8, ["environment", "modelValue"])
        ]),
        _: 1
      })) : f("", !0),
      "x-scalar-secret-username" in n.value && "x-scalar-secret-password" in n.value ? (m(), k(p, { key: 1 }, [
        r(o(i), null, {
          default: l(() => [
            r(c, {
              class: "text-c-2",
              environment: t.environment,
              modelValue: n.value["x-scalar-secret-username"],
              placeholder: "janedoe",
              "onUpdate:modelValue": e[5] || (e[5] = (a) => d({ "x-scalar-secret-username": a }))
            }, {
              default: l(() => [...e[17] || (e[17] = [
                u(" Username ", -1)
              ])]),
              _: 1
            }, 8, ["environment", "modelValue"])
          ]),
          _: 1
        }),
        r(o(i), null, {
          default: l(() => [
            r(c, {
              environment: t.environment,
              modelValue: n.value["x-scalar-secret-password"],
              placeholder: "********",
              type: "password",
              "onUpdate:modelValue": e[6] || (e[6] = (a) => d({ "x-scalar-secret-password": a }))
            }, {
              default: l(() => [...e[18] || (e[18] = [
                u(" Password ", -1)
              ])]),
              _: 1
            }, 8, ["environment", "modelValue"])
          ]),
          _: 1
        })
      ], 64)) : f("", !0),
      r(o(i), null, {
        default: l(() => [
          r(c, {
            environment: t.environment,
            modelValue: n.value["x-scalar-secret-client-id"],
            placeholder: "12345",
            "onUpdate:modelValue": e[7] || (e[7] = (a) => d({ "x-scalar-secret-client-id": a }))
          }, {
            default: l(() => [...e[19] || (e[19] = [
              u(" Client ID ", -1)
            ])]),
            _: 1
          }, 8, ["environment", "modelValue"])
        ]),
        _: 1
      }),
      "x-scalar-secret-client-secret" in n.value ? (m(), v(o(i), { key: 2 }, {
        default: l(() => [
          r(c, {
            environment: t.environment,
            modelValue: n.value["x-scalar-secret-client-secret"],
            placeholder: "XYZ123",
            type: "password",
            "onUpdate:modelValue": e[8] || (e[8] = (a) => d({ "x-scalar-secret-client-secret": a }))
          }, {
            default: l(() => [...e[20] || (e[20] = [
              u(" Client Secret ", -1)
            ])]),
            _: 1
          }, 8, ["environment", "modelValue"])
        ]),
        _: 1
      })) : f("", !0),
      "x-usePkce" in n.value ? (m(), v(o(i), { key: 3 }, {
        default: l(() => [
          r(c, {
            enum: o(L),
            environment: t.environment,
            modelValue: n.value["x-usePkce"],
            readOnly: "",
            "onUpdate:modelValue": e[9] || (e[9] = (a) => y({
              "x-usePkce": a
            }))
          }, {
            default: l(() => [...e[21] || (e[21] = [
              u(" Use PKCE ", -1)
            ])]),
            _: 1
          }, 8, ["enum", "environment", "modelValue"])
        ]),
        _: 1
      })) : f("", !0),
      t.type !== "implicit" ? (m(), v(o(i), { key: 4 }, {
        default: l(() => [
          r(c, {
            enum: ["header", "body"],
            environment: t.environment,
            modelValue: n.value["x-scalar-credentials-location"] || "header",
            placeholder: "header",
            readOnly: "",
            "onUpdate:modelValue": e[10] || (e[10] = (a) => h(a))
          }, {
            default: l(() => [...e[22] || (e[22] = [
              u(" Credentials Location ", -1)
            ])]),
            _: 1
          }, 8, ["environment", "modelValue"])
        ]),
        _: 1
      })) : f("", !0),
      r(o(i), null, {
        default: l(() => [
          r(E, {
            flow: n.value,
            flowType: t.type,
            selectedScopes: w.value,
            "onUpdate:selectedScopes": e[11] || (e[11] = (a) => g("update:selectedScopes", a))
          }, null, 8, ["flow", "flowType", "selectedScopes"])
        ]),
        _: 1
      }),
      r(o(i), { class: "min-w-full" }, {
        default: l(() => [
          S("div", Q, [
            t.scheme.type === "openIdConnect" ? (m(), v(o(V), {
              key: 0,
              class: "mr-1 p-0 px-2 py-0.5",
              loader: o(x),
              size: "sm",
              variant: "outlined",
              onClick: b
            }, {
              default: l(() => [...e[23] || (e[23] = [
                u(" Clear ", -1)
              ])]),
              _: 1
            }, 8, ["loader"])) : f("", !0),
            r(o(V), {
              class: "mr-0.75 p-0 px-2 py-0.5",
              loader: o(x),
              size: "sm",
              variant: "outlined",
              onClick: z
            }, {
              default: l(() => [...e[24] || (e[24] = [
                u(" Authorize ", -1)
              ])]),
              _: 1
            }, 8, ["loader"])
          ])
        ]),
        _: 1
      })
    ], 64));
  }
});
export {
  J as default
};
