var T,S;import{useRef as c,useState as b}from"react";import{disposables as m}from'../utils/disposables.js';import{useDisposables as g}from'./use-disposables.js';import{useFlags as y}from'./use-flags.js';import{useIsoMorphicEffect as L}from'./use-iso-morphic-effect.js';typeof process!="undefined"&&typeof globalThis!="undefined"&&typeof Element!="undefined"&&((T=process==null?void 0:process.env)==null?void 0:T["NODE_ENV"])==="test"&&typeof((S=Element==null?void 0:Element.prototype)==null?void 0:S.getAnimations)=="undefined"&&(Element.prototype.getAnimations=function(){return console.warn(["Headless UI has polyfilled `Element.prototype.getAnimations` for your tests.","Please install a proper polyfill e.g. `jsdom-testing-mocks`, to silence these warnings.","","Example usage:","```js","import { mockAnimationsApi } from 'jsdom-testing-mocks'","mockAnimationsApi()","```"].join(`
`)),[]});var A=(i=>(i[i.None=0]="None",i[i.Closed=1]="Closed",i[i.Enter=2]="Enter",i[i.Leave=4]="Leave",i))(A||{});function x(e){let r={};for(let t in e)e[t]===!0&&(r[`data-${t}`]="");return r}function N(e,r,t,n){let[i,a]=b(t),{hasFlag:s,addFlag:o,removeFlag:l}=y(e&&i?3:0),u=c(!1),f=c(!1),E=g();return L(()=>{var d;if(e){if(t&&a(!0),!r){t&&o(3);return}return(d=n==null?void 0:n.start)==null||d.call(n,t),C(r,{inFlight:u,prepare(){f.current?f.current=!1:f.current=u.current,u.current=!0,!f.current&&(t?(o(3),l(4)):(o(4),l(2)))},run(){f.current?t?(l(3),o(4)):(l(4),o(3)):t?l(1):o(1)},done(){var p;f.current&&D(r)||(u.current=!1,l(7),t||a(!1),(p=n==null?void 0:n.end)==null||p.call(n,t))}})}},[e,t,r,E]),e?[i,{closed:s(1),enter:s(2),leave:s(4),transition:s(2)||s(4)}]:[t,{closed:void 0,enter:void 0,leave:void 0,transition:void 0}]}function C(e,{prepare:r,run:t,done:n,inFlight:i}){let a=m();return j(e,{prepare:r,inFlight:i}),a.nextFrame(()=>{t(),a.requestAnimationFrame(()=>{a.add(M(e,n))})}),a.dispose}function M(e,r){var a,s;let t=m();if(!e)return t.dispose;let n=!1;t.add(()=>{n=!0});let i=(s=(a=e.getAnimations)==null?void 0:a.call(e).filter(o=>o instanceof CSSTransition))!=null?s:[];return i.length===0?(r(),t.dispose):(Promise.allSettled(i.map(o=>o.finished)).then(()=>{n||r()}),t.dispose)}function j(e,{inFlight:r,prepare:t}){if(r!=null&&r.current){t();return}let n=e.style.transition;e.style.transition="none",t(),e.offsetHeight,e.style.transition=n}function D(e){var t,n;return((n=(t=e.getAnimations)==null?void 0:t.call(e))!=null?n:[]).some(i=>i instanceof CSSTransition&&i.playState!=="finished")}export{x as transitionDataAttributes,N as useTransition};
