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 useAccordion=c=>{let{setting:l,builderProps:a,children:d}=c,{activeKey:m,expandItem:g,expandedMode:f}=l??{},p=t([]),[s,S]=r([]),A=o(),{onListener:v,saveToElementInteractionData:w,ref:x,findElementIncludingSelf:y}=u(),C=e(()=>"multiple"===f,[f]);n(()=>{g||(p.current=[],S([]))},[g]),n(()=>{C||m||(p.current=["0"],S(["0"])),m||(p.current=[],S([]))},[C,m]),n(()=>{if(m&&g){let t=m?.toString()?.toLowerCase()?.split(",")?.map(t=>parseInt(t.trim())-1)?.map(t=>t.toString());S(t),p.current=t}},[m,g]),n(()=>{if(g||(S([]),p.current=[]),m?.toString()?.toLowerCase()==="all"&&g){let t=Array.from(i?.map(d,(t,r)=>r.toString()));S(t),p.current=t}},[m,d,g]);let L=t=>{s.includes(t)?(S([]),p.current=[]):(S([t]),p.current=[t])},h=t=>{let r=[];S(r=s.includes(t)?s.filter(r=>r!==t):[...s,t]),p.current=r},k=t=>{C?h(t.toString()):L(t.toString())};return n(()=>{let t=`[data-id="${a?.uid}"]`;v({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=[...p.current];w(e,o,JSON.stringify({data:u}));let c=n.toString();S(u=C?[...u.filter(t=>t!==c),c]:[c])})},[a?.uid,C,y,v,x,w]),n(()=>{p.current=s},[s]),{ref:x,mode:A,accordions:s,activeKey:m,setAccordions:S,handleDropdown:k}};export{useAccordion as default};
