import { VALID_PROMPT_BUILDER_MODELS, DEFAULT_PROMPT_BUILDER_MODEL, MAX_MESSAGES, MAX_MESSAGE_CHARS, } from "./constants.js";
import { promptBuilderError } from "./errors.js";
import { normalizeAttachments } from "./attachments.js";
export function normalizeModel(raw) {
    if (typeof raw !== "string" || raw.trim().length === 0)
        return DEFAULT_PROMPT_BUILDER_MODEL;
    if (!VALID_PROMPT_BUILDER_MODELS.has(raw)) {
        throw promptBuilderError("model must be one of: gpt-5.5, gpt-5.4, gpt-5.4-mini", "PROMPT_BUILDER_BAD_MODEL");
    }
    return raw;
}
export function normalizeMessages(raw) {
    if (!Array.isArray(raw)) {
        throw promptBuilderError("messages must be an array", "PROMPT_BUILDER_BAD_MESSAGES");
    }
    const messages = raw.slice(-MAX_MESSAGES).map((message) => {
        if (!message || typeof message !== "object") {
            throw promptBuilderError("each message must be an object", "PROMPT_BUILDER_BAD_MESSAGES");
        }
        const item = message;
        const role = item.role === "assistant" ? "assistant" : item.role === "user" ? "user" : null;
        if (!role) {
            throw promptBuilderError("message role must be user or assistant", "PROMPT_BUILDER_BAD_MESSAGES");
        }
        const content = typeof item.content === "string" ? item.content.trim() : "";
        if (!content && role === "user") {
            throw promptBuilderError("message content is required", "PROMPT_BUILDER_EMPTY_MESSAGE");
        }
        return {
            role,
            content: content.slice(0, MAX_MESSAGE_CHARS),
            attachments: normalizeAttachments(item.attachments),
        };
    });
    const last = messages.at(-1);
    if (!last || last.role !== "user" || !last.content.trim()) {
        throw promptBuilderError("last message must be a non-empty user message", "PROMPT_BUILDER_EMPTY_MESSAGE");
    }
    return messages;
}
