"use strict";const a=require("react/jsx-runtime"),i=require("../vendor.cjs"),t=require("react"),ue=require("react-dom"),f=require("../tools/tools.cjs"),z=require("./popover-arrow.cjs"),U=require("./popover-body.cjs"),G=require("./popover-header.cjs"),v=function(J){var I,K;const{arrowProps:m,as:Q="div",body:X,bodyProps:Y,className:C,container:r,dropOldClass:L,fade:q=!0,header:Z,headerProps:$,offset:ee,onChange:y,placement:o="end",style:B,trigger:b,triggerType:h,triggerWrapper:te,variables:H,visible:g=!1,...oe}=J,[R,w]=t.useState(!1),[n,E]=t.useState(!1),S=t.useRef(null),[j,A]=t.useState(typeof r!="string"?r:null),[T,k]=t.useState(!1),re=t.useRef(()=>{p(!0)}),ne=t.useRef(()=>{p(!0)}),se=t.useRef(()=>{p(!1)}),ce=t.useRef(()=>{k(!0)}),ae=t.useRef(()=>{k(!1)}),D=t.useRef(null),{floatingStyles:ie,middlewareData:O,refs:c}=i.useFloating({middleware:[i.offset(()=>ee??8,[n]),i.shift(e=>e,[n]),i.flip(e=>e,[n]),i.size(e=>e,[n]),i.arrow(e=>({...e,element:S}),[n])],onOpenChange:E,open:n,placement:f.findTruthyClassOrDefault([["bottom",o==="bottom"],["left",o==="start"||o==="left"],["right",o==="end"||o==="right"],["top",o==="top"]],"right"),whileElementsMounted:i.autoUpdate}),p=t.useCallback(e=>{y==null||y(e)},[y]),M=t.useMemo(()=>f.isArray(h)?h:[h],[h]),_=t.useMemo(()=>{const e=f.classx(!L&&"popover",q&&"fade",n&&"show",o&&{"bs-popover-bottom":o==="bottom","bs-popover-end":o==="end"||o==="right","bs-popover-start":o==="start"||o==="left","bs-popover-top":o==="top"},C),l=f.stylex((u,P)=>({tKey:f.convertBsKeyToVar(P)}),H,B);return{className:e,style:l}},[C,L,q,o,n,B,H]),N=t.useMemo(()=>e=>({...e}),[]);return t.useEffect(()=>{if(g&&!R){w(!0);return}const e=c.floating.current;if(!e)return;const l=()=>{w(!!g)};e.addEventListener("transitionend",l);let u;return g?u=requestAnimationFrame(()=>E(!0)):E(!1),()=>{e.removeEventListener("transitionend",l),u&&cancelAnimationFrame(u)}},[c.floating,R,g]),t.useEffect(()=>{if(typeof r=="string"){const e=r.startsWith("#")?document.getElementById(r.slice(1)):document.querySelector(r);A(e)}else A(r)},[r]),t.useEffect(()=>{const e=re.current,l=ne.current,u=se.current,P=ce.current,le=ae.current,V=D.current,s=[];if(M.includes("focus")&&s.push({fn:e,type:"focus"}),M.includes("hover")?(s.push({fn:l,type:"mouseenter"}),s.push({fn:u,type:"mouseleave"})):(s.push({fn:P,type:"mouseenter"}),s.push({fn:le,type:"mouseleave"})),s.length){let d=c.reference.current;V&&(d=V);const W=()=>{T||(E(!1),p(!1))};return document.body.addEventListener("click",W),d&&s.forEach(({fn:F,type:x})=>d.addEventListener(x,F)),()=>{document.body.removeEventListener("click",W),d&&s.forEach(({fn:F,type:x})=>d.removeEventListener(x,F))}}},[T,p,c.reference,M]),a.jsxs(a.Fragment,{children:[b&&(te?a.jsx("span",{className:"d-inline-block",ref:D,tabIndex:0,children:b(c.setReference,N)}):b(c.setReference,N)),ue.createPortal(R&&a.jsxs(Q,{...oe,..._,ref:c.setFloating,style:{..._.style,...ie},children:[a.jsx(z.PopoverArrow,{...f.mergeProps(m,{style:{...m==null?void 0:m.style,left:(I=O.arrow)==null?void 0:I.x,top:(K=O.arrow)==null?void 0:K.y}}),onRef:e=>S.current=e}),a.jsx(G.PopoverHeader,{...$,children:Z}),a.jsx(U.PopoverBody,{...Y,children:X})]}),j||document.body)]})};v.Arrow=z.PopoverArrow;v.Header=G.PopoverHeader;v.Body=U.PopoverBody;v.displayName="BRL.Popover";exports.Popover=v;
//# sourceMappingURL=popover.cjs.map
