"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.connectToWeaviateCloud = connectToWeaviateCloud;
exports.connectToLocal = connectToLocal;
exports.connectToCustom = connectToCustom;
const errors_js_1 = require("../errors.js");
const auth_js_1 = require("./auth.js");
function connectToWeaviateCloud(clusterURL, clientMaker, options) {
    // check if the URL is set
    if (!clusterURL)
        throw new Error('Missing `clusterURL` parameter');
    if (!clusterURL.startsWith('http')) {
        clusterURL = `https://${clusterURL}`;
    }
    const url = new URL(clusterURL);
    let grpcHost;
    if (url.hostname.endsWith('.weaviate.network')) {
        const [ident, ...rest] = url.hostname.split('.');
        grpcHost = `${ident}.grpc.${rest.join('.')}`;
    }
    else {
        grpcHost = `grpc-${url.hostname}`;
    }
    const { authCredentials: auth, headers, ...rest } = options || {};
    if ([auth_js_1.AuthAccessTokenCredentials, auth_js_1.AuthClientCredentials, auth_js_1.AuthUserPasswordCredentials].some((c) => auth instanceof c)) {
        console.warn('Connecting to Weaviate Cloud (WCD) using OIDC is deprecated and will be removed in August 2025. Please use API keys instead.');
    }
    return clientMaker({
        connectionParams: {
            http: {
                secure: true,
                host: url.hostname,
                port: 443,
            },
            grpc: {
                secure: true,
                host: grpcHost,
                port: 443,
            },
        },
        auth,
        headers: options?.headers,
        ...rest,
    }).catch((e) => {
        throw new errors_js_1.WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
    });
}
function connectToLocal(clientMaker, options) {
    const { host, port, grpcPort, authCredentials: auth, ...rest } = options || {};
    return clientMaker({
        connectionParams: {
            http: {
                secure: false,
                host: host || 'localhost',
                port: port || 8080,
            },
            grpc: {
                secure: false,
                host: host || 'localhost',
                port: grpcPort || 50051,
            },
        },
        auth,
        ...rest,
    }).catch((e) => {
        throw new errors_js_1.WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
    });
}
function connectToCustom(clientMaker, options) {
    const { httpHost, httpPath, httpPort, httpSecure, grpcHost, grpcPort, grpcSecure, authCredentials: auth, ...rest } = options || {};
    return clientMaker({
        connectionParams: {
            http: {
                secure: httpSecure || false,
                host: httpHost || 'localhost',
                path: httpPath || '',
                port: httpPort || 8080,
            },
            grpc: {
                secure: grpcSecure || false,
                host: grpcHost || 'localhost',
                port: grpcPort || 50051,
            },
        },
        auth,
        ...rest,
    }).catch((e) => {
        throw new errors_js_1.WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
    });
}
