/* eslint-disable @typescript-eslint/consistent-return */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
// #region Imports
import { AsyncDirectiveBase, PartType, compose } from '../../Controls/Directives/Abstracts/AsyncDirectiveBase';
import { nothing } from '../../Dom/Html';
import { RendererServiceLocator } from '../../Dom/Renderer';
import { AnimationPlayer2 } from '../AnimationPlayer2';
import { isAnimationRef } from './AnimationRefDirective';
// #endregion
/**
 * The `AnimateDirective` class.
 *
 * @public
 */
export class AnimateDirective extends AsyncDirectiveBase {
    // #region Fields
    _animationPlayer;
    _wasTriggered;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `AnimateDirective` class.
     *
     * @public
     */
    constructor(args) {
        super(args);
        if (args.type !== PartType.ELEMENT) {
            throw new Error('The `animate` directive must be used in attribute position.');
        }
        this._wasTriggered = false;
        this._animationPlayer = new AnimationPlayer2();
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    render(optionsOrRef) {
        if (!this._wasTriggered) {
            this._wasTriggered = true;
            if (isAnimationRef(optionsOrRef)) {
                return;
            }
            Object.entries({
                ...optionsOrRef.from,
                ...optionsOrRef.initial,
                ...{}
            }).forEach(([k, v]) => {
                if (k in this.internals.element.style) {
                    RendererServiceLocator.current.setProperty(this.internals.element, k, v?.toString() ?? '');
                }
            });
            if (optionsOrRef.to) {
                if (optionsOrRef.trigger) {
                    optionsOrRef.trigger(() => {
                        this.play(optionsOrRef);
                    });
                }
                else {
                    this.play(optionsOrRef);
                }
            }
        }
        return nothing;
    }
    play(options) {
        void this._animationPlayer.play(this.internals.element, options.to, {
            ...{
                duration: 333,
                easing: 'ease'
            },
            ...options.options
        }).then(() => {
            options.onFinished?.();
        });
    }
}
export const animate = compose(AnimateDirective);
//# sourceMappingURL=AnimateDirective.js.map