"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const module_1 = require("module");
const os = __importStar(require("os"));
const path = __importStar(require("path"));
function stripPlatformExtensionSuffix(loadablePath) {
    const ext = path.extname(loadablePath);
    return ext === '.so' || ext === '.dylib' || ext === '.dll'
        ? loadablePath.slice(0, -ext.length)
        : loadablePath;
}
function loadWithFallback(db, loadablePath) {
    const withoutSuffix = stripPlatformExtensionSuffix(loadablePath);
    db.loadExtension(withoutSuffix);
    return withoutSuffix;
}
function main() {
    const packageRoot = process.argv[2];
    if (!packageRoot) {
        process.stderr.write('Missing package root for sqlite-vss probe child');
        return 1;
    }
    const packageRequire = (0, module_1.createRequire)(path.join(packageRoot, 'package.json'));
    const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-vss-probe-'));
    const dbPath = path.join(tmpDir, 'probe.db');
    let db;
    try {
        const Database = packageRequire('better-sqlite3');
        const vss = packageRequire('sqlite-vss');
        db = new Database(dbPath);
        const getVectorLoadablePath = vss.getVectorLoadablePath || vss.default?.getVectorLoadablePath;
        const getVssLoadablePath = vss.getVssLoadablePath || vss.default?.getVssLoadablePath;
        if (typeof db.loadExtension !== 'function') {
            throw new Error('SQLite connection does not support loadExtension');
        }
        if (typeof getVectorLoadablePath !== 'function' || typeof getVssLoadablePath !== 'function') {
            throw new Error('sqlite-vss loadable path functions not found');
        }
        const vectorPath = loadWithFallback(db, getVectorLoadablePath());
        const vssPath = loadWithFallback(db, getVssLoadablePath());
        db.exec('CREATE VIRTUAL TABLE probe_vectors USING vss0(embedding(3));');
        process.stdout.write(JSON.stringify({ vector: vectorPath, vss: vssPath }));
        return 0;
    }
    catch (error) {
        process.stderr.write(error instanceof Error ? error.stack || error.message : String(error));
        return 1;
    }
    finally {
        try {
            db?.close();
        }
        catch { }
        try {
            fs.rmSync(tmpDir, { recursive: true, force: true });
        }
        catch { }
    }
}
process.exit(main());
//# sourceMappingURL=sqlite-vss-probe-child.js.map