import { b as balDevice } from './device.js';
import { L as ListenerAbstract, a as SingleSubject } from './listener.js';
import { n as rOnLoad } from './helpers.js';

class BalSwipeListener extends ListenerAbstract {
    async connect(el) {
        super.connect(el);
        await this.loadLib();
        if (this.PointerListenerLib) {
            this.pointerListener = new this.PointerListenerLib(el, { handleTouchEvents: false });
            // contactjs calls setPointerCapture on the event target in its onPointerDown handler.
            // When a pointerdown originates inside a shadow DOM component, the browser retargets
            // the event so the target appears as the shadow host element (e.g. bal-date-calendar).
            // This causes pointer capture to be set on the shadow host, which redirects pointerup
            // away from the actual element inside the shadow DOM, preventing the click event from
            // being dispatched on the intended target (e.g. bal-date-calendar-cell).
            // Fix: release pointer capture from shadow host elements immediately after contactjs sets it.
            this.shadowFixPointerDownListener = (ev) => {
                const target = ev.target;
                if (target && target !== el && target.shadowRoot) {
                    target.releasePointerCapture(ev.pointerId);
                }
            };
            el.addEventListener('pointerdown', this.shadowFixPointerDownListener);
            this.pointerListener.on('swipeleft', () => this.notify({ left: true, right: false }));
            this.pointerListener.on('swiperight', () => this.notify({ left: false, right: true }));
        }
    }
    disconnect() {
        var _a;
        if (this.shadowFixPointerDownListener && this.el) {
            this.el.removeEventListener('pointerdown', this.shadowFixPointerDownListener);
            this.shadowFixPointerDownListener = undefined;
        }
        super.disconnect();
        (_a = this.pointerListener) === null || _a === void 0 ? void 0 : _a.destroy();
    }
    async loadLib() {
        return new Promise((resolve, reject) => {
            rOnLoad(async () => {
                import(/* @vite-ignore */ './contact.js')
                    .then(module => {
                    this.PointerListenerLib = module.PointerListener;
                    resolve();
                })
                    .catch(reject);
            });
        });
    }
}

class BalSwipeSubject extends SingleSubject {
    constructor(options = { mobileOnly: false }) {
        super((observer, data) => {
            if (data) {
                observer.swipeListener(data);
            }
        });
        this.options = options;
        this.listener = new BalSwipeListener();
    }
    attach(observer) {
        if ((this.options.mobileOnly === true && balDevice.isMobile) || this.options.mobileOnly !== true) {
            super.attach(observer);
            this.listener.connect(observer.el);
            this.listener.add(info => super.notify(info));
        }
    }
    detach() {
        super.detach();
        this.listener.disconnect();
    }
}

export { BalSwipeSubject as B };
