import { InjectionToken, Optional } from '@angular/core';
import { ConfigService } from '@rxap/config';
import { connect } from 'socket.io-client';

const RXAP_SOCKET_IO_URL = new InjectionToken('rxap-socket-io/url');
const RXAP_SOCKET_IO_NAMESPACE = new InjectionToken('rxap-socket-io/namespace');
const RXAP_SOCKET_IO_OPTIONS = new InjectionToken('rxap-socket-io/options');
const RXAP_SOCKET_IO = new InjectionToken('rxap-socket-io/instance');

function provideSocketIo(...providers) {
    return [
        ...providers,
        {
            provide: RXAP_SOCKET_IO,
            useFactory: (url, namespace, options) => {
                if (namespace) {
                    url = url.replace(/\/$/, '');
                    namespace = namespace.replace(/^\//, '');
                    url = [url, namespace].join('/');
                }
                return connect(url, options ?? {});
            },
            deps: [
                RXAP_SOCKET_IO_URL,
                [new Optional(), RXAP_SOCKET_IO_NAMESPACE],
                [new Optional(), RXAP_SOCKET_IO_OPTIONS]
            ]
        }
    ];
}
function withSocketIoUrl(url) {
    return {
        provide: RXAP_SOCKET_IO_URL,
        useValue: url
    };
}
function withSocketIoNamespace(namespace) {
    return {
        provide: RXAP_SOCKET_IO_NAMESPACE,
        useValue: namespace
    };
}
function withSocketIoOptions(options) {
    return {
        provide: RXAP_SOCKET_IO_OPTIONS,
        useValue: options
    };
}
// region dynamic from config
function withDynamicSocketIoUrl(key = 'socket.url') {
    return {
        provide: RXAP_SOCKET_IO_URL,
        useFactory: (config) => config.getOrThrow(key),
        deps: [ConfigService]
    };
}
function withDynamicSocketIoNamespace(key = 'socket.namespace') {
    return {
        provide: RXAP_SOCKET_IO_NAMESPACE,
        useFactory: (config) => config.getOrThrow(key),
        deps: [ConfigService]
    };
}
function withDynamicSocketIoOptions(key = 'socket.options') {
    return {
        provide: RXAP_SOCKET_IO_OPTIONS,
        useFactory: (config) => config.get(key, {}),
        deps: [ConfigService]
    };
}
// endregion

// region 
// endregion

/**
 * Generated bundle index. Do not edit.
 */

export { RXAP_SOCKET_IO, RXAP_SOCKET_IO_NAMESPACE, RXAP_SOCKET_IO_OPTIONS, RXAP_SOCKET_IO_URL, provideSocketIo, withDynamicSocketIoNamespace, withDynamicSocketIoOptions, withDynamicSocketIoUrl, withSocketIoNamespace, withSocketIoOptions, withSocketIoUrl };
//# sourceMappingURL=rxap-socket-io.mjs.map
