import{useRef as t,useState as r,useMemo as e,useEffect as n,Children as i}from"react";import{useEditorMode as o,useInteraction as u}from"@gem-sdk/core";let l=l=>{let{setting:c,builderProps:a,children:d}=l,{activeKey:m,expandItem:g,expandedMode:p}=c??{},f=t([]),[s,S]=r([]),w=o(),{onListener:h,saveToElementInteractionData:v,ref:x,findElementIncludingSelf:y}=u(),A=e(()=>"multiple"===p,[p]);n(()=>{g||(f.current=[],S([]))},[g]),n(()=>{A||m||(f.current=["0"],S(["0"])),m||(f.current=[],S([]))},[A,m]),n(()=>{if(m&&g){let t=m?.toString()?.toLowerCase()?.split(",")?.map(t=>parseInt(t.trim())-1)?.map(t=>t.toString());S(t),f.current=t}},[m,g]),n(()=>{if(g||(S([]),f.current=[]),m?.toString()?.toLowerCase()==="all"&&g){let t=Array.from(i?.map(d,(t,r)=>r.toString()));S(t),f.current=t}},[m,d,g]);let C=t=>{s.includes(t)?(S([]),f.current=[]):(S([t]),f.current=[t])},L=t=>{let r=[];S(r=s.includes(t)?s.filter(r=>r!==t):[...s,t]),f.current=r};return n(()=>{let t=`[data-id="${a?.uid}"]`;h({event:"gp:change-expanded-item-original",selector:t},r=>{let e=y(x.current||document,t),{data:n,isRollback:i,key:o}=r||{};if(void 0===n&&i)return;if(i){S(n);return}let u=[...f.current];v(e,o,JSON.stringify({data:u}));let l=n.toString();S(u=A?[...u.filter(t=>t!==l),l]:[l])})},[a?.uid,A,y,h,x,v]),n(()=>{f.current=s},[s]),{ref:x,mode:w,accordions:s,activeKey:m,setAccordions:S,handleDropdown:t=>{A?L(t.toString()):C(t.toString())}}};export{l as default};
