import { L as ListenerAbstract, a as SingleSubject } from './listener-Cwtj-tmC.js';
import { U as addEventListener, a as balBrowser, V as removeEventListener } from './index-DGWPFpP6.js';

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,
};

class BalElementStateSubject extends SingleSubject {
    constructor() {
        super((observer, data) => {
            if (data) {
                observer.elementStateListener(data);
            }
        });
        this.listener = new BalElementStateListener();
    }
    attach(observer) {
        super.attach(observer);
        this.listener.connect(observer.el);
        this.listener.add(info => super.notify(info));
    }
    detach() {
        super.detach();
        this.listener.disconnect();
    }
}

function ListenToElementStates() {
    return function (target, _propertyKey, _descriptor) {
        const { connectedCallback, disconnectedCallback } = target;
        target.connectedCallback = function () {
            if (!this._balElementStateSubject) {
                this._balElementStateSubject = new BalElementStateSubject();
                this._balElementStateSubject.attach(this);
            }
            return connectedCallback && connectedCallback.call(this);
        };
        target.disconnectedCallback = function () {
            if (this._balElementStateSubject) {
                this._balElementStateSubject.detach();
                this._balElementStateSubject = undefined;
            }
            return disconnectedCallback && disconnectedCallback.call(this);
        };
    };
}

export { BalElementStateListener as B, ListenToElementStates as L };
