import{n as e,r as t}from"./rolldown-runtime-C0FnF6B9.js";import{t as n}from"./react-C21x__mS.js";import{n as r,t as i}from"./check-fB4gCKB_.js";import{n as a}from"./iframe-DaN-XFub.js";import{a as o,i as s,n as c,r as l,s as u,t as d}from"./utils-DUM89eIj.js";import{n as f,t as p}from"./ui.module-DoMIk8Oc.js";function m(e,t,n){if(n){if(Array.isArray(t)&&t.length>0){let n=e.findIndex(e=>t.includes(e.value)&&!e.disabled);if(n>=0)return n}return l(e,v)}if(t!==void 0){let n=e.findIndex(e=>e.value===t&&!e.disabled);if(n>=0)return n}return l(e,v)}function h(e){let{value:t,defaultValue:n,multiple:r,focusOnMount:a,renderItem:s,getKey:p,options:h,onChange:y,id:b,...x}=e,S=t!==void 0,C=(0,g.useId)(),w=b??`${C}-listbox`,[T,E]=(0,g.useState)(n),D=(0,g.useRef)(null),[O,k]=(0,g.useState)(()=>m(h,t??n,r)),A=(0,g.useMemo)(()=>(S?t:T)??(r?[]:null),[T,S,r,t]),j=O,M=t=>{if(t.disabled)return;let n;if(e.multiple){let r=Array.isArray(A)?A:[];n=r.includes(t.value)?r.filter(e=>e!==t.value):[...r,t.value],e.onChange?.(n,t)}else n=t.value,e.onChange?.(n,t);S||E(n)};return(0,g.useEffect)(()=>{let e=D.current?.querySelectorAll(`[role="option"]`)[j];e&&e.scrollIntoView({block:`nearest`})},[j]),(0,g.useLayoutEffect)(()=>{a&&D.current?.focus()},[a]),(0,_.jsx)(`ul`,{ref:D,id:w,role:`listbox`,"aria-multiselectable":r||void 0,"aria-activedescendant":j>=0?`${w}-option-${j}`:void 0,tabIndex:0,onKeyDown:e=>{if(e.key===`ArrowDown`)e.preventDefault(),k(e=>d(h,e,1,v));else if(e.key===`ArrowUp`)e.preventDefault(),k(e=>d(h,e,-1,v));else if(e.key===`Home`)e.preventDefault(),k(l(h,v));else if(e.key===`End`)e.preventDefault(),k(u(h,v));else if(c(e,()=>{let e=h[j];e&&M(e)}))return},...x,className:o(f.uiPanel,`scrollable`),children:h.map((e,t)=>{let n=r?Array.isArray(A)&&A.includes(e.value):A===e.value,a=t===j;return(0,_.jsxs)(`li`,{id:`${w}-option-${t}`,role:`option`,"aria-selected":n,"aria-disabled":e.disabled||void 0,onMouseDown:n=>{n.preventDefault(),!e.disabled&&(k(t),M(e))},tabIndex:-1,className:o(f.uiPopupOption,e.disabled&&f.disabled,a&&f.uiPopupOptionActive,n&&f.selected),children:[n?(0,_.jsx)(i,{className:f.uiPopupOptionIcon}):(0,_.jsx)(`span`,{className:f.uiPopupOptionIcon}),(0,_.jsx)(`div`,{children:s?s(e,n,a):e.label??String(e.value)})]},p?p(e,t):t)})})}var g,_,v;function y(){return(y=e((()=>{g=n(),s(),r(),p(),_=a(),v=e=>e.disabled===!0,h.__docgenInfo={description:``,methods:[],displayName:`Listbox`}})))()}var b=t({CustomRender:()=>D,MultiSelect:()=>E,SingleSelect:()=>T,__namedExportsOrder:()=>O,default:()=>w});function x(e){return e.multiple?(0,C.jsx)(h,{...e,multiple:!0}):(0,C.jsx)(h,{...e})}var S,C,w,T,E,D,O;function k(){return(k=e((()=>{S=n(),y(),C=a(),w={title:`Shared/UI/Listbox`,component:x,args:{options:[{id:`low`,label:`Низкий`},{id:`medium`,label:`Средний`},{id:`high`,label:`Высокий`},{id:`critical`,label:`Критический`,disabled:!0}],multiple:!1,value:`medium`,onChange:()=>{}},parameters:{atomicCanvas:!0,layout:`padded`},argTypes:{options:{description:`Массив доступных опций списка.`,control:!1},multiple:{description:`Режим множественного выбора.`,control:`boolean`},value:{description:`Текущее выбранное значение (или массив значений).`,control:!1},defaultValue:{description:`Начальное значение для uncontrolled-режима.`,control:!1},onChange:{description:`Вызывается при выборе опции.`,control:!1},focusOnMount:{description:`Автофокус списка при монтировании.`,control:`boolean`},renderItem:{description:`Кастомный рендер строки списка.`,control:!1},getKey:{description:`Функция генерации ключа для опции.`,control:!1},className:{description:`Дополнительный CSS-класс.`,control:`text`}}},T={render:e=>{let[t,n]=(0,S.useState)(`medium`);return(0,C.jsxs)(`div`,{style:{display:`grid`,gap:12,maxWidth:280},children:[(0,C.jsx)(x,{...e,multiple:!1,value:t,onChange:e=>{typeof e==`string`&&n(e)}}),(0,C.jsxs)(`div`,{children:[`Выбрано: `,t]})]})}},E={render:e=>{let[t,n]=(0,S.useState)([`low`,`high`]);return(0,C.jsxs)(`div`,{style:{display:`grid`,gap:12,maxWidth:320},children:[(0,C.jsx)(x,{...e,multiple:!0,value:t,onChange:e=>{Array.isArray(e)&&n(e)}}),(0,C.jsxs)(`div`,{children:[`Выбрано: `,t.join(`, `)||`пусто`]})]})},args:{multiple:!0}},D={render:e=>{let[t,n]=(0,S.useState)(`low`);return(0,C.jsx)(x,{...e,value:t,onChange:e=>{typeof e==`string`&&n(e)},renderItem:(e,t,n)=>(0,C.jsxs)(`div`,{style:{display:`flex`,justifyContent:`space-between`,width:`100%`,opacity:e.disabled?.5:1},children:[(0,C.jsx)(`span`,{children:e.label}),(0,C.jsx)(`span`,{children:t?`Выбрано`:n?`Фокус`:``})]})})}},T.parameters={...T.parameters,docs:{...T.parameters?.docs,source:{originalSource:`{
  render: args => {
    const [value, setValue] = useState<string>("medium");
    return <div style={{
      display: "grid",
      gap: 12,
      maxWidth: 280
    }}>
                <StringListbox {...args} multiple={false} value={value} onChange={next => {
        if (typeof next === "string") setValue(next);
      }} />
                <div>Выбрано: {value}</div>
            </div>;
  }
}`,...T.parameters?.docs?.source}}},E.parameters={...E.parameters,docs:{...E.parameters?.docs,source:{originalSource:`{
  render: args => {
    const [value, setValue] = useState<string[]>(["low", "high"]);
    return <div style={{
      display: "grid",
      gap: 12,
      maxWidth: 320
    }}>
                <StringListbox {...args} multiple value={value} onChange={next => {
        if (Array.isArray(next)) setValue(next);
      }} />
                <div>Выбрано: {value.join(", ") || "пусто"}</div>
            </div>;
  },
  args: {
    multiple: true
  }
}`,...E.parameters?.docs?.source}}},D.parameters={...D.parameters,docs:{...D.parameters?.docs,source:{originalSource:`{
  render: args => {
    const [value, setValue] = useState<string>("low");
    return <StringListbox {...args} value={value} onChange={next => {
      if (typeof next === "string") setValue(next);
    }} renderItem={(option, selected, active) => <div style={{
      display: "flex",
      justifyContent: "space-between",
      width: "100%",
      opacity: option.disabled ? 0.5 : 1
    }}>
                        <span>{option.label}</span>
                        <span>{selected ? "Выбрано" : active ? "Фокус" : ""}</span>
                    </div>} />;
  }
}`,...D.parameters?.docs?.source}}},O=[`SingleSelect`,`MultiSelect`,`CustomRender`]})))()}export{k as a,T as i,b as n,E as r,D as t};