import EmbeddedPostgres from "embedded-postgres";
import * as fs from "fs";
import * as path from "path";
export async function startDatabase(db, port, dataDir = ".specific/data") {
    if (db.engine === "redis") {
        return startRedis(db, port);
    }
    if (db.engine === "object_store") {
        return startObjectStore(db, port, dataDir);
    }
    return startPostgres(db, port, dataDir);
}
async function startPostgres(db, port, dataDir) {
    const dbDataPath = path.join(process.cwd(), dataDir, db.name);
    const host = "127.0.0.1";
    const user = "postgres";
    const password = "postgres";
    const pg = new EmbeddedPostgres({
        databaseDir: dbDataPath,
        port,
        user,
        password,
        persistent: true,
        onLog: () => { },
        onError: () => { },
    });
    // Only initialize if the data directory doesn't exist yet
    const dataExists = fs.existsSync(dbDataPath);
    if (!dataExists) {
        await pg.initialise();
    }
    await pg.start();
    // Create the database if it doesn't exist
    try {
        await pg.createDatabase(db.name);
    }
    catch {
        // Database may already exist, which is fine
    }
    return {
        name: db.name,
        engine: "postgres",
        port,
        host,
        user,
        password,
        dbName: db.name,
        url: `postgres://${user}:${password}@${host}:${port}/${db.name}`,
        async stop() {
            await pg.stop();
        },
    };
}
async function startRedis(db, port) {
    const { RedisMemoryServer } = await import("redis-memory-server");
    const server = new RedisMemoryServer({
        instance: {
            port,
        },
    });
    const host = await server.getHost();
    const actualPort = await server.getPort();
    return {
        name: db.name,
        engine: "redis",
        port: actualPort,
        host,
        user: "",
        password: "",
        dbName: db.name,
        url: `redis://${host}:${actualPort}`,
        async stop() {
            await server.stop();
        },
    };
}
async function startObjectStore(db, port, dataDir) {
    const S3rver = (await import("s3rver")).default;
    const dbDataPath = path.join(process.cwd(), dataDir, db.name);
    // Ensure data directory exists
    if (!fs.existsSync(dbDataPath)) {
        fs.mkdirSync(dbDataPath, { recursive: true });
    }
    const host = "127.0.0.1";
    const accessKey = "S3RVER";
    const secretKey = "S3RVER";
    const server = new S3rver({
        address: host,
        port,
        directory: dbDataPath,
        silent: true,
        allowMismatchedSignatures: true,
        configureBuckets: [{ name: db.name }],
    });
    await server.run();
    const endpoint = `http://${host}:${port}`;
    return {
        name: db.name,
        engine: "object_store",
        port,
        host,
        user: "",
        password: "",
        dbName: db.name,
        url: endpoint,
        endpoint,
        accessKey,
        secretKey,
        async stop() {
            await server.close();
        },
    };
}
//# sourceMappingURL=database-manager.js.map