import { isObject as p } from "@scalar/helpers/object/is-object";
import { objectEntries as u } from "@scalar/helpers/object/object-entries";
const f = {
  "x-scalar-secret-client-id": "x-scalar-client-id",
  "x-scalar-secret-client-secret": "clientSecret",
  "x-scalar-secret-password": "password",
  "x-scalar-secret-redirect-uri": "x-scalar-redirect-uri",
  "x-scalar-secret-token": "token",
  "x-scalar-secret-username": "username",
  "x-scalar-secret-auth-url": "authorizationUrl",
  "x-scalar-secret-token-url": "tokenUrl"
}, i = (t, s, n = {}) => Object.fromEntries(
  t.map((c) => {
    const r = n[c] || s[c] || s[f[c]] || "";
    return [c, r];
  })
), l = (t = {}) => {
  const s = t["x-scalar-secret-refresh-token"];
  return typeof s == "string" ? { "x-scalar-secret-refresh-token": s } : {};
}, x = (t, s) => {
  const n = /* @__PURE__ */ new Set();
  return { flows: u(t ?? {}).reduce((r, [e, a]) => {
    if (!p(a))
      return r;
    const o = a.selectedScopes;
    return Array.isArray(o) && o.forEach((d) => typeof d == "string" && n.add(d)), e === "implicit" && (r.implicit = {
      ...a,
      ...i(
        [
          "x-scalar-secret-client-id",
          "x-scalar-secret-redirect-uri",
          "x-scalar-secret-token",
          "x-scalar-secret-auth-url"
        ],
        a,
        s?.implicit
      ),
      ...l(s?.implicit)
    }), e === "password" && (r[e] = {
      ...a,
      ...i(
        [
          "x-scalar-secret-client-id",
          "x-scalar-secret-client-secret",
          "x-scalar-secret-username",
          "x-scalar-secret-password",
          "x-scalar-secret-token",
          "x-scalar-secret-token-url"
        ],
        a,
        s?.password
      ),
      ...l(s?.password)
    }), e === "clientCredentials" && (r[e] = {
      ...a,
      ...i(
        [
          "x-scalar-secret-client-id",
          "x-scalar-secret-client-secret",
          "x-scalar-secret-token",
          "x-scalar-secret-token-url"
        ],
        a,
        s?.clientCredentials
      ),
      ...l(s?.clientCredentials)
    }), e === "authorizationCode" && (r[e] = {
      ...a,
      ...i(
        [
          "x-scalar-secret-client-id",
          "x-scalar-secret-client-secret",
          "x-scalar-secret-redirect-uri",
          "x-scalar-secret-token",
          "x-scalar-secret-auth-url",
          "x-scalar-secret-token-url"
        ],
        a,
        s?.authorizationCode
      ),
      ...l(s?.authorizationCode)
    }), r;
  }, {}), selectedScopes: Array.from(n) };
}, C = (t, s, n, c) => {
  const r = s.getAuthSecrets(c, n);
  if (t.type === "apiKey") {
    const e = r?.type === "apiKey" ? r : void 0;
    return {
      ...t,
      "x-scalar-secret-token": e?.["x-scalar-secret-token"] || t.value || ""
    };
  }
  if (t.type === "http") {
    const e = r?.type === "http" ? r : void 0;
    return {
      ...t,
      "x-scalar-secret-token": e?.["x-scalar-secret-token"] || t.token || "",
      "x-scalar-secret-username": e?.["x-scalar-secret-username"] || t.username || "",
      "x-scalar-secret-password": e?.["x-scalar-secret-password"] || t.password || ""
    };
  }
  if (t.type === "oauth2") {
    const e = r?.type === "oauth2" ? r : void 0, a = x(t.flows, e);
    return {
      ...t,
      flows: a.flows,
      "x-default-scopes": a.selectedScopes
    };
  }
  if (t.type === "openIdConnect") {
    const e = r?.type === "openIdConnect" ? r : void 0, a = x(
      {
        implicit: e?.implicit,
        password: e?.password,
        clientCredentials: e?.clientCredentials,
        authorizationCode: e?.authorizationCode
      },
      e
    );
    return {
      ...t,
      ...u(a.flows).length ? { flows: a.flows } : {}
    };
  }
  return t;
};
export {
  C as extractSecuritySchemeSecrets
};
