import{useRef as o,useState as e,useCallback as r,useEffect as n}from"react";import{useFocusOnShow as i}from"../../../hooks/dist/esm/use-focus-on-show.js";import{cssVars as t}from"../../../popper/dist/esm/utils.js";import{px as s}from"../../../utils/dist/esm/breakpoint.js";import{useDisclosure as u}from"../../../hooks/dist/esm/use-disclosure.js";import{useIds as l}from"../../../hooks/dist/esm/use-id.js";import{usePopper as c}from"../../../popper/dist/esm/use-popper.js";import{useFocusOnPointerDown as a}from"../../../hooks/dist/esm/use-focus-on-pointerdown.js";import{useFocusOnHide as d}from"../../../hooks/dist/esm/use-focus-on-hide.js";import{determineLazyBehavior as p}from"../../../utils/dist/esm/lazy.js";import{mergeRefs as v}from"../../../react-utils/dist/esm/refs.js";import{callAllHandlers as f}from"../../../utils/dist/esm/function.js";import{getRelatedTarget as m,contains as y}from"../../../utils/dist/esm/dom.js";function h(){return(h=Object.assign||function(o){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(o[n]=r[n])}return o}).apply(this,arguments)}var g={click:"click",hover:"hover"};function w(w){void 0===w&&(w={});var{closeOnBlur:O=!0,closeOnEsc:k=!0,initialFocusRef:P,id:b,returnFocusOnClose:j=!0,autoFocus:B=!0,arrowSize:z,arrowShadowColor:F,trigger:M=g.click,openDelay:C=200,closeDelay:D=200,isLazy:E,lazyBehavior:S="unmount",computePositionOnMount:T}=w,L=function(o,e){if(null==o)return{};var r,n,i={},t=Object.keys(o);for(n=0;n<t.length;n++)r=t[n],e.indexOf(r)>=0||(i[r]=o[r]);return i}(w,["closeOnBlur","closeOnEsc","initialFocusRef","id","returnFocusOnClose","autoFocus","arrowSize","arrowShadowColor","trigger","openDelay","closeDelay","isLazy","lazyBehavior","computePositionOnMount"]),{isOpen:R,onClose:x,onOpen:A,onToggle:K}=u(w),I=o(null),U=o(null),H=o(!1),q=o(!1);R&&(q.current=!0);var[G,J]=e(!1),[N,Q]=e(!1),[V,W,X,Y]=l(b,"popover-trigger","popover-content","popover-header","popover-body"),{referenceRef:Z,getArrowProps:$,getPopperProps:_,getArrowInnerProps:oo,forceUpdate:eo}=c(h({},L,{enabled:R||T}));a({enabled:R,ref:I}),d(U,{focusRef:I,visible:R,shouldFocus:j&&M===g.click}),i(U,{focusRef:P,visible:R,shouldFocus:B&&M===g.click});var ro=p({hasBeenSelected:q.current,isLazy:E,lazyBehavior:S,isSelected:R}),no=r((function(o,e){void 0===o&&(o={}),void 0===e&&(e=null);var r=h({},o,{style:h({},o.style,{transformOrigin:t.transformOrigin.varRef,[t.arrowSize.var]:z?s(z):void 0,[t.arrowShadowColor.var]:F}),ref:v(U,e),children:ro?o.children:null,id:W,tabIndex:-1,role:"dialog",onKeyDown:f(o.onKeyDown,(o=>{k&&"Escape"===o.key&&x()})),onBlur:f(o.onBlur,(o=>{var e=m(o),r=y(U.current,e),n=y(I.current,e);R&&O&&(!r&&!n)&&x()})),"aria-labelledby":G?X:void 0,"aria-describedby":N?Y:void 0});return M===g.hover&&(r.role="tooltip",r.onMouseEnter=f(o.onMouseEnter,(()=>{H.current=!0})),r.onMouseLeave=f(o.onMouseLeave,(()=>{H.current=!1,setTimeout(x,D)}))),r}),[ro,W,G,X,N,Y,M,k,x,R,O,D,F,z]),io=r((function(o,e){return void 0===o&&(o={}),void 0===e&&(e=null),_(h({},o,{style:h({visibility:R?"visible":"hidden"},o.style)}),e)}),[R,_]),to=o(),so=o(),uo=r((function(o,e){void 0===o&&(o={}),void 0===e&&(e=null);var r=h({},o,{ref:v(I,e,Z),id:V,"aria-haspopup":"dialog","aria-expanded":R,"aria-controls":W});return M===g.click&&(r.onClick=f(o.onClick,K)),M===g.hover&&(r.onFocus=f(o.onFocus,A),r.onBlur=f(o.onBlur,x),r.onKeyDown=f(o.onKeyDown,(o=>{"Escape"===o.key&&x()})),r.onMouseEnter=f(o.onMouseEnter,(()=>{H.current=!0,to.current=window.setTimeout(A,C)})),r.onMouseLeave=f(o.onMouseLeave,(()=>{H.current=!1,to.current&&(clearTimeout(to.current),to.current=void 0),so.current=window.setTimeout((()=>{!1===H.current&&x()}),D)}))),r}),[V,R,W,M,Z,K,A,x,C,D]);n((()=>()=>{to.current&&clearTimeout(to.current),so.current&&clearTimeout(so.current)}),[]);var lo=r((function(o,e){return void 0===o&&(o={}),void 0===e&&(e=null),h({},o,{id:X,ref:v(e,(o=>{J(!!o)}))})}),[X]),co=r((function(o,e){return void 0===o&&(o={}),void 0===e&&(e=null),h({},o,{id:Y,ref:v(e,(o=>{Q(!!o)}))})}),[Y]);return{forceUpdate:eo,isOpen:R,onClose:x,getArrowProps:$,getArrowInnerProps:oo,getPopoverPositionerProps:io,getPopoverProps:no,getTriggerProps:uo,getHeaderProps:lo,getBodyProps:co}}export{w as usePopover};
//# sourceMappingURL=use-popover.js.map
