import { ValidationError } from './errors.js';
import { contextSearchSchema, feedbackSchema, contextQualityReportSchema, contextPackIdArgumentsSchema, } from './schemas/context.js';
import { parseOrThrow } from './schemas/primitives.js';
export { TASK_TYPES, CONTEXT_MODES, CONTEXT_NOISE_TOLERANCES, KNOWLEDGE_ITEM_TYPES, TRIGGER_TYPES, FEEDBACK_TYPES, CONTEXT_QUALITY_FEEDBACK_TYPES, AGENT_SESSION_OUTCOMES, AGENT_LEARNING_MODES, REFLECTION_DRAFT_STATUSES, AGENT_LEARNING_SIGNAL_KINDS, MAINTENANCE_ITEM_KINDS, } from './schemas/enums.js';
import { KNOWLEDGE_REVIEW_FILTERS, KNOWLEDGE_STATUSES, LEARNING_REVIEW_STATUSES, LEARNING_PROPOSAL_TYPES, } from './schemas/enums.js';
import { knowledgeSchema, knowledgePatchSchema, knowledgeRelationSchema, knowledgeRelationPatchSchema, knowledgeConflictPatchSchema, knowledgeGapPatchSchema, learningProposalPatchSchema, } from './schemas/knowledge.js';
import { ingestFilesSchema } from './schemas/ingest.js';
import { startAgentSessionSchema, recordContextDecisionSchema, finishAgentSessionSchema, captureLearningSignalSchema, appendSessionNoteSchema, } from './schemas/agent-session.js';
import { reflectionDraftSchema, reflectionDraftPatchSchema, reflectionDraftIdArgumentsSchema, reflectionDraftListSchema, reflectionDraftReviewSchema, } from './schemas/reflection.js';
import { errorLogSchema, errorLogPatchSchema, errorLogListSchema, collectErrorLogsSchema, createErrorLogReflectionDraftSchema, resolveErrorLogSchema, errorLogIdArgumentsSchema, } from './schemas/error-log.js';
import { maintenanceProposeSchema, maintenanceApplySchema, } from './schemas/maintenance.js';
import { cleanupOperationsSchema, createBackupSchema, backupRetentionSchema, restoreBackupSchema, } from './schemas/backup.js';
export function validateKnowledgeInput(value) {
    return parseOrThrow(knowledgeSchema, value, 'knowledge input');
}
export function validateKnowledgePatchInput(value) {
    return parseOrThrow(knowledgePatchSchema, value, 'knowledge patch input');
}
export function validateKnowledgeRelationInput(value) {
    return parseOrThrow(knowledgeRelationSchema, value, 'knowledge relation input');
}
export function validateKnowledgeRelationPatchInput(value) {
    return parseOrThrow(knowledgeRelationPatchSchema, value, 'knowledge relation patch input');
}
export function validateKnowledgeConflictPatchInput(value) {
    return parseOrThrow(knowledgeConflictPatchSchema, value, 'knowledge conflict patch input');
}
export function validateKnowledgeGapPatchInput(value) {
    return parseOrThrow(knowledgeGapPatchSchema, value, 'knowledge gap patch input');
}
export function validateLearningProposalPatchInput(value) {
    return parseOrThrow(learningProposalPatchSchema, value, 'learning proposal patch input');
}
export function validateMaintenanceProposeInput(value) {
    return parseOrThrow(maintenanceProposeSchema, value, 'maintenance propose input');
}
export function validateMaintenanceApplyInput(value) {
    return parseOrThrow(maintenanceApplySchema, value, 'maintenance apply input');
}
export function validateIngestFilesRequest(value) {
    return parseOrThrow(ingestFilesSchema, value, 'file ingestion input');
}
export function validateContextSearchInput(value) {
    return parseOrThrow(contextSearchSchema, value, 'context search input');
}
export function validateStartAgentSessionInput(value) {
    return parseOrThrow(startAgentSessionSchema, value, 'agent session input');
}
export function validateFeedbackInput(value) {
    return parseOrThrow(feedbackSchema, value, 'feedback input');
}
export function validateContextQualityReportInput(value) {
    return parseOrThrow(contextQualityReportSchema, value, 'context quality report input');
}
export function validateRecordAgentContextDecisionInput(value, sessionId) {
    const parsed = parseOrThrow(recordContextDecisionSchema, withSessionId(value, sessionId), 'agent context decision input');
    return sessionId ? { ...parsed, sessionId } : parsed;
}
export function validateFinishAgentSessionInput(value, sessionId) {
    const parsed = parseOrThrow(finishAgentSessionSchema, withSessionId(value, sessionId), 'finish agent session input');
    return sessionId ? { ...parsed, sessionId } : parsed;
}
export function validateCaptureAgentLearningSignalInput(value, sessionId) {
    const parsed = parseOrThrow(captureLearningSignalSchema, withSessionId(value, sessionId), 'agent learning signal input');
    return sessionId ? { ...parsed, sessionId } : parsed;
}
export function validateReflectionDraftInput(value) {
    return parseOrThrow(reflectionDraftSchema, value, 'reflection draft input');
}
export function validateReflectionDraftPatchInput(value) {
    return parseOrThrow(reflectionDraftPatchSchema, value, 'reflection draft patch input');
}
export function validateAppendAgentSessionNoteInput(value, sessionId) {
    const parsed = parseOrThrow(appendSessionNoteSchema, withSessionId(value, sessionId), 'agent session note input');
    return sessionId ? { ...parsed, sessionId } : parsed;
}
export function validateReflectionDraftIdArguments(value) {
    return parseOrThrow(reflectionDraftIdArgumentsSchema, value, 'reflection draft arguments');
}
export function validateReflectionDraftListInput(value) {
    return parseOrThrow(reflectionDraftListSchema, value, 'reflection draft list input');
}
export function validateReflectionDraftReviewInput(value) {
    return parseOrThrow(reflectionDraftReviewSchema, value, 'reflection draft review input');
}
export function validateCleanupOperationsInput(value) {
    return parseOrThrow(cleanupOperationsSchema, value, 'cleanup input');
}
export function validateCreateBackupInput(value) {
    return parseOrThrow(createBackupSchema, value, 'backup input');
}
export function validateBackupRetentionInput(value) {
    return parseOrThrow(backupRetentionSchema, value, 'backup retention input');
}
export function validateRestoreBackupInput(value, backupIdOrPath) {
    const parsed = parseOrThrow(restoreBackupSchema, value, 'restore backup input');
    return backupIdOrPath ? { ...parsed, backupIdOrPath } : parsed;
}
export function validateErrorLogInput(value) {
    return parseOrThrow(errorLogSchema, value, 'error log input');
}
export function validateErrorLogPatchInput(value) {
    return parseOrThrow(errorLogPatchSchema, value, 'error log patch input');
}
export function validateErrorLogListInput(value) {
    return parseOrThrow(errorLogListSchema, value, 'error log list input');
}
export function validateCollectErrorLogsInput(value) {
    return parseOrThrow(collectErrorLogsSchema, value, 'error log collection input');
}
export function validateCreateErrorLogReflectionDraftInput(value) {
    return parseOrThrow(createErrorLogReflectionDraftSchema, value, 'error log reflection draft input');
}
export function validateResolveErrorLogInput(value, id) {
    const parsed = parseOrThrow(resolveErrorLogSchema, withId(value, id), 'error log resolution input');
    return id ? { ...parsed, id } : parsed;
}
export function validateErrorLogIdArguments(value) {
    return parseOrThrow(errorLogIdArgumentsSchema, value, 'error log arguments');
}
/**
 * Query-param enum parser (matches the old validate*Query helpers): a `null`
 * value (param absent) yields `undefined`; a non-member throws ValidationError
 * with the same `query.<field>: must be one of …` message shape.
 */
function parseEnumQuery(value, allowed, field) {
    if (value === null) {
        return undefined;
    }
    if (!allowed.includes(value)) {
        const path = `query.${field}`;
        const message = `${path} must be one of: ${allowed.join(', ')}.`;
        throw new ValidationError(message, [{ path, message }]);
    }
    return value;
}
export function validateKnowledgeReviewFilter(value) {
    return parseEnumQuery(value, KNOWLEDGE_REVIEW_FILTERS, 'review');
}
export function validateKnowledgeStatusQuery(value) {
    return parseEnumQuery(value, KNOWLEDGE_STATUSES, 'status');
}
export function validateLearningReviewStatusQuery(value) {
    return parseEnumQuery(value, LEARNING_REVIEW_STATUSES, 'status');
}
export function validateLearningProposalTypeQuery(value) {
    return parseEnumQuery(value, LEARNING_PROPOSAL_TYPES, 'proposalType');
}
export function validateContextPackIdArguments(value) {
    return parseOrThrow(contextPackIdArgumentsSchema, value, 'context pack arguments');
}
export function expectRecord(value, path) {
    return expectObject(value, path);
}
/**
 * When a sessionId is supplied out-of-band (e.g. from a path param), inject it
 * into the payload so a schema that requires `sessionId` parses; the caller
 * overrides the field with the param value after parse. Non-object payloads are
 * returned untouched so the schema still rejects them with the right message.
 */
function withSessionId(value, sessionId) {
    if (sessionId && value && typeof value === 'object' && !Array.isArray(value)) {
        return { ...value, sessionId };
    }
    return value;
}
/** Same as withSessionId for an out-of-band `id` (e.g. error-log resolve path param). */
function withId(value, id) {
    if (id && value && typeof value === 'object' && !Array.isArray(value)) {
        return { ...value, id };
    }
    return value;
}
function expectObject(value, path) {
    if (!value || typeof value !== 'object' || Array.isArray(value)) {
        const message = `${path}: must be an object.`;
        throw new ValidationError(message, [{ path, message }]);
    }
    return value;
}
