"use client";
import { useEffect } from "react";
import { useAnimationsFinished } from "./useAnimationsFinished.js";
import { useEventCallback } from "./useEventCallback.js";
import { useValueAsRef } from "./useValueAsRef.js";
/**
 * Waits for the element's current Web Animations / CSS transitions to finish after an
 * `open` state change, then invokes `onComplete`. Guards against race conditions by
 * comparing the `open` value captured at scheduling time with the latest `open` via ref;
 * if they differ (state flipped again mid‑animation) the callback is skipped.
 */
export function useOpenChangeAnimationComplete(parameters) {
    const { enabled = true, open, ref = null, onComplete: onCompleteParam, } = parameters;
    const openRef = useValueAsRef(open);
    const onComplete = useEventCallback(onCompleteParam);
    const runOnceAnimationsFinish = useAnimationsFinished(ref, open);
    useEffect(() => {
        if (!enabled) {
            return;
        }
        /*
         * Schedule completion once the *current* set of animations settle. If during
         * that wait `open` toggles again, skip to avoid firing for an outdated cycle.
         */
        runOnceAnimationsFinish(() => {
            if (open === openRef.current) {
                onComplete();
            }
        });
    }, [enabled, open, onComplete, runOnceAnimationsFinish, openRef]);
}
//# sourceMappingURL=useOpenChangeAnimationComplete.js.map