import { databaseTypes } from '../../../lib/jhipster/index.js';
import databaseData from './database-data.js';
const { ORACLE, MYSQL, POSTGRESQL, MARIADB, MSSQL, H2_DISK, H2_MEMORY } = databaseTypes;
export default function getDatabaseUrl(databaseType, protocol, options = {}) {
    if (!protocol) {
        throw new Error('protocol is required');
    }
    const { databaseName, hostname, skipExtraOptions } = options;
    if (!databaseName) {
        throw new Error("option 'databaseName' is required");
    }
    if ([MYSQL, MARIADB, POSTGRESQL, ORACLE, MSSQL].includes(databaseType) && !hostname) {
        throw new Error(`option 'hostname' is required for ${databaseType} databaseType`);
    }
    else if (![MYSQL, MARIADB, POSTGRESQL, ORACLE, MSSQL, H2_DISK, H2_MEMORY].includes(databaseType)) {
        throw new Error(`${databaseType} databaseType is not supported`);
    }
    let databaseDataForType = databaseData[databaseType];
    if (databaseDataForType[protocol]) {
        databaseDataForType = {
            ...databaseDataForType,
            ...databaseDataForType[protocol],
        };
    }
    if (databaseDataForType.getData) {
        databaseDataForType = {
            ...databaseDataForType,
            ...(databaseDataForType.getData(options) || {}),
        };
    }
    const { port = '', protocolSuffix = '', extraOptions = '', localDirectory = options.localDirectory } = databaseDataForType;
    let url = `${protocol}:${protocolSuffix}`;
    if (hostname || localDirectory) {
        url = `${url}${localDirectory || hostname + port}${databaseName}`;
    }
    else {
        url = `${url}${databaseName}${port}`;
    }
    return `${url}${skipExtraOptions ? '' : extraOptions}`;
}
export function getJdbcUrl(databaseType, options) {
    return getDatabaseUrl(databaseType, 'jdbc', options);
}
export function getR2dbcUrl(databaseType, options) {
    return getDatabaseUrl(databaseType, 'r2dbc', options);
}
