import { z } from 'zod';
import { CodebaseIndexSchema, CodeEdgeSchema, CodeNodeSchema, ParsedFileSchema, } from './types.js';
export const SlimParsedFileSchema = ParsedFileSchema.omit({ content: true, lines: true, excerpt: true }).extend({
    content: z.undefined().optional(),
    lines: z.undefined().optional(),
    excerpt: z.undefined().optional(),
});
export const CodebaseIndexSlimSchema = CodebaseIndexSchema.extend({
    contentMode: z.literal('none'),
    files: z.array(SlimParsedFileSchema),
    nodes: z.array(CodeNodeSchema),
    edges: z.array(CodeEdgeSchema),
});
function slimFile(file) {
    const { content: _content, lines: _lines, excerpt: _excerpt, ...slim } = file;
    return slim;
}
function slimCapabilities(capabilities) {
    return Object.fromEntries(Object.entries(capabilities).map(([language, value]) => [language, { ...value, content: false }]));
}
export function slimifyIndex(index) {
    return {
        ...index,
        contentMode: 'none',
        capabilities: slimCapabilities(index.capabilities),
        files: index.files.map(slimFile),
    };
}
export async function slimifyIndexCooperative(index, yielder) {
    const files = [];
    for (let i = 0; i < index.files.length; i++) {
        files.push(slimFile(index.files[i]));
        if ((i + 1) % 256 === 0) {
            await yielder.tick();
        }
    }
    return {
        ...index,
        contentMode: 'none',
        capabilities: slimCapabilities(index.capabilities),
        files,
    };
}
export function parseSlimIndex(value) {
    const parsed = typeof value === 'string' ? JSON.parse(value) : value;
    return CodebaseIndexSlimSchema.parse(parsed);
}
export function tryParseSlimIndex(value) {
    try {
        const parsed = typeof value === 'string' ? JSON.parse(value) : value;
        const result = CodebaseIndexSlimSchema.safeParse(parsed);
        return result.success ? result.data : undefined;
    }
    catch {
        return undefined;
    }
}
export function validateSlimIndex(value) {
    let parsed;
    if (typeof value === 'string') {
        try {
            parsed = JSON.parse(value);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            return {
                success: false,
                error: new z.ZodError([{ code: z.ZodIssueCode.custom, path: [], message: `Invalid JSON: ${message}` }]),
            };
        }
    }
    else {
        parsed = value;
    }
    const result = CodebaseIndexSlimSchema.safeParse(parsed);
    if (result.success)
        return { success: true, data: result.data };
    return { success: false, error: result.error };
}
//# sourceMappingURL=slim.js.map