'use strict';

function isBrowserPayload(payload) {
    return (typeof window !== 'undefined' &&
        typeof document !== 'undefined' &&
        (payload.target == null || payload.target instanceof Element));
}
function isBrowserEventSource(source) {
    return typeof window !== 'undefined' && typeof document !== 'undefined';
}
function createDOMEventSource(eventName, options) {
    const target = options?.target ?? document;
    const capture = options?.capture ?? false;
    return {
        name: eventName,
        description: options?.description ?? `DOM ${eventName} events`,
        supportedEvents: [eventName],
        subscribe(subscribeOptions, context) {
            const handler = (e) => {
                const payload = {
                    type: eventName,
                    data: e,
                    target: e.target instanceof Element ? e.target : null,
                    nativeEvent: e,
                };
                subscribeOptions.handler(payload, context);
            };
            target.addEventListener(eventName, handler, { capture });
            return {
                id: `${eventName}_${Date.now()}_${Math.random().toString(36).slice(2)}`,
                source: eventName,
                event: eventName,
                unsubscribe: () => target.removeEventListener(eventName, handler, { capture }),
            };
        },
        supports(event) {
            return event === eventName;
        },
    };
}
function createCustomEventSource(eventName, options) {
    const target = options?.target ?? document;
    return {
        name: eventName,
        description: options?.description ?? `Custom ${eventName} events`,
        supportedEvents: [eventName],
        subscribe(subscribeOptions, context) {
            const handler = (e) => {
                const customEvent = e;
                const payload = {
                    type: eventName,
                    data: customEvent.detail,
                    target: e.target instanceof Element ? e.target : null,
                    nativeEvent: e,
                    meta: {
                        bubbles: customEvent.bubbles,
                        cancelable: customEvent.cancelable,
                        composed: customEvent.composed,
                    },
                };
                subscribeOptions.handler(payload, context);
            };
            target.addEventListener(eventName, handler);
            return {
                id: `${eventName}_${Date.now()}_${Math.random().toString(36).slice(2)}`,
                source: eventName,
                event: eventName,
                unsubscribe: () => target.removeEventListener(eventName, handler),
            };
        },
        supports(event) {
            return event === eventName;
        },
    };
}

exports.createCustomEventSource = createCustomEventSource;
exports.createDOMEventSource = createDOMEventSource;
exports.isBrowserEventSource = isBrowserEventSource;
exports.isBrowserPayload = isBrowserPayload;
//# sourceMappingURL=browser-types.js.map
