import{r as n,j as l}from"./iframe-p4aYZyoq.js";import{A as I,t as A}from"./tree-data-I_VnBKcO.js";import{c as g}from"./index-Dx18ixSn.js";import{a as $}from"./dropdown-button-D0tG_Xmy.js";import{B as R}from"./button-BsNF7f-b.js";import{c as W,p as H,b as _}from"./utils-pBtDpbI-.js";import"./preload-helper-BDBacUwf.js";import"./substring-highlight-D19--ls-.js";import"./search-input-P8XqbDB1.js";import"./search-CiD9rZJ0.js";import"./spinner-CEdkaGgP.js";import"./times-DeY5Bu0x.js";/* empty css                 */const f=({data:a,onSelect:i,autocomplete:c=!1,autocompletePlaceholder:O="",autocompleteFilter:C=!0,defaultActiveNodes:F=[],label:D,...E})=>{const[h,P]=n.useState(F),[b,p]=n.useState([]);n.useEffect(()=>{p(Array.from(W(a)))},[a]);const[k,w]=n.useState(!1),B=n.useMemo(()=>H(_(a)),[a]),v=n.useCallback(t=>p(r=>r.includes(t.id)?r.filter(o=>o!==t.id):[...r,t.id]),[]),d=n.useCallback((t,r)=>{if(typeof t!="string")if(t.items)v(t);else{const o=_(a,t.id)[0],e=o[o.length-1];P(o.map(s=>s.id)),p(o.map(s=>s.id)),i(e),r(!1)}},[a,i,v]),y=n.useCallback((t,r,o=!1)=>l.jsx("ul",{role:o?"tree":"group",children:t.map(e=>{var S;let s;return e.items&&(s=b.includes(e.id)?"true":"false"),l.jsxs("li",{role:"treeitem","aria-expanded":s,"aria-selected":s,className:g({branch:e.items}),children:[l.jsx(R,{onClick:()=>d(e,r),className:g({active:h.includes(e.id)}),variant:"secondary","aria-label":(S=e.items)!=null&&S.length?`${e.label} (${e.items.length} nested option${e.items.length===1?"":"s"})`:void 0,children:e.label}),e.items&&y(e.items,r)]},e.id)})}),[h,d,b]);return l.jsx($,{label:D||"Select",...E,children:t=>l.jsxs(l.Fragment,{children:[c&&l.jsx(I,{data:B,onDropdownChange:w,onSelect:r=>d(r,t),placeholder:O,filter:C,clearOnSelect:!0,autoFocus:!0}),!k&&l.jsx("div",{className:"dropdown-menu__panel",children:y(a,t,!0)})]})})};try{treeselect.displayName="treeselect",treeselect.__docgenInfo={description:"",displayName:"treeselect",props:{openOnHover:{defaultValue:null,description:"Open on pointer over (useful for dropdowns in header)",name:"openOnHover",required:!1,type:{name:"boolean"}},type:{defaultValue:null,description:"Type to pass to the underlying <button></button>",name:"type",required:!1,type:{name:"enum",value:[{value:'"button"'},{value:'"submit"'},{value:'"reset"'}]}},className:{defaultValue:null,description:"Classnames to be added to the button",name:"className",required:!1,type:{name:"string"}},disabled:{defaultValue:null,description:"Flag to disable the button",name:"disabled",required:!1,type:{name:"boolean"}},variant:{defaultValue:null,description:"Variant of the button",name:"variant",required:!1,type:{name:"enum",value:[{value:'"primary"'},{value:'"secondary"'},{value:'"tertiary"'}]}},data:{defaultValue:null,description:"The tree structure",name:"data",required:!0,type:{name:"Item[]"}},onSelect:{defaultValue:null,description:"What happens when something is selected",name:"onSelect",required:!0,type:{name:'(item: Omit<Item, "items">) => void'}},autocomplete:{defaultValue:{value:"false"},description:"Contains autocomplete functionality to search through tree",name:"autocomplete",required:!1,type:{name:"boolean"}},autocompletePlaceholder:{defaultValue:{value:""},description:"Placeholder for the autocomplete input box",name:"autocompletePlaceholder",required:!1,type:{name:"string"}},autocompleteFilter:{defaultValue:{value:"true"},description:"",name:"autocompleteFilter",required:!1,type:{name:"boolean"}},label:{defaultValue:null,description:"The displayed label on the button",name:"label",required:!1,type:{name:"ReactNode"}},defaultActiveNodes:{defaultValue:{value:"[]"},description:"Array of default active nodes for initialisation",name:"defaultActiveNodes",required:!1,type:{name:'Item["id"][]'}}}}}catch{}const{fn:K}=__STORYBOOK_MODULE_TEST__,re={title:"Forms/Tree Select",component:f,argTypes:{variant:{control:"select",options:["primary","secondary","tertiary"]}},args:{label:"Select",variant:"primary",onSelect:K()}},u={render:({label:a,variant:i,onSelect:c})=>l.jsx(f,{label:a,data:A,onSelect:c,variant:i})},m={render:({label:a,variant:i,onSelect:c})=>l.jsx(f,{label:a,data:A,onSelect:c,autocomplete:!0,autocompletePlaceholder:"Search for item",autocompleteFilter:!0,variant:i})};var x,T,N;u.parameters={...u.parameters,docs:{...(x=u.parameters)==null?void 0:x.docs,source:{originalSource:`{
  render: ({
    label,
    variant,
    onSelect
  }) => <TS label={label} data={treeData} onSelect={onSelect} variant={variant} />
}`,...(N=(T=u.parameters)==null?void 0:T.docs)==null?void 0:N.source}}};var V,q,j;m.parameters={...m.parameters,docs:{...(V=m.parameters)==null?void 0:V.docs,source:{originalSource:`{
  render: ({
    label,
    variant,
    onSelect
  }) => <TS label={label} data={treeData} onSelect={onSelect} autocomplete autocompletePlaceholder="Search for item" autocompleteFilter variant={variant} />
}`,...(j=(q=m.parameters)==null?void 0:q.docs)==null?void 0:j.source}}};const le=["TreeSelect","TreeSelectWithAutocomplete"];export{u as TreeSelect,m as TreeSelectWithAutocomplete,le as __namedExportsOrder,re as default};
