import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { getResourcesDir } from './index.js';
const schemaCache = new Map();
function loadSchema(name) {
    const cached = schemaCache.get(name);
    if (cached) {
        return cached;
    }
    const schemaPath = join(getResourcesDir(), 'schemas', name);
    const content = readFileSync(schemaPath, 'utf-8');
    const parsed = JSON.parse(content);
    schemaCache.set(name, parsed);
    return parsed;
}
function cloneSchema(schema) {
    return JSON.parse(JSON.stringify(schema));
}
export function loadJudgmentSchema() {
    return loadSchema('judgment.json');
}
export function loadEvaluationSchema() {
    return loadSchema('evaluation.json');
}
export function loadDecompositionSchema(maxParts) {
    if (!Number.isInteger(maxParts) || maxParts <= 0) {
        throw new Error(`maxParts must be a positive integer: ${maxParts}`);
    }
    const schema = cloneSchema(loadSchema('decomposition.json'));
    const properties = schema.properties;
    if (!properties || typeof properties !== 'object' || Array.isArray(properties)) {
        throw new Error('decomposition schema is invalid: properties is missing');
    }
    const rawParts = properties.parts;
    if (!rawParts || typeof rawParts !== 'object' || Array.isArray(rawParts)) {
        throw new Error('decomposition schema is invalid: parts is missing');
    }
    rawParts.maxItems = maxParts;
    return schema;
}
export function loadMorePartsSchema(maxAdditionalParts) {
    if (!Number.isInteger(maxAdditionalParts) || maxAdditionalParts <= 0) {
        throw new Error(`maxAdditionalParts must be a positive integer: ${maxAdditionalParts}`);
    }
    const schema = cloneSchema(loadSchema('more-parts.json'));
    const properties = schema.properties;
    if (!properties || typeof properties !== 'object' || Array.isArray(properties)) {
        throw new Error('more-parts schema is invalid: properties is missing');
    }
    const rawParts = properties.parts;
    if (!rawParts || typeof rawParts !== 'object' || Array.isArray(rawParts)) {
        throw new Error('more-parts schema is invalid: parts is missing');
    }
    rawParts.maxItems = maxAdditionalParts;
    return schema;
}
//# sourceMappingURL=schema-loader.js.map