// #region Imports
// #endregion
/**
 * @public
 */
export class AnimationPlayer {
    // #region Fields
    _target;
    // #endregion
    // #region Ctor
    constructor(target) {
        this._target = target;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `target` property.
     *
     * @public
     * @readonly
     */
    get target() {
        return this._target;
    }
    // #endregion
    async play(metadata) {
        return new Promise((resolve) => {
            if (metadata.options?.duration === Infinity) {
                throw new Error('Promise-based animations must be finite.');
            }
            const parsedKeyframes = this.parseKeyframes(metadata.steps);
            const options = {
                ...metadata.options,
                duration: window.matchMedia('(prefers-reduced-motion: reduce)').matches
                    ? 0
                    : metadata.options?.duration
            };
            const animation = this._target.animate(parsedKeyframes, options);
            animation.addEventListener('cancel', resolve, { once: true });
            animation.addEventListener('finish', resolve, { once: true });
        });
    }
    stopAll() {
        return Promise.all(this._target.getAnimations().map((animation) => new Promise((resolve) => {
            const handleAnimationEvent = () => requestAnimationFrame(resolve);
            animation.addEventListener('cancel', handleAnimationEvent, {
                once: true
            });
            animation.addEventListener('finish', handleAnimationEvent, {
                once: true
            });
            animation.cancel();
        })));
    }
    parseKeyframes(keyframes) {
        return keyframes.map((keyframe) => {
            if (!keyframe.height) {
                return keyframe;
            }
            return {
                ...keyframe,
                height: keyframe.height === 'auto'
                    ? `${this._target.scrollHeight}px`
                    : keyframe.height
            };
        });
    }
}
//# sourceMappingURL=AnimationPlayer.js.map