"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizePromptJsonInput = normalizePromptJsonInput;
exports.parsePromptJsonString = parsePromptJsonString;
exports.isConversationMessageArray = isConversationMessageArray;
exports.getConversationPromptValidationError = getConversationPromptValidationError;
exports.getConversationMessageShapeError = getConversationMessageShapeError;
/** Non-standard spaces often pasted from docs/chat; JSON.parse only accepts ASCII whitespace. */
const EXOTIC_WHITESPACE = /[\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]/g;
/** Curly quotes pasted from rich text editors. */
const CURLY_DOUBLE_QUOTES = /[\u201c\u201d\u201e\u2033\u2036]/g;
/** Normalize pasted prompt JSON so JSON.parse accepts common copy/paste artifacts. */
function normalizePromptJsonInput(value) {
    if (typeof value !== 'string') {
        return '';
    }
    return value
        .replace(/^\uFEFF/, '')
        .replace(EXOTIC_WHITESPACE, ' ')
        .replace(CURLY_DOUBLE_QUOTES, '"')
        .trim();
}
/** Parse prompt JSON; normalizes paste artifacts. Returns null for empty input. */
function parsePromptJsonString(value) {
    const normalized = normalizePromptJsonInput(value);
    if (!normalized) {
        return null;
    }
    return JSON.parse(normalized);
}
/** True when value is a non-empty array of { role, content } string pairs only. */
function isConversationMessageArray(parsed) {
    if (!Array.isArray(parsed) || parsed.length === 0) {
        return false;
    }
    return parsed.every((msg) => {
        if (typeof msg !== 'object' || msg === null || Object.keys(msg).length !== 2) {
            return false;
        }
        return ('role' in msg &&
            'content' in msg &&
            typeof msg.role === 'string' &&
            typeof msg.content === 'string');
    });
}
/** Returns a user-facing error when conversation JSON rules fail; null when valid or not a conversation array. */
function getConversationPromptValidationError(parsed) {
    if (!isConversationMessageArray(parsed)) {
        return null;
    }
    const normalizedRoles = parsed.map((msg) => msg.role.trim().toLowerCase());
    const hasUser = normalizedRoles.some((role) => role === 'user');
    const hasAssistant = normalizedRoles.some((role) => role === 'assistant');
    if (!hasUser || !hasAssistant) {
        return 'Conversation JSON must include at least one user message and one assistant message.';
    }
    const lastRole = normalizedRoles[normalizedRoles.length - 1];
    if (lastRole !== 'user') {
        return 'The last message in conversation json must be a user turn.';
    }
    return null;
}
/** Form validate helper: invalid message shape in a conversation array. */
function getConversationMessageShapeError(parsed) {
    if (!Array.isArray(parsed)) {
        return 'Prompt JSON must be an array of messages';
    }
    const invalidMessage = parsed.find((msg) => {
        if (typeof msg !== 'object' ||
            msg === null ||
            Object.keys(msg).length !== 2 ||
            !('role' in msg) ||
            !('content' in msg) ||
            typeof msg.role !== 'string' ||
            typeof msg.content !== 'string') {
            return true;
        }
        return false;
    });
    if (invalidMessage) {
        return 'Each message must be an object with only "role" and "content" as string fields';
    }
    return null;
}
//# sourceMappingURL=validateConversationPromptJson.js.map