"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 __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 });
exports.__TEST_ONLY__ = exports.KernelSourceIngester = void 0;
const fsPromises = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const pathSecurityGate_1 = require("../codebase/pathSecurityGate");
const secretPatterns_1 = require("../security/secretPatterns");
const baseIngester_1 = require("./baseIngester");
const DEFAULT_MAX_CHUNK_CHARS = 1800;
const UNKNOWN_LICENSE = 'UNKNOWN';
function spdxLicense(content) {
    const header = content.split(/\r?\n/).slice(0, 12).join('\n');
    const match = header.match(/SPDX-License-Identifier:\s*([A-Za-z0-9+_.()-]+(?:\s+(?:WITH|OR|AND)\s+[A-Za-z0-9+_.()-]+)*)/);
    return match?.[1]?.trim();
}
function filterPreviewFiles(preview, ref, opts) {
    const prefixes = [
        ...(ref.pathFilters ?? []),
        ...(opts.pathPrefix ? [opts.pathPrefix] : []),
    ].filter(Boolean).map(prefix => prefix.replace(/^\//, ''));
    if (prefixes.length === 0)
        return [];
    return preview.acceptedFiles.filter(file => prefixes.some(prefix => file.relativePath.startsWith(prefix)));
}
class KernelSourceIngester {
    constructor(store, registry, gate = new pathSecurityGate_1.PathSecurityGate()) {
        this.store = store;
        this.registry = registry;
        this.gate = gate;
    }
    async ingest(codebaseId, opts = {}) {
        const ref = this.registry.get(codebaseId);
        if (!ref) {
            throw new Error(`Codebase '${codebaseId}' not found`);
        }
        if (ref.kind !== 'kernel_source') {
            throw new Error(`Codebase '${codebaseId}' is kind=${ref.kind}; kernel ingestion requires kernel_source`);
        }
        if (!ref.vendor) {
            throw new Error(`Kernel codebase '${codebaseId}' requires vendor`);
        }
        if ((ref.pathFilters ?? []).length === 0 && !opts.pathPrefix) {
            throw new Error(`Kernel codebase '${codebaseId}' requires pathFilters or pathPrefix`);
        }
        const preview = await this.gate.preview(ref.rootRealpath);
        if (preview.blocked) {
            this.registry.updateIngestStatus(codebaseId, {
                lastIngestStatus: 'blocked_by_security',
                lastIngestAt: Date.now(),
                lastIngestError: preview.blockedReason,
                blockedFileCount: preview.skippedFiles.length,
            });
            return {
                codebaseId,
                filesProcessed: 0,
                chunksAdded: 0,
                chunksSkipped: 0,
                blockedFileCount: preview.skippedFiles.length,
                redactionHitCount: 0,
                errors: [{ filePath: ref.displayName, reason: preview.blockedReason ?? 'blocked' }],
            };
        }
        const result = {
            codebaseId,
            filesProcessed: 0,
            chunksAdded: 0,
            chunksSkipped: 0,
            blockedFileCount: preview.skippedFiles.length,
            redactionHitCount: 0,
            errors: [],
        };
        const maxChars = opts.maxChunkChars ?? DEFAULT_MAX_CHUNK_CHARS;
        for (const file of filterPreviewFiles(preview, ref, opts)) {
            result.filesProcessed++;
            await new Promise(r => setImmediate(r));
            try {
                const absolutePath = path.join(ref.rootRealpath, file.relativePath);
                const content = await fsPromises.readFile(absolutePath, 'utf-8');
                const license = ref.licenseTag ?? spdxLicense(content);
                const chunks = (0, baseIngester_1.chunkSourceBySymbols)(content, maxChars);
                if (chunks.length === 0) {
                    result.chunksSkipped++;
                    continue;
                }
                for (const chunk of chunks) {
                    const redaction = (0, secretPatterns_1.redactSecrets)(chunk.text);
                    result.redactionHitCount += redaction.redactedCount;
                    this.store.addChunk({
                        chunkId: (0, baseIngester_1.stableChunkId)([codebaseId, ref.indexGeneration, file.relativePath, chunk.startLine]),
                        kind: 'kernel_source',
                        uri: `codebase://${codebaseId}/${file.relativePath}`,
                        title: file.relativePath.split('/').pop(),
                        snippet: redaction.text,
                        tokenCount: (0, baseIngester_1.estimateTokenCount)(redaction.text),
                        license: license ?? UNKNOWN_LICENSE,
                        indexedAt: Date.now(),
                        filePath: file.relativePath,
                        lineRange: { start: chunk.startLine, end: chunk.endLine },
                        ...(chunk.symbol ? { symbol: chunk.symbol } : {}),
                        language: (0, baseIngester_1.languageForPath)(file.relativePath),
                        ...(ref.commitHash ? { commitHash: ref.commitHash } : {}),
                        vendor: ref.vendor,
                        ...(ref.buildId ? { buildId: ref.buildId } : {}),
                        codebaseId,
                        registryOrigin: 'codebase_registry',
                        ...(!license ? { unsupportedReason: 'license_unknown' } : {}),
                    });
                    result.chunksAdded++;
                }
            }
            catch (error) {
                result.chunksSkipped++;
                result.errors.push({
                    filePath: file.relativePath,
                    reason: error instanceof Error ? error.message : String(error),
                });
            }
        }
        this.store.flush();
        this.registry.updateIngestStatus(codebaseId, {
            lastIngestStatus: result.errors.length > 0 ? 'partial' : 'ok',
            lastIngestAt: Date.now(),
            lastIngestError: result.errors[0]?.reason,
            chunkCount: result.chunksAdded,
            blockedFileCount: result.blockedFileCount,
            redactionHitCount: result.redactionHitCount,
        });
        return result;
    }
}
exports.KernelSourceIngester = KernelSourceIngester;
exports.__TEST_ONLY__ = {
    spdxLicense,
    filterPreviewFiles,
};
//# sourceMappingURL=kernelSourceIngester.js.map