"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionInfo = void 0;
const tempdb_1 = require("./tempdb");
let ConnectionInfo = /** @class */ (() => {
    class ConnectionInfo {
        constructor(_connectionString, _type) {
            this._connectionString = _connectionString;
            this._type = _type;
            this._options = {};
            this._user = "";
            this._pass = "";
            this._host = "";
            this._database = "";
            this._port = 0;
            const config = (_connectionString || "").split(";")
                .reduce((acc, cur) => {
                const parts = cur.split("=");
                acc[parts[0]] = parts.slice(1).join("=");
                return acc;
            }, {}), keys = Object.keys(config);
            keys.forEach(k => {
                const lower = k.toLowerCase(), value = config[k], mapped = ConnectionInfo.optionMap[lower] || lower;
                if (!this.mapToInstance(mapped, value)) {
                    this._options[k] = value;
                }
            });
        }
        get options() {
            return { ...this._options };
        }
        get knexConfig() {
            return {
                client: this.makeKnexClientString(),
                connection: this.config,
                useNullAsDefault: true
            };
        }
        get config() {
            return this.makeConnectionInfo();
        }
        clone() {
            return new ConnectionInfo(this._connectionString, this._type);
        }
        makeConnectionInfo() {
            if (this._type === tempdb_1.Databases.sqlite) {
                return {
                    filename: this.sanitizePath(this._database)
                };
            }
            else {
                return {
                    database: this._database,
                    host: this._host,
                    password: this._pass,
                    user: this._user,
                    port: this._port
                };
            }
        }
        sanitizePath(str) {
            if (str.startsWith(`"`)) {
                str = str.substr(1);
            }
            if (str.endsWith(`"`)) {
                str = str.substr(0, str.length - 1);
            }
            return str;
        }
        makeKnexClientString() {
            return this._type === tempdb_1.Databases.sqlite
                ? "sqlite3"
                : this._type;
        }
        mapToInstance(key, value) {
            switch (key) {
                case "user":
                    this._user = value;
                    return true;
                case "password":
                case "pass":
                    this._pass = value;
                    return true;
                case "host":
                    this._host = value;
                    return true;
                case "database":
                case "datasource":
                    this._database = value;
                    return true;
                case "port":
                    const parsed = parseInt(value, 10);
                    if (isNaN(parsed)) {
                        throw new Error(`Invalid value for port: ${value}`);
                    }
                    this._port = parsed;
                    return true;
            }
            return false;
        }
    }
    ConnectionInfo.optionMap = {
        "user id": "user",
        "server": "host",
        "initial catalog": "database"
    };
    return ConnectionInfo;
})();
exports.ConnectionInfo = ConnectionInfo;
