function isBrowserEnvironment() {
    return typeof window !== 'undefined' && typeof document !== 'undefined';
}
function isNodeEnvironment() {
    return (typeof process !== 'undefined' && process.versions != null && process.versions.node != null);
}

function createTimerEventSource(name, interval) {
    return {
        name,
        description: `Timer that fires every ${interval}ms`,
        supportedEvents: [name],
        subscribe(options, context) {
            const handler = () => {
                const payload = {
                    type: name,
                    data: { timestamp: Date.now() },
                    target: isBrowserEnvironment() && typeof document !== 'undefined' ? document.body : {},
                };
                options.handler(payload, context);
            };
            const timerId = setInterval(handler, interval);
            return {
                id: `${name}_${Date.now()}_${Math.random().toString(36).slice(2)}`,
                source: name,
                event: name,
                unsubscribe: () => clearInterval(timerId),
            };
        },
        supports(event) {
            return event === name;
        },
    };
}
function createAdaptiveEventSource(name, options) {
    return {
        name,
        description: options?.description ?? `Adaptive ${name} events`,
        supportedEvents: [name],
        subscribe(subscribeOptions, context) {
            let unsubscribe;
            if (isBrowserEnvironment()) {
                const handler = (e) => {
                    const customEvent = e;
                    const payload = {
                        type: name,
                        data: customEvent.detail,
                        target: e.target instanceof Element ? e.target : null,
                        nativeEvent: e,
                    };
                    subscribeOptions.handler(payload, context);
                };
                document.addEventListener(name, handler);
                unsubscribe = () => document.removeEventListener(name, handler);
            }
            else if (isNodeEnvironment()) {
                unsubscribe = () => {
                };
            }
            else {
                unsubscribe = () => { };
            }
            return {
                id: `${name}_${Date.now()}_${Math.random().toString(36).slice(2)}`,
                source: name,
                event: name,
                unsubscribe,
            };
        },
        supports(event) {
            return event === name;
        },
    };
}
function isBrowserLikePayload(payload) {
    return isBrowserEnvironment() && (payload.target == null || payload.target instanceof Element);
}
function isServerLikePayload(payload) {
    return (isNodeEnvironment() &&
        typeof payload.data === 'object' &&
        payload.data !== null &&
        'request' in payload.data);
}

export { createAdaptiveEventSource, createTimerEventSource, isBrowserLikePayload, isServerLikePayload };
//# sourceMappingURL=universal-types.mjs.map
