import { addEventListener, removeEventListener, debounce, isChildOfEventTarget } from "../helpers";
import { ListenerAbstract } from "../types/listener";
export class BalAnimationListener extends ListenerAbstract {
    constructor() {
        super(...arguments);
        this.debouncedNotify = debounce(target => this.notify({ target }), 42);
        this.callbackWillAnimate = (ev) => {
            isChildOfEventTarget(ev, this.el, target => this.debouncedNotify(target));
        };
        this.callbackDidAnimate = (ev) => {
            isChildOfEventTarget(ev, this.el, target => this.debouncedNotify(target));
        };
    }
    connect(el) {
        super.connect(el);
        const win = window;
        if (win) {
            addEventListener(win, 'balWillAnimate', this.callbackWillAnimate, { passive: true });
            addEventListener(win, 'balDidAnimate', this.callbackDidAnimate, { passive: true });
        }
    }
    disconnect() {
        super.disconnect();
        const win = window;
        if (win) {
            removeEventListener(win, 'balWillAnimate', this.callbackWillAnimate);
            removeEventListener(win, 'balDidAnimate', this.callbackDidAnimate);
        }
    }
}
