"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var React=require("react"),core=require("@gem-sdk/core");const useAccordion=e=>{let{setting:t,builderProps:r,children:c}=e,{activeKey:u,expandItem:n,expandedMode:i}=t??{},a=React.useRef([]),[o,s]=React.useState([]),f=core.useEditorMode(),{onListener:l,saveToElementInteractionData:d,ref:g,findElementIncludingSelf:R}=core.useInteraction(),p=React.useMemo(()=>"multiple"===i,[i]);React.useEffect(()=>{n||(a.current=[],s([]))},[n]),React.useEffect(()=>{p||u||(a.current=["0"],s(["0"])),u||(a.current=[],s([]))},[p,u]),React.useEffect(()=>{if(u&&n){let e=u?.toString()?.toLowerCase()?.split(",")?.map(e=>parseInt(e.trim())-1)?.map(e=>e.toString());s(e),a.current=e}},[u,n]),React.useEffect(()=>{if(n||(s([]),a.current=[]),u?.toString()?.toLowerCase()==="all"&&n){let e=Array.from(React.Children?.map(c,(e,t)=>t.toString()));s(e),a.current=e}},[u,c,n]);let m=e=>{o.includes(e)?(s([]),a.current=[]):(s([e]),a.current=[e])},S=e=>{let t=[];s(t=o.includes(e)?o.filter(t=>t!==e):[...o,e]),a.current=t},E=e=>{p?S(e.toString()):m(e.toString())};return React.useEffect(()=>{let e=`[data-id="${r?.uid}"]`;l({event:"gp:change-expanded-item-original",selector:e},t=>{let r=R(g.current||document,e),{data:c,isRollback:u,key:n}=t||{};if(void 0===c&&u)return;if(u){s(c);return}let i=[...a.current];d(r,n,JSON.stringify({data:i}));let o=c.toString();s(i=p?[...i.filter(e=>e!==o),o]:[o])})},[r?.uid,p,R,l,g,d]),React.useEffect(()=>{a.current=o},[o]),{ref:g,mode:f,accordions:o,activeKey:u,setAccordions:s,handleDropdown:E}};exports.default=useAccordion;
