import Database from 'better-sqlite3';
import fs from 'node:fs';
import path from 'node:path';
export class ReadonlyDbError extends Error {
    code;
    hint;
    constructor(code, message, hint) {
        super(message);
        this.code = code;
        this.hint = hint;
        this.name = 'ReadonlyDbError';
    }
}
export function openDbReadonly(projectDir) {
    const dbPath = path.join(projectDir, '.claude4spec', 'db.sqlite');
    if (!fs.existsSync(dbPath)) {
        throw new ReadonlyDbError('PROJECT_NOT_FOUND', `no claude4spec project found at ${projectDir}`, 'run `npx claude4spec` first or pass `--project <path>`');
    }
    let handle;
    try {
        handle = new Database(dbPath, { readonly: true, fileMustExist: true });
    }
    catch (err) {
        throw new ReadonlyDbError('PROJECT_NOT_FOUND', `failed to open ${dbPath}: ${err.message}`);
    }
    return {
        handle,
        dbPath,
        close: () => handle.close(),
    };
}
//# sourceMappingURL=readonly.js.map