"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Animator = void 0;
const solid_js_1 = require("solid-js");
const tools_1 = require("@arwes-amir/tools");
const animator_1 = require("@arwes-amir/animator");
const AnimatorContext_js_1 = require("../internal/AnimatorContext.js");
const index_js_1 = require("../useGetAnimator/index.js");
const Animator = (props) => {
    const getParentAnimatorInterface = (0, index_js_1.useGetAnimator)();
    const parentAnimatorInterface = getParentAnimatorInterface?.();
    const settingsComponentRef = { current: {} };
    const settingsDynamicRef = { current: null };
    const getParentInterface = (0, solid_js_1.createMemo)(() => props.root ? undefined : parentAnimatorInterface);
    const getIsDisabled = (0, solid_js_1.createMemo)(() => !!props.disabled);
    const getIsDismissed = (0, solid_js_1.createMemo)(() => !!props.dismissed);
    (0, solid_js_1.createMemo)(() => {
        settingsComponentRef.current = {
            active: props.active,
            duration: props.duration,
            manager: props.manager,
            merge: props.merge,
            combine: props.combine,
            initialState: props.initialState,
            condition: props.condition,
            onTransition: props.onTransition
        };
    });
    const getAnimatorInterface = (0, solid_js_1.createMemo)((prevAnimatorInterface) => {
        const parentInterface = getParentInterface();
        const isDisabled = getIsDisabled();
        const isDismissed = getIsDismissed();
        if (isDisabled) {
            if (prevAnimatorInterface) {
                prevAnimatorInterface.system.unregister(prevAnimatorInterface.node);
            }
            return undefined;
        }
        if (isDismissed) {
            if (prevAnimatorInterface) {
                prevAnimatorInterface.system.unregister(prevAnimatorInterface.node);
            }
            return parentInterface;
        }
        if (prevAnimatorInterface) {
            prevAnimatorInterface.system.unregister(prevAnimatorInterface.node);
        }
        const getSettings = () => {
            return {
                ...(0, tools_1.filterProps)(settingsComponentRef.current),
                ...(settingsDynamicRef.current ? (0, tools_1.filterProps)(settingsDynamicRef.current) : null),
                duration: {
                    ...(settingsComponentRef.current.duration
                        ? (0, tools_1.filterProps)(settingsComponentRef.current.duration)
                        : null),
                    ...(settingsDynamicRef.current?.duration
                        ? (0, tools_1.filterProps)(settingsDynamicRef.current?.duration)
                        : null)
                }
            };
        };
        const system = parentInterface ? parentInterface.system : (0, animator_1.createAnimatorSystem)();
        const parentNode = parentInterface ? parentInterface.node : undefined;
        const node = system.register(parentNode, { getSettings });
        return Object.freeze({ system, node });
    });
    (0, solid_js_1.createEffect)(() => {
        const animatorInterface = getAnimatorInterface();
        if (animatorInterface) {
            queueMicrotask(() => {
                animatorInterface.node.send('setup');
            });
        }
    });
    (0, solid_js_1.createEffect)(() => {
        void props.root;
        void props.disabled;
        void props.dismissed;
        void props.active;
        void props.combine;
        void props.merge;
        void props.manager;
        const animatorInterface = getAnimatorInterface();
        if (animatorInterface) {
            queueMicrotask(() => {
                animatorInterface.node.send('update');
            });
        }
    });
    (0, solid_js_1.onCleanup)(() => {
        const animatorInterface = getAnimatorInterface();
        if (animatorInterface) {
            animatorInterface.system.unregister(animatorInterface.node);
        }
    });
    return (<AnimatorContext_js_1.AnimatorContext.Provider value={getAnimatorInterface}>
      {props.children}
    </AnimatorContext_js_1.AnimatorContext.Provider>);
};
exports.Animator = Animator;
