import { defineComponent as U, useId as $, ref as v, computed as S, createBlock as x, openBlock as c, unref as s, withCtx as r, createVNode as m, createCommentVNode as p, createElementBlock as y, Fragment as b, createElementVNode as u, createTextVNode as g, toDisplayString as f, withModifiers as q, normalizeClass as M } from "vue";
import { useModal as N, ScalarComboboxMultiselect as T, ScalarButton as j, ScalarListboxCheckbox as z, ScalarIconButton as E } from "@scalar/components";
import { ScalarIconCaretDown as L, ScalarIconTrash as P } from "@scalar/icons";
import { getResolvedRef as F } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { unpackProxyObject as G } from "@scalar/workspace-store/helpers/unpack-proxy";
import H from "./DeleteRequestAuthModal.vue.js";
import { isAuthOptional as J } from "../helpers/is-auth-optional.js";
import { getSecuritySchemeOptions as K, formatComplexScheme as Q, formatScheme as W } from "../helpers/security-scheme.js";
import X from "./RequestAuthDataTable.vue.js";
import Y from "../../../components/layout/CollapsibleSection.vue.js";
const Z = ["id"], _ = { class: "min-w-0 flex-1 truncate" }, ue = /* @__PURE__ */ U({
  __name: "AuthSelector",
  props: {
    environment: {},
    eventBus: {},
    createAnySecurityScheme: { type: Boolean, default: !1 },
    isStatic: { type: Boolean, default: !1 },
    meta: {},
    proxyUrl: {},
    securityRequirements: {},
    securitySchemes: {},
    selectedSecurity: {},
    server: {},
    title: {}
  },
  setup(e, { expose: A }) {
    const k = $(), C = v(null), B = v(!1), d = N(), o = v(null), h = S(() => {
      if (!e.securityRequirements?.length)
        return null;
      const l = J(e.securityRequirements);
      return {
        icon: l ? "Unlock" : "Lock",
        text: l ? "Optional" : "Required"
      };
    }), O = S(
      () => K(
        e.securityRequirements ?? [],
        e.securitySchemes ?? {},
        e.selectedSecurity?.selectedSchemes ?? [],
        e.createAnySecurityScheme
      )
    ), i = S(() => {
      const l = e.selectedSecurity?.selectedSchemes;
      return l?.length ? l.flatMap((t) => {
        const n = Object.keys(t);
        if (n.length === 0)
          return [];
        if (n.length > 1)
          return Q(t);
        const a = n[0];
        return a ? F(e.securitySchemes?.[a]) ? W({
          name: a,
          value: t
        }) : [] : [];
      }) : [];
    }), D = (l) => {
      B.value && l.stopPropagation(), C.value?.$el.click();
    }, I = (l) => {
      const t = l.filter((a) => a.payload === void 0).map((a) => G(a.value, { depth: 2 })), n = l.filter((a) => a.payload !== void 0).map((a) => ({
        name: a.label,
        scheme: a.payload
      }));
      e.eventBus.emit("auth:update:selected-security-schemes", {
        selectedRequirements: t,
        newSchemes: n,
        meta: e.meta
      });
    }, R = (l) => {
      o.value = { label: l.label, payload: l.value }, d.show();
    }, w = () => {
      o.value && (e.eventBus.emit("auth:delete:security-scheme", {
        names: Object.keys(o.value.payload)
      }), o.value = null, d.hide());
    };
    return A({
      authIndicator: h,
      selectedSchemeOptions: i,
      schemeOptions: O
    }), (l, t) => (c(), x(s(Y), {
      class: "group/params relative",
      isStatic: e.isStatic,
      itemCount: i.value.length,
      "onUpdate:modelValue": t[1] || (t[1] = (n) => B.value = n)
    }, {
      title: r(() => [
        u("div", {
          id: s(k),
          class: "inline-flex items-center gap-0.5 leading-[20px]"
        }, [
          u("span", null, f(e.title), 1),
          h.value ? (c(), y("span", {
            key: 0,
            class: M(["text-c-3 hover:bg-b-3 hover:text-c-1 -my-0.5 -mr-1 cursor-pointer rounded px-1 py-0.5 leading-[normal] font-normal", { "text-c-1": h.value.text === "Required" }]),
            "data-testid": "auth-indicator",
            onClick: D
          }, f(h.value.text), 3)) : p("", !0)
        ], 8, Z)
      ]),
      actions: r(() => [
        m(s(T), {
          class: "w-72 text-xs",
          modelValue: i.value,
          multiple: "",
          options: O.value,
          placement: "bottom-end",
          teleport: "",
          onDelete: R,
          "onUpdate:modelValue": I
        }, {
          option: r(({ option: n, selected: a }) => [
            m(s(z), {
              multiselect: "",
              selected: a
            }, null, 8, ["selected"]),
            u("div", _, f(n.label), 1),
            n.isDeletable ? (c(), x(s(E), {
              key: 0,
              class: "-m-0.5 shrink-0 p-0.5 opacity-0 group-hover/item:opacity-100",
              icon: s(P),
              label: `Delete ${n.label}`,
              size: "xs",
              onClick: q((V) => R(n), ["stop"])
            }, null, 8, ["icon", "label", "onClick"])) : p("", !0)
          ]),
          default: r(() => [
            m(s(j), {
              ref_key: "comboboxButtonRef",
              ref: C,
              "aria-describedby": s(k),
              class: "group/combobox-button hover:text-c-1 text-c-2 flex h-fit w-full items-center gap-1 px-0.75 py-0.25 text-base font-normal",
              variant: "ghost"
            }, {
              default: r(() => [
                i.value.length === 1 ? (c(), y(b, { key: 0 }, [
                  t[2] || (t[2] = u("span", { class: "sr-only" }, "Selected Auth Type:", -1)),
                  g(" " + f(i.value[0]?.label), 1)
                ], 64)) : i.value.length > 1 ? (c(), y(b, { key: 1 }, [
                  t[3] || (t[3] = g(" Multiple ", -1)),
                  t[4] || (t[4] = u("span", { class: "sr-only" }, "Auth Types Selected", -1))
                ], 64)) : (c(), y(b, { key: 2 }, [
                  t[5] || (t[5] = u("span", { class: "sr-only" }, "Select", -1)),
                  t[6] || (t[6] = g(" Auth Type ", -1))
                ], 64)),
                m(s(L), {
                  class: "size-3 shrink-0 transition-transform duration-100 group-aria-expanded/combobox-button:rotate-180",
                  weight: "bold"
                })
              ]),
              _: 1
            }, 8, ["aria-describedby"])
          ]),
          _: 1
        }, 8, ["modelValue", "options"])
      ]),
      default: r(() => [
        m(X, {
          activeAuthIndex: e.selectedSecurity?.selectedIndex ?? 0,
          environment: e.environment,
          eventBus: e.eventBus,
          isStatic: e.isStatic,
          meta: e.meta,
          proxyUrl: e.proxyUrl,
          securitySchemes: e.securitySchemes,
          selectedSchemeOptions: i.value,
          server: e.server
        }, null, 8, ["activeAuthIndex", "environment", "eventBus", "isStatic", "meta", "proxyUrl", "securitySchemes", "selectedSchemeOptions", "server"]),
        o.value ? (c(), x(H, {
          key: 0,
          label: o.value.label,
          scheme: o.value,
          state: s(d),
          onClose: t[0] || (t[0] = (n) => s(d).hide()),
          onDelete: w
        }, null, 8, ["label", "scheme", "state"])) : p("", !0)
      ]),
      _: 1
    }, 8, ["isStatic", "itemCount"]));
  }
});
export {
  ue as default
};
