"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ENTERPRISE_DB_PATH_ENV = void 0;
exports.resolveEnterpriseDbPath = resolveEnterpriseDbPath;
exports.openEnterpriseDb = openEnterpriseDb;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
const enterpriseSchema_1 = require("./enterpriseSchema");
exports.ENTERPRISE_DB_PATH_ENV = 'SMARTPERFETTO_ENTERPRISE_DB_PATH';
function resolveEnterpriseDbPath(env = process.env) {
    const configured = env[exports.ENTERPRISE_DB_PATH_ENV];
    if (configured && configured.trim().length > 0) {
        return path_1.default.resolve(configured);
    }
    return path_1.default.join(process.cwd(), 'data', 'sessions', 'sessions.db');
}
function openEnterpriseDb(dbPath = resolveEnterpriseDbPath()) {
    fs_1.default.mkdirSync(path_1.default.dirname(dbPath), { recursive: true });
    const db = new better_sqlite3_1.default(dbPath);
    db.pragma('journal_mode = WAL');
    db.pragma('busy_timeout = 5000');
    db.pragma('foreign_keys = ON');
    (0, enterpriseSchema_1.applyEnterpriseMinimalSchema)(db);
    return db;
}
//# sourceMappingURL=enterpriseDb.js.map