import*as e from"react";import{createDescendantContext as t}from"../../../descendant/dist/esm/use-descendant.js";import{callAllHandlers as i}from"../../../utils/dist/esm/function.js";import{useControllableState as n}from"../../../hooks/dist/esm/use-controllable.js";import{useId as o}from"../../../hooks/dist/esm/use-id.js";import{createContext as r}from"../../../react-utils/dist/esm/context.js";import{normalizeEventKey as s}from"../../../utils/dist/esm/dom.js";import{useClickable as a}from"../../../clickable/dist/esm/use-clickable.js";import{mergeRefs as l}from"../../../react-utils/dist/esm/refs.js";import{getValidChildren as d}from"../../../react-utils/dist/esm/children.js";import{determineLazyBehavior as u}from"../../../utils/dist/esm/lazy.js";import{useSafeLayoutEffect as c}from"../../../hooks/dist/esm/use-safe-layout-effect.js";import{isUndefined as f}from"../../../utils/dist/esm/assertion.js";import{focus as m}from"../../../utils/dist/esm/focus.js";function h(){return(h=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var i=arguments[t];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n])}return e}).apply(this,arguments)}function b(e,t){if(null==e)return{};var i,n,o={},r=Object.keys(e);for(n=0;n<r.length;n++)i=r[n],t.indexOf(i)>=0||(o[i]=e[i]);return o}var[p,v,x,y]=t();function g(t){var{defaultIndex:i,onChange:r,index:s,isManual:a,isLazy:l,lazyBehavior:d="unmount",orientation:u="horizontal",direction:c="ltr"}=t,f=b(t,["defaultIndex","onChange","index","isManual","isLazy","lazyBehavior","orientation","direction"]),[m,h]=e.useState(null!=i?i:0),[p,v]=n({defaultValue:null!=i?i:0,value:s,onChange:r});e.useEffect((()=>{null!=s&&h(s)}),[s]);var y=x();return{id:o(t.id,"tabs"),selectedIndex:p,focusedIndex:m,setSelectedIndex:v,setFocusedIndex:h,isManual:a,isLazy:l,lazyBehavior:d,orientation:u,descendants:y,direction:c,htmlProps:f}}var[j,z]=r({name:"TabsContext",errorMessage:"useTabsContext: `context` is undefined. Seems you forgot to wrap all tabs components within <Tabs />"});function w(t){var{focusedIndex:n,orientation:o,direction:r}=z(),a=v(),l=e.useCallback((e=>{var t=()=>{var e=a.nextEnabled(n);e&&m(e.node)},i=()=>{var e=a.prevEnabled(n);e&&m(e.node)},l="horizontal"===o,d="vertical"===o,u=s(e),c={["ltr"===r?"ArrowLeft":"ArrowRight"]:()=>l&&i(),["ltr"===r?"ArrowRight":"ArrowLeft"]:()=>l&&t(),ArrowDown:()=>d&&t(),ArrowUp:()=>d&&i(),Home:()=>{var e=a.firstEnabled();e&&m(e.node)},End:()=>{var e=a.lastEnabled();e&&m(e.node)}}[u];c&&(e.preventDefault(),c(e))}),[a,n,o,r]);return h({},t,{role:"tablist","aria-orientation":o,onKeyDown:i(t.onKeyDown,l)})}function I(e){var{isDisabled:t,isFocusable:n}=e,o=b(e,["isDisabled","isFocusable"]),{setSelectedIndex:r,isManual:s,id:d,setFocusedIndex:u,selectedIndex:c}=z(),{index:f,register:m}=y({disabled:t&&!n}),p=f===c,v=a(h({},o,{ref:l(m,e.ref),isDisabled:t,isFocusable:n,onClick:i(e.onClick,(()=>{r(f)}))}));return h({},v,{id:A(d,f),role:"tab",tabIndex:p?0:-1,type:"button","aria-selected":p,"aria-controls":C(d,f),onFocus:t?void 0:i(e.onFocus,(()=>{u(f),!s&&!(t&&n)&&r(f)}))})}function S(t){var i=z(),{id:n,selectedIndex:o}=i;return h({},t,{children:d(t.children).map(((t,i)=>e.cloneElement(t,{isSelected:i===o,id:C(n,i),"aria-labelledby":A(n,i)})))})}function F(t){var{isSelected:i,id:n,children:o}=t,r=b(t,["isSelected","id","children"]),{isLazy:s,lazyBehavior:a}=z(),l=e.useRef(!1);return i&&(l.current=!0),h({tabIndex:0},r,{children:u({hasBeenSelected:l.current,isSelected:i,isLazy:s,lazyBehavior:a})?o:null,role:"tabpanel",hidden:!i,id:n})}function k(){var t=z(),i=v(),{selectedIndex:n,orientation:o}=t,r="horizontal"===o,s="vertical"===o,[a,l]=e.useState((()=>r?{left:0,width:0}:s?{top:0,height:0}:void 0)),[d,u]=e.useState(!1);return c((()=>{if(!f(n)){var e=i.item(n);if(!f(e)){r&&l({left:e.node.offsetLeft,width:e.node.offsetWidth}),s&&l({top:e.node.offsetTop,height:e.node.offsetHeight});var t=requestAnimationFrame((()=>{u(!0)}));return()=>{t&&cancelAnimationFrame(t)}}}}),[n,r,s,i]),h({position:"absolute",transitionProperty:"left, right, top, bottom",transitionDuration:d?"200ms":"0ms",transitionTimingFunction:"cubic-bezier(0, 0, 0.2, 1)"},a)}function A(e,t){return e+"--tab-"+t}function C(e,t){return e+"--tabpanel-"+t}export{p as TabsDescendantsProvider,j as TabsProvider,I as useTab,k as useTabIndicator,w as useTabList,F as useTabPanel,S as useTabPanels,g as useTabs,z as useTabsContext,y as useTabsDescendant,x as useTabsDescendants,v as useTabsDescendantsContext};
//# sourceMappingURL=use-tabs.js.map
