"use client";
'use strict';

var react = require('react');
var Image = require('next/image');
var lucideReact = require('lucide-react');
var clsx = require('clsx');
var tailwindMerge = require('tailwind-merge');
var jsxRuntime = require('react/jsx-runtime');
var popover = require('@base-ui/react/popover');
var input = require('@base-ui/react/input');
var classVarianceAuthority = require('class-variance-authority');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var Image__default = /*#__PURE__*/_interopDefault(Image);

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
function cn(...inputs) {
  return tailwindMerge.twMerge(clsx.clsx(inputs));
}
function AccountSelectorChevronsIcon(_a) {
  var _b = _a, {
    className
  } = _b, props = __objRest(_b, [
    "className"
  ]);
  return /* @__PURE__ */ jsxRuntime.jsxs(
    "svg",
    __spreadProps(__spreadValues({
      viewBox: "0 0 24 24",
      fill: "none",
      xmlns: "http://www.w3.org/2000/svg",
      stroke: "currentColor",
      strokeWidth: "2",
      strokeLinecap: "round",
      strokeLinejoin: "round",
      className: cn("shrink-0", className),
      "aria-hidden": true
    }, props), {
      children: [
        /* @__PURE__ */ jsxRuntime.jsx("path", { d: "m7 6 5-5 5 5" }),
        /* @__PURE__ */ jsxRuntime.jsx("path", { d: "m7 18 5 5 5-5" })
      ]
    })
  );
}
function Popover(_a) {
  var props = __objRest(_a, []);
  return /* @__PURE__ */ jsxRuntime.jsx(popover.Popover.Root, __spreadValues({ "data-slot": "popover" }, props));
}
function PopoverTrigger(_a) {
  var props = __objRest(_a, []);
  return /* @__PURE__ */ jsxRuntime.jsx(popover.Popover.Trigger, __spreadValues({ "data-slot": "popover-trigger" }, props));
}
function PopoverContent(_a) {
  var _b = _a, {
    className,
    align = "center",
    alignOffset = 0,
    side = "bottom",
    sideOffset = 4,
    collisionPadding,
    collisionBoundary,
    collisionAvoidance
  } = _b, props = __objRest(_b, [
    "className",
    "align",
    "alignOffset",
    "side",
    "sideOffset",
    "collisionPadding",
    "collisionBoundary",
    "collisionAvoidance"
  ]);
  return /* @__PURE__ */ jsxRuntime.jsx(popover.Popover.Portal, { children: /* @__PURE__ */ jsxRuntime.jsx(
    popover.Popover.Positioner,
    {
      align,
      alignOffset,
      side,
      sideOffset,
      collisionPadding,
      collisionBoundary,
      collisionAvoidance,
      className: "isolate z-50",
      children: /* @__PURE__ */ jsxRuntime.jsx(
        popover.Popover.Popup,
        __spreadValues({
          "data-slot": "popover-content",
          className: cn(
            "z-50 flex w-72 origin-(--transform-origin) flex-col gap-2.5 rounded-sm bg-popover p-2.5 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
            className
          )
        }, props)
      )
    }
  ) });
}
classVarianceAuthority.cva(
  "text-xs leading-4",
  {
    variants: {
      variant: {
        default: "text-muted-foreground",
        error: "text-destructive",
        success: "text-[var(--theme-text-success)]"
      }
    },
    defaultVariants: {
      variant: "default"
    }
  }
);
var inputContainerVariants = classVarianceAuthority.cva(
  "flex w-full items-center overflow-clip rounded-[var(--radius-sm)] transition-[background-color,border-color] duration-150 has-[:focus-within]:transition-none has-[:focus-visible]:border-input-hover has-[:focus-visible]:ring-0 has-[:disabled]:pointer-events-none has-[:disabled]:opacity-50 has-[aria-invalid]:border-destructive has-[aria-invalid]:ring-3 has-[aria-invalid]:ring-destructive/20 dark:has-[aria-invalid]:border-destructive/50 dark:has-[aria-invalid]:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  {
    variants: {
      inputStyle: {
        default: "border border-border bg-card shadow-none hover:bg-[var(--theme-background-input-hover)] dark:bg-sidebar dark:hover:bg-[var(--theme-background-input-hover)] dark:has-[:disabled]:hover:bg-sidebar dark:has-[:disabled]:bg-input/80",
        soft: "border border-transparent bg-card hover:bg-[var(--theme-background-input-hover)] has-[:disabled]:hover:bg-card dark:bg-sidebar dark:hover:bg-[var(--theme-background-input-hover)] dark:has-[:disabled]:hover:bg-sidebar"
      },
      size: {
        sm: "min-h-8",
        /** Fixed height (not min-height) so flex children cannot grow the row past 36px (e.g. toolbar alignment). */
        lg: "h-9"
      }
    },
    defaultVariants: {
      inputStyle: "default",
      size: "sm"
    }
  }
);
function InputContainer(_a) {
  var _b = _a, {
    className,
    inputStyle = "default",
    size = "sm"
  } = _b, props = __objRest(_b, [
    "className",
    "inputStyle",
    "size"
  ]);
  return /* @__PURE__ */ jsxRuntime.jsx(
    "div",
    __spreadValues({
      "data-slot": "input-container",
      className: cn(
        inputContainerVariants({ inputStyle, size, className })
      )
    }, props)
  );
}
var inputVariants = classVarianceAuthority.cva(
  "w-full min-w-0 bg-transparent text-sm text-foreground outline-none transition-colors placeholder:text-muted-foreground file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground disabled:pointer-events-none disabled:cursor-not-allowed",
  {
    variants: {
      size: {
        sm: "h-8 px-2.5 py-1",
        /** `py-0` avoids UA + padding stacking past 36px; `leading-5` matches `text-sm` line box. */
        lg: "h-9 px-2.5 py-0 leading-5"
      },
      standalone: {
        true: "rounded-[var(--radius-sm)] border border-border bg-card shadow-none transition-[background-color,border-color] duration-150 hover:bg-[var(--theme-background-input-hover)] focus-visible:transition-none focus-visible:border-input-hover focus-visible:ring-0 disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:bg-sidebar dark:hover:bg-[var(--theme-background-input-hover)] dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
        false: "border-none focus-visible:ring-0 focus-visible:ring-offset-0"
      }
    },
    defaultVariants: {
      size: "sm",
      standalone: true
    }
  }
);
function Input(_a) {
  var _b = _a, {
    className,
    type,
    size = "sm",
    standalone = true
  } = _b, props = __objRest(_b, [
    "className",
    "type",
    "size",
    "standalone"
  ]);
  return /* @__PURE__ */ jsxRuntime.jsx(
    input.Input,
    __spreadValues({
      type,
      "data-slot": "input",
      className: cn(inputVariants({ size, standalone, className }))
    }, props)
  );
}
function InputIcon(_a) {
  var _b = _a, {
    className,
    position = "lead",
    children
  } = _b, props = __objRest(_b, [
    "className",
    "position",
    "children"
  ]);
  return /* @__PURE__ */ jsxRuntime.jsx(
    "div",
    __spreadProps(__spreadValues({
      "data-slot": "input-icon",
      "data-position": position,
      className: cn(
        "flex shrink-0 items-center justify-center text-muted-foreground",
        position === "lead" ? "pl-2.5" : "pr-2.5",
        className
      )
    }, props), {
      children
    })
  );
}
function filterSidebarAccountsByQuery(accounts, query) {
  const q = query.trim().toLowerCase();
  if (!q) return accounts;
  return accounts.filter(
    (a) => a.primaryText.toLowerCase().includes(q) || a.secondaryText.toLowerCase().includes(q)
  );
}
function getPersonInitialsFromName(primaryText, explicitInitials) {
  var _a, _b;
  const raw = explicitInitials == null ? void 0 : explicitInitials.trim();
  if (raw) {
    return raw.length >= 2 ? raw.slice(0, 2).toUpperCase() : `${(_a = raw[0]) != null ? _a : "?"}${(_b = raw[0]) != null ? _b : "?"}`.toUpperCase();
  }
  const parts = primaryText.trim().split(/\s+/).filter(Boolean);
  if (parts.length === 0) return "??";
  if (parts.length === 1) return parts[0].slice(0, 2).toUpperCase();
  const first = parts[0][0];
  const last = parts[parts.length - 1][0];
  return `${first != null ? first : "?"}${last != null ? last : "?"}`.toUpperCase();
}
var ACCOUNT_CHIP_LABEL_TRANSITION_CLASS = "overflow-hidden transition-[max-width,opacity] duration-200 ease-[cubic-bezier(0.4,0,0.2,1)] motion-reduce:transition-none";
var FOOTER_NAV_MATCH_GAP_CLASS = "transition-[gap] duration-200 ease-[cubic-bezier(0.4,0,0.2,1)] motion-reduce:transition-none";
var ACCOUNT_CHIP_BOX_CLASS = "h-[58px] min-h-[58px]";
function resolveActiveAccount(accounts, activeAccountId) {
  if (activeAccountId) {
    const match = accounts.find((a) => a.id === activeAccountId);
    if (match) {
      return match;
    }
  }
  return accounts[0];
}
function SidebarAccountSwitcher({
  accounts,
  activeAccountId,
  onAccountChange,
  onLogOut,
  collapsed,
  anchorAtFooter = false
}) {
  var _a;
  const [open, setOpen] = react.useState(false);
  const [searchQuery, setSearchQuery] = react.useState("");
  const titleId = react.useId();
  const activeAccount = react.useMemo(
    () => resolveActiveAccount(accounts, activeAccountId),
    [accounts, activeAccountId]
  );
  const filteredAccounts = react.useMemo(
    () => filterSidebarAccountsByQuery(accounts, searchQuery),
    [accounts, searchQuery]
  );
  function handleOpenChange(next) {
    setOpen(next);
    if (!next) {
      setSearchQuery("");
    }
  }
  const displayInitials = (_a = activeAccount.initials) != null ? _a : activeAccount.primaryText.slice(0, 2).toUpperCase();
  const footerPersonInitials = react.useMemo(
    () => getPersonInitialsFromName(activeAccount.primaryText, activeAccount.initials),
    [activeAccount.primaryText, activeAccount.initials]
  );
  const popoverSide = collapsed ? "right" : anchorAtFooter ? "top" : "bottom";
  const useFooterUserChrome = Boolean(anchorAtFooter);
  return /* @__PURE__ */ jsxRuntime.jsx(
    "div",
    {
      className: cn(
        // Chip stays full-width in both states and shares the nav's side padding,
        // so the collapsed logo lands in the same icon column as the nav rows.
        "flex w-full justify-start",
        anchorAtFooter ? "pl-[var(--spacing-16)] pr-[var(--spacing-16)]" : "px-[var(--spacing-16)]"
      ),
      style: {
        paddingTop: 0,
        paddingBottom: anchorAtFooter ? 0 : "var(--spacing-8)"
      },
      children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full min-w-0", children: /* @__PURE__ */ jsxRuntime.jsxs(Popover, { open, onOpenChange: handleOpenChange, children: [
        /* @__PURE__ */ jsxRuntime.jsxs(
          PopoverTrigger,
          {
            type: "button",
            title: collapsed ? activeAccount.primaryText : void 0,
            "aria-haspopup": "dialog",
            "aria-expanded": open,
            className: cn(
              "box-border flex cursor-pointer items-center rounded-[var(--radius-md)] transition-colors",
              // Constant `justify-center` + the `flex-1` label below: the logo/
              // avatar stays pinned left while the chip is wide, then glides to
              // center as the label collapses — monotonic, no overshoot.
              useFooterUserChrome ? cn(
                "w-full min-w-0",
                "border-0 bg-transparent pt-[var(--spacing-4)] pl-[var(--spacing-4)] pb-[var(--spacing-4)] pr-[var(--spacing-10)] hover:bg-sidebar-accent",
                "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[color:var(--sidebar-ring)] focus-visible:ring-offset-2 focus-visible:ring-offset-[color:var(--sidebar)]",
                FOOTER_NAV_MATCH_GAP_CLASS,
                "justify-center overflow-hidden",
                collapsed ? "h-[38px] gap-0" : "h-auto min-h-[38px] items-center gap-[var(--spacing-8)]"
              ) : cn(
                "pt-[var(--spacing-8)] px-[var(--spacing-8)] pb-[var(--spacing-8)]",
                "border border-border bg-[var(--theme-background-account-selector)] hover:bg-neutral-100",
                ACCOUNT_CHIP_BOX_CLASS,
                FOOTER_NAV_MATCH_GAP_CLASS,
                "w-full justify-center overflow-hidden",
                collapsed ? "gap-0" : "gap-[var(--spacing-8)]"
              )
            ),
            children: [
              useFooterUserChrome ? /* @__PURE__ */ jsxRuntime.jsx(
                "div",
                {
                  className: "box-border flex size-[34px] shrink-0 items-center justify-center rounded-full border border-border bg-neutral-800 text-center text-xs font-semibold leading-none text-white antialiased dark:border-neutral-600 dark:bg-neutral-900",
                  "aria-hidden": true,
                  children: footerPersonInitials
                }
              ) : /* @__PURE__ */ jsxRuntime.jsx(
                "div",
                {
                  className: "relative box-border flex size-10 shrink-0 items-center justify-center overflow-hidden rounded-[var(--radius-md)] border border-border bg-muted text-muted-foreground dark:border-neutral-600",
                  style: { fontSize: "var(--text-sm)" },
                  "aria-hidden": true,
                  children: activeAccount.logoSrc ? /* @__PURE__ */ jsxRuntime.jsx(
                    Image__default.default,
                    {
                      src: activeAccount.logoSrc,
                      alt: "",
                      fill: true,
                      sizes: "40px",
                      unoptimized: true,
                      className: "size-full object-cover object-center"
                    }
                  ) : displayInitials
                }
              ),
              /* @__PURE__ */ jsxRuntime.jsxs(
                "span",
                {
                  className: cn(
                    "flex min-w-0 flex-1 items-center overflow-hidden text-left",
                    "gap-[var(--spacing-8)]",
                    ACCOUNT_CHIP_LABEL_TRANSITION_CLASS,
                    collapsed ? "max-w-0 opacity-0" : "max-w-full opacity-100"
                  ),
                  "aria-hidden": collapsed,
                  children: [
                    /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "min-w-0 flex-1 flex flex-col gap-0 self-center", children: [
                      /* @__PURE__ */ jsxRuntime.jsx(
                        "p",
                        {
                          className: "truncate font-medium leading-[var(--leading-5)]",
                          style: {
                            color: "var(--theme-text-primary)",
                            fontSize: "var(--text-xs)"
                          },
                          children: activeAccount.primaryText
                        }
                      ),
                      /* @__PURE__ */ jsxRuntime.jsx(
                        "p",
                        {
                          className: cn(
                            "truncate font-normal",
                            useFooterUserChrome && "-mt-0.5"
                          ),
                          style: {
                            color: "var(--theme-text-secondary)",
                            fontSize: "var(--text-xs)"
                          },
                          children: activeAccount.secondaryText
                        }
                      )
                    ] }),
                    useFooterUserChrome ? /* @__PURE__ */ jsxRuntime.jsx(
                      lucideReact.MoreHorizontal,
                      {
                        className: "size-4 shrink-0 text-muted-foreground",
                        "aria-hidden": true
                      }
                    ) : /* @__PURE__ */ jsxRuntime.jsx(
                      AccountSelectorChevronsIcon,
                      {
                        className: "size-4 shrink-0 text-muted-foreground",
                        "aria-hidden": true
                      }
                    )
                  ]
                }
              )
            ]
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsxs(
          PopoverContent,
          {
            side: popoverSide,
            align: "start",
            sideOffset: 6,
            collisionPadding: 12,
            collisionAvoidance: { side: "flip", align: "shift" },
            className: cn(
              "w-[260px] max-w-[min(100vw-2rem,260px)] gap-0 rounded-[var(--radius-md)] border border-border bg-card p-[var(--spacing-8)] shadow-lg ring-0",
              "data-[side=bottom]:slide-in-from-top-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2"
            ),
            children: [
              /* @__PURE__ */ jsxRuntime.jsx("p", { className: "sr-only", id: titleId, children: "Select account" }),
              /* @__PURE__ */ jsxRuntime.jsxs(
                "div",
                {
                  className: "flex flex-col gap-[var(--spacing-8)]",
                  role: "dialog",
                  "aria-labelledby": titleId,
                  children: [
                    /* @__PURE__ */ jsxRuntime.jsxs(InputContainer, { size: "sm", inputStyle: "default", className: "w-full", children: [
                      /* @__PURE__ */ jsxRuntime.jsx(InputIcon, { position: "lead", children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Search, { className: "size-4 text-muted-foreground", "aria-hidden": true }) }),
                      /* @__PURE__ */ jsxRuntime.jsx(
                        Input,
                        {
                          type: "search",
                          size: "sm",
                          standalone: false,
                          placeholder: "Search accounts\u2026",
                          value: searchQuery,
                          onChange: (e) => setSearchQuery(e.target.value),
                          autoComplete: "off",
                          "aria-label": "Search accounts"
                        }
                      )
                    ] }),
                    /* @__PURE__ */ jsxRuntime.jsx(
                      "div",
                      {
                        className: "max-h-[min(50vh,320px)] overflow-y-auto rounded-[var(--radius-md)] border border-border",
                        role: "listbox",
                        "aria-label": "Accounts",
                        children: filteredAccounts.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(
                          "p",
                          {
                            className: "px-[var(--spacing-8)] py-[var(--spacing-10)] text-center text-xs text-muted-foreground",
                            role: "status",
                            children: "No accounts match"
                          }
                        ) : filteredAccounts.map((account) => {
                          var _a2;
                          const isSelected = account.id === activeAccount.id;
                          const rowInitials = (_a2 = account.initials) != null ? _a2 : account.primaryText.slice(0, 2).toUpperCase();
                          return /* @__PURE__ */ jsxRuntime.jsxs(
                            "button",
                            {
                              type: "button",
                              role: "option",
                              "aria-selected": isSelected,
                              onClick: () => {
                                onAccountChange == null ? void 0 : onAccountChange(account.id);
                                setOpen(false);
                              },
                              className: cn(
                                "flex w-full items-center gap-[var(--spacing-8)] px-[var(--spacing-8)] py-[var(--spacing-6)] text-left transition-colors first:rounded-t-[calc(var(--radius-md)-1px)] last:rounded-b-[calc(var(--radius-md)-1px)]",
                                isSelected ? "bg-muted" : "hover:bg-neutral-100"
                              ),
                              children: [
                                /* @__PURE__ */ jsxRuntime.jsx(
                                  "div",
                                  {
                                    className: "relative box-border flex size-10 shrink-0 items-center justify-center overflow-hidden rounded-[var(--radius-md)] border border-border bg-muted text-muted-foreground dark:border-neutral-600",
                                    style: { fontSize: "var(--text-sm)" },
                                    "aria-hidden": true,
                                    children: account.logoSrc ? /* @__PURE__ */ jsxRuntime.jsx(
                                      Image__default.default,
                                      {
                                        src: account.logoSrc,
                                        alt: "",
                                        fill: true,
                                        sizes: "40px",
                                        unoptimized: true,
                                        className: "size-full object-cover object-center"
                                      }
                                    ) : rowInitials
                                  }
                                ),
                                /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "min-w-0 flex-1 flex flex-col gap-0 self-center", children: [
                                  /* @__PURE__ */ jsxRuntime.jsx(
                                    "span",
                                    {
                                      className: "block truncate font-medium leading-[var(--leading-5)]",
                                      style: {
                                        color: "var(--theme-text-primary)",
                                        fontSize: "var(--text-xs)"
                                      },
                                      children: account.primaryText
                                    }
                                  ),
                                  /* @__PURE__ */ jsxRuntime.jsx(
                                    "span",
                                    {
                                      className: "block truncate font-normal",
                                      style: {
                                        color: "var(--theme-text-secondary)",
                                        fontSize: "var(--text-xs)"
                                      },
                                      children: account.secondaryText
                                    }
                                  )
                                ] })
                              ]
                            },
                            account.id
                          );
                        })
                      }
                    ),
                    onLogOut ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "border-t border-border pt-[var(--spacing-6)]", children: /* @__PURE__ */ jsxRuntime.jsxs(
                      "button",
                      {
                        type: "button",
                        onClick: () => {
                          setOpen(false);
                          onLogOut();
                        },
                        className: "flex w-full items-center gap-[var(--spacing-6)] rounded-[var(--radius-sm)] p-[var(--spacing-6)] text-left text-sm font-medium text-[var(--theme-text-primary)] transition-colors hover:bg-neutral-100",
                        children: [
                          /* @__PURE__ */ jsxRuntime.jsx(lucideReact.LogOut, { className: "size-4 shrink-0 text-muted-foreground", "aria-hidden": true }),
                          "Log out"
                        ]
                      }
                    ) }) : null
                  ]
                }
              )
            ]
          }
        )
      ] }) })
    }
  );
}

exports.ACCOUNT_CHIP_BOX_CLASS = ACCOUNT_CHIP_BOX_CLASS;
exports.SidebarAccountSwitcher = SidebarAccountSwitcher;
//# sourceMappingURL=sidebar-account-switcher.cjs.map
//# sourceMappingURL=sidebar-account-switcher.cjs.map