"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.AospSourceIngester = void 0;
const fsPromises = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const pathSecurityGate_1 = require("../codebase/pathSecurityGate");
const baseIngester_1 = require("./baseIngester");
const DEFAULT_MAX_CHUNK_CHARS = 2200;
function filterPreviewFiles(preview, ref, opts) {
    const prefixes = [
        ...(ref.pathFilters ?? []),
        ...(opts.pathPrefix ? [opts.pathPrefix] : []),
    ].filter(Boolean).map(prefix => prefix.replace(/^\//, ''));
    if (prefixes.length === 0)
        return preview.acceptedFiles;
    return preview.acceptedFiles.filter(file => prefixes.some(prefix => file.relativePath.startsWith(prefix)));
}
class AospSourceIngester {
    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 !== 'aosp') {
            throw new Error(`Codebase '${codebaseId}' is kind=${ref.kind}; AOSP ingestion requires aosp`);
        }
        if (!ref.licenseTag) {
            throw new Error(`AOSP codebase '${codebaseId}' requires licenseTag`);
        }
        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,
                errors: [{ filePath: ref.displayName, reason: preview.blockedReason ?? 'blocked' }],
            };
        }
        const result = {
            codebaseId,
            filesProcessed: 0,
            chunksAdded: 0,
            chunksSkipped: 0,
            blockedFileCount: preview.skippedFiles.length,
            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 content = await fsPromises.readFile(path.join(ref.rootRealpath, file.relativePath), 'utf-8');
                const chunks = (0, baseIngester_1.chunkSourceBySymbols)(content, maxChars);
                if (chunks.length === 0) {
                    result.chunksSkipped++;
                    continue;
                }
                for (const chunk of chunks) {
                    this.store.addChunk({
                        chunkId: (0, baseIngester_1.stableChunkId)([codebaseId, ref.indexGeneration, file.relativePath, chunk.startLine]),
                        kind: 'aosp',
                        uri: `codebase://${codebaseId}/${file.relativePath}`,
                        title: file.relativePath.split('/').pop(),
                        snippet: chunk.text,
                        tokenCount: (0, baseIngester_1.estimateTokenCount)(chunk.text),
                        license: ref.licenseTag,
                        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 } : {}),
                        ...(ref.buildId ? { buildId: ref.buildId } : {}),
                        codebaseId,
                        registryOrigin: 'codebase_registry',
                    });
                    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,
        });
        return result;
    }
}
exports.AospSourceIngester = AospSourceIngester;
//# sourceMappingURL=aospSourceIngester.js.map