import { j as jsxRuntimeExports } from "./jsx-runtime-mMz73N64.js";
import { createContext, useCallback, useContext } from "react";
import { o as useSyncedRef, I as Icon, p as m0, W as WithIcon, e as Text, E as Editable } from "./Toggle-DcOiWiHG.js";
import { S as Space, P as Pack } from "./Center-hbwH7rg9.js";
import { C as CSS, d as f } from "./css-DoalWJF6.js";
import { h as usePurePassthrough, C as ComponentSizeLevels } from "./aggregator-Dl2jHye5.js";
const resetSelection = (selected = "", tabs = []) => {
  var _a;
  if (tabs.length === 0)
    return void 0;
  return tabs.find((t) => t.tabKey === selected) != null ? selected : (_a = tabs[tabs.length - 1]) == null ? void 0 : _a.tabKey;
};
const rename = (key, title, tabs) => {
  title = title.trim();
  if (title.length === 0)
    return tabs;
  const t = tabs.find((t2) => t2.tabKey === key);
  if (t == null || t.name === title)
    return tabs;
  return tabs.map((t2) => t2.tabKey === key ? { ...t2, name: title } : t2);
};
const useStatic = ({
  tabs,
  content,
  selected,
  onSelect
}) => {
  var _a;
  const [value, onChange] = usePurePassthrough({
    initial: selected ?? ((_a = tabs[0]) == null ? void 0 : _a.tabKey) ?? "",
    value: selected,
    onChange: onSelect
  });
  const valueRef = useSyncedRef(selected ?? value);
  const handleSelect = useCallback(
    (key) => {
      onChange(key);
      if (valueRef.current == null)
        onSelect == null ? void 0 : onSelect(key);
    },
    [value, onSelect]
  );
  return {
    tabs,
    selected: value,
    content,
    onSelect: handleSelect
  };
};
const TabsContext = createContext({ tabs: [] });
const useTabsContext = () => useContext(TabsContext);
const Tabs = ({
  id,
  content,
  children,
  onSelect,
  selected,
  closable,
  tabs,
  onClose,
  onDragStart,
  onDragEnd,
  onCreate,
  onRename,
  emptyContent,
  className,
  onDragOver,
  onDrop,
  size = "medium",
  direction: dir = "y",
  ...props
}) => /* @__PURE__ */ jsxRuntimeExports.jsx(
  Space,
  {
    id,
    empty: true,
    className: CSS(CSS.B("tabs"), className),
    onDragOver,
    onDrop,
    direction: dir,
    ...props,
    children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
      TabsContext.Provider,
      {
        value: {
          tabs,
          emptyContent,
          selected,
          closable,
          content: children ?? content,
          onSelect,
          onClose,
          onDragStart,
          onDragEnd,
          onRename,
          onCreate,
          onDrop
        },
        children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Selector, { size, direction: f.swap(dir) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(Content, {})
        ]
      }
    )
  }
);
const Provider = TabsContext.Provider;
const Content = () => {
  const {
    tabs,
    selected,
    content: renderProp,
    emptyContent,
    onSelect
  } = useTabsContext();
  let content = null;
  const selectedTab = tabs.find((tab) => tab.tabKey === selected);
  if (selected == null || selectedTab == null)
    return emptyContent ?? null;
  if (renderProp != null) {
    if (typeof renderProp === "function")
      content = renderProp(selectedTab);
    else
      content = renderProp;
  } else if (selectedTab.content != null)
    content = selectedTab.content;
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: CSS.B("tabs-content"), onClick: () => onSelect == null ? void 0 : onSelect(selected), children: content });
};
const CLS = "tabs-selector";
const Selector = ({
  className,
  size = "medium",
  direction = "x",
  ...props
}) => {
  const {
    tabs,
    selected,
    onSelect,
    onClose,
    closable,
    onDragEnd,
    onDragStart,
    onDrop,
    onRename,
    onCreate
  } = useTabsContext();
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    Space,
    {
      className: CSS(CSS.B(CLS), CSS.size(size), className),
      align: "center",
      justify: "spaceBetween",
      onDrop,
      empty: true,
      direction,
      ...props,
      children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Space, { direction, className: CSS.BE(CLS, "tabs"), empty: true, children: tabs.map((tab) => /* @__PURE__ */ jsxRuntimeExports.jsx(
          SelectorButton,
          {
            selected,
            onSelect,
            onClose,
            onDragStart,
            onDragEnd,
            onRename,
            closable: tab.closable ?? closable,
            size,
            ...tab
          },
          tab.tabKey
        )) }),
        onCreate != null && /* @__PURE__ */ jsxRuntimeExports.jsx(Space, { className: CSS.BE(CLS, "actions"), children: /* @__PURE__ */ jsxRuntimeExports.jsx(Icon, { size, sharp: true, onClick: onCreate, children: /* @__PURE__ */ jsxRuntimeExports.jsx(m0.Add, {}) }) })
      ]
    }
  );
};
const SelectorButton = ({
  selected,
  onSelect,
  onClose,
  tabKey,
  name,
  onDragStart,
  onDragEnd,
  onRename,
  closable = true,
  icon,
  size,
  editable = true
}) => {
  const handleDragStart = useCallback(
    (e) => onDragStart == null ? void 0 : onDragStart(e, { tabKey, name }),
    [onDragStart, tabKey, name]
  );
  const handleDragEnd = useCallback(
    (e) => onDragEnd == null ? void 0 : onDragEnd(e, { tabKey, name }),
    [onDragEnd, tabKey, name]
  );
  const handleClose = useCallback(
    (e) => {
      e.stopPropagation();
      onClose == null ? void 0 : onClose(tabKey);
    },
    [onClose, tabKey]
  );
  const _onSelect = useCallback(() => onSelect == null ? void 0 : onSelect(tabKey), [onSelect, tabKey]);
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    Pack,
    {
      size,
      className: CSS(
        CSS.BE(CLS, "btn"),
        onRename == null && CSS.BEM(CLS, "btn", "uneditable"),
        CSS.selected(selected === tabKey),
        closable && onClose != null && CSS.BEM(CLS, "btn", "closable")
      ),
      draggable: true,
      direction: "x",
      justify: "center",
      align: "center",
      onClick: _onSelect,
      onDragStart: handleDragStart,
      onDragEnd: handleDragEnd,
      bordered: false,
      rounded: false,
      children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          Name,
          {
            name,
            tabKey,
            onRename,
            icon,
            editable,
            level: ComponentSizeLevels[size]
          }
        ),
        closable && onClose != null && /* @__PURE__ */ jsxRuntimeExports.jsx(Icon, { onClick: handleClose, children: /* @__PURE__ */ jsxRuntimeExports.jsx(m0.Close, { "aria-label": "pluto-tabs__close" }) })
      ]
    }
  );
};
const Name = ({
  onRename,
  name,
  tabKey,
  icon,
  editable = true,
  ...props
}) => {
  if (onRename == null || !editable) {
    if (icon != null)
      return /* @__PURE__ */ jsxRuntimeExports.jsx(WithIcon, { startIcon: icon, noWrap: true, ...props, children: name });
    return /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { noWrap: true, ...props, children: name });
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
    Editable,
    {
      onChange: (newText) => onRename(tabKey, newText),
      value: name,
      noWrap: true,
      ...props
    }
  );
};
const external = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  Content,
  Provider,
  Selector,
  Tabs,
  TabsContext,
  rename,
  resetSelection,
  useStatic,
  useTabsContext
}, Symbol.toStringTag, { value: "Module" }));
export {
  Tabs as T,
  rename as a,
  external as e,
  resetSelection as r,
  useStatic as u
};
//# sourceMappingURL=external-CtH30naU.js.map
