import { balBrowser } from "../browser";
import { addEventListener, removeEventListener } from "../helpers";
import { ListenerAbstract } from "../types/listener";
export class BalElementStateListener extends ListenerAbstract {
    constructor() {
        super(...arguments);
        this.state = BalElementStateListener.DefaultState;
        this.onMouseEnter = () => {
            this.updateState({ hovered: true });
        };
        this.onMouseLeave = () => {
            this.updateState({ hovered: false });
        };
        this.onPointerDown = () => {
            this.updateState({ pressed: true });
        };
        this.onPointerUp = () => {
            this.updateState({ pressed: false });
        };
    }
    connect(el) {
        super.connect(el);
        addEventListener(this.el, 'mouseenter', this.onMouseEnter, BalElementStateListener.EventListenerOptions);
        addEventListener(this.el, 'mouseleave', this.onMouseLeave, BalElementStateListener.EventListenerOptions);
        addEventListener(this.el, 'pointerdown', this.onPointerDown, BalElementStateListener.EventListenerOptions);
        if (balBrowser.hasDocument) {
            addEventListener(document, 'pointerup', this.onPointerUp, BalElementStateListener.EventListenerOptions);
        }
    }
    disconnect() {
        super.disconnect();
        if (this.el) {
            removeEventListener(this.el, 'mouseenter', this.onMouseEnter);
            removeEventListener(this.el, 'mouseleave', this.onMouseLeave);
            removeEventListener(this.el, 'pointerdown', this.onPointerDown);
            if (balBrowser.hasDocument) {
                removeEventListener(document, 'pointerup', this.onPointerUp);
            }
        }
    }
    updateState(newState) {
        this.state = Object.assign(Object.assign({}, this.state), newState);
        this.notify(this.state);
    }
}
BalElementStateListener.EventListenerOptions = {
    passive: true,
};
BalElementStateListener.DefaultState = {
    hovered: false,
    pressed: false,
};
