import Ajv from 'ajv';
const ajv = new Ajv({
    allErrors: false,
    schemaId: 'auto',
    unknownFormats: 'ignore',
});
const validatorCache = new WeakMap();
function isPlainObject(value) {
    return typeof value === 'object' && value != null && !Array.isArray(value);
}
function formatPathSegment(segment) {
    return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(segment)
        ? `.${segment}`
        : `[${JSON.stringify(segment)}]`;
}
function formatInstancePath(error) {
    const basePath = error.dataPath === '' ? '$' : `$${error.dataPath}`;
    if (error.keyword === 'required') {
        const missingProperty = error.params.missingProperty;
        if (typeof missingProperty === 'string') {
            return `${basePath}${formatPathSegment(missingProperty)}`;
        }
    }
    if (error.keyword === 'additionalProperties') {
        const additionalProperty = error.params.additionalProperty;
        if (typeof additionalProperty === 'string') {
            return `${basePath}${formatPathSegment(additionalProperty)}`;
        }
    }
    return basePath;
}
function formatValidationError(error) {
    if (!error) {
        return 'Structured output does not satisfy the schema';
    }
    const path = formatInstancePath(error);
    if (error.keyword === 'required') {
        return `${path} is required`;
    }
    if (error.keyword === 'additionalProperties') {
        return `${path} is not allowed by the schema`;
    }
    const message = error.message?.replace(/^should\b/, 'must');
    return message ? `${path} ${message}` : path;
}
function getValidator(schema) {
    const cached = validatorCache.get(schema);
    if (cached) {
        return cached;
    }
    try {
        const validate = ajv.compile(schema);
        validatorCache.set(schema, validate);
        return validate;
    }
    catch (error) {
        const detail = error instanceof Error ? error.message : String(error);
        throw new Error(`Structured output schema is invalid: ${detail}`);
    }
}
export function validateStructuredOutputAgainstSchema(value, schema) {
    if (!isPlainObject(schema)) {
        throw new Error('Structured output schema must be an object');
    }
    const validate = getValidator(schema);
    if (validate(value)) {
        return;
    }
    throw new Error(formatValidationError(validate.errors?.[0]));
}
//# sourceMappingURL=structured-output-schema-validator.js.map