import{useState as e,useRef as o,useCallback as n}from"react";import{createDescendantContext as s}from"../../../descendant/dist/esm/use-descendant.js";import{useUnmountEffect as i}from"../../../hooks/dist/esm/use-unmount-effect.js";import{useControllableState as t}from"../../../hooks/dist/esm/use-controllable.js";import{useIds as l}from"../../../hooks/dist/esm/use-id.js";import{normalizeEventKey as r}from"../../../utils/dist/esm/dom.js";import{isArray as a,isUndefined as d}from"../../../utils/dist/esm/assertion.js";import{addItem as u,removeItem as c}from"../../../utils/dist/esm/array.js";import{createContext as p}from"../../../react-utils/dist/esm/context.js";import{mergeRefs as m}from"../../../react-utils/dist/esm/refs.js";import{callAllHandlers as f,warn as g}from"../../../utils/dist/esm/function.js";import{focus as b}from"../../../utils/dist/esm/focus.js";function v(){return(v=Object.assign||function(e){for(var o=1;o<arguments.length;o++){var n=arguments[o];for(var s in n)Object.prototype.hasOwnProperty.call(n,s)&&(e[s]=n[s])}return e}).apply(this,arguments)}function w(e,o){if(null==e)return{};var n,s,i={},t=Object.keys(e);for(s=0;s<t.length;s++)n=t[s],o.indexOf(n)>=0||(i[n]=e[n]);return i}var[h,x,y,j]=s();function D(o){var{onChange:n,defaultIndex:s,index:l,allowMultiple:r,allowToggle:p}=o,m=w(o,["onChange","defaultIndex","index","allowMultiple","allowToggle"]);!function(e){var o=e.index||e.defaultIndex,n=!d(o)&&!a(o)&&e.allowMultiple;g({condition:!!n,message:"If 'allowMultiple' is passed, then 'index' or 'defaultIndex' must be an array. You passed: "+typeof o+","})}(o),function(e){g({condition:!(!e.allowMultiple||!e.allowToggle),message:"If 'allowMultiple' is passed, 'allowToggle' will be ignored. Either remove 'allowToggle' or 'allowMultiple' depending on whether you want multiple accordions visible or not"})}(o);var f=y(),[b,v]=e(-1);i((()=>{v(-1)}));var[h,x]=t({value:l,defaultValue:()=>r?null!=s?s:[]:null!=s?s:-1,onChange:n});return{index:h,setIndex:x,htmlProps:m,getAccordionItemProps:e=>{var o=!1;null!==e&&(o=a(h)?h.includes(e):h===e);return{isOpen:o,onChange:o=>{if(null!==e)if(r&&a(h)){var n=o?u(h,e):c(h,e);x(n)}else o?x(e):p&&x(-1)}}},focusedIndex:b,setFocusedIndex:v,descendants:f}}var[I,C]=p({name:"AccordionContext",errorMessage:"useAccordionContext: `context` is undefined. Seems you forgot to wrap the accordion components in `<Accordion />`"});function O(e){var{isDisabled:s,isFocusable:i,id:t}=e,a=w(e,["isDisabled","isFocusable","id"]),{getAccordionItemProps:d,setFocusedIndex:u}=C(),c=o(null),[p,h]=l(t,"accordion-button","accordion-panel");!function(e){g({condition:!(!e.isFocusable||e.isDisabled),message:"Using only 'isFocusable', this prop is reserved for situations where you pass 'isDisabled' but you still want the element to receive focus (A11y). Either remove it or pass 'isDisabled' as well.\n    "})}(e);var{register:x,index:y,descendants:D}=j({disabled:s&&!i}),{isOpen:I,onChange:O}=d(-1===y?null:y);!function(e){g({condition:e.isOpen&&!!e.isDisabled,message:"Cannot open a disabled accordion item"})}({isOpen:I,isDisabled:s});var F=n((()=>{null==O||O(!I),u(y)}),[y,u,I,O]),A=n((e=>{var o={ArrowDown:()=>{var e=D.nextEnabled(y);e&&b(e.node)},ArrowUp:()=>{var e=D.prevEnabled(y);e&&b(e.node)},Home:()=>{var e=D.firstEnabled();e&&b(e.node)},End:()=>{var e=D.lastEnabled();e&&b(e.node)}}[r(e)];o&&(e.preventDefault(),o(e))}),[D,y]),M=n((()=>{u(y)}),[u,y]),P=n((function(e,o){return void 0===e&&(e={}),void 0===o&&(o=null),v({},e,{type:"button",ref:m(x,c,o),id:p,disabled:!!s,"aria-expanded":!!I,"aria-controls":h,onClick:f(e.onClick,F),onFocus:f(e.onFocus,M),onKeyDown:f(e.onKeyDown,A)})}),[p,s,I,F,M,A,h,x]),E=n((function(e,o){return void 0===e&&(e={}),void 0===o&&(o=null),v({},e,{ref:o,role:"region",id:h,"aria-labelledby":p,hidden:!I})}),[p,I,h]);return{isOpen:I,isDisabled:s,isFocusable:i,onOpen:()=>{null==O||O(!0)},onClose:()=>{null==O||O(!1)},getButtonProps:P,getPanelProps:E,htmlProps:a}}export{h as AccordionDescendantsProvider,I as AccordionProvider,D as useAccordion,C as useAccordionContext,j as useAccordionDescendant,y as useAccordionDescendants,x as useAccordionDescendantsContext,O as useAccordionItem};
//# sourceMappingURL=use-accordion.js.map
