import { BestAgentError } from "@best-agent/foundation";
import { isPlainObject, readStringField } from "./builtins/shared.js";
export function normalizeAskUserInput(input) {
    if (typeof input === "string") {
        throw new BestAgentError("TOOL_INPUT_INVALID", 'ask-user string input is not allowed; send an object question with 2-4 options. Use kind "input" explicitly only for rare freeform-only prompts.');
    }
    if (!isPlainObject(input)) {
        throw new BestAgentError("TOOL_INPUT_INVALID", "ask-user expects a string or object input.");
    }
    const candidates = Array.isArray(input.questions) ? input.questions : [input];
    if (candidates.length === 0) {
        throw new BestAgentError("TOOL_INPUT_INVALID", "ask-user requires at least one question.");
    }
    return {
        questions: candidates.map((candidate, index) => normalizeAskUserQuestion(candidate, index)),
    };
}
export function formatAskUserAnswers(answers) {
    return JSON.stringify(answers);
}
export function coerceLooseStringInput(toolName, input) {
    if (typeof input === "string") {
        return input;
    }
    if (isPlainObject(input)) {
        const candidate = readStringField(input, ["input", "text", "content", "query", "command"]);
        if (candidate !== undefined) {
            return candidate;
        }
    }
    throw new BestAgentError("TOOL_INPUT_INVALID", `${toolName} requires a string input.`);
}
function normalizeAskUserQuestion(value, index) {
    if (!isPlainObject(value)) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${index + 1} must be an object.`);
    }
    const question = normalizeRequiredString(value.question, `ask-user question ${index + 1} is missing a question.`);
    const id = normalizeOptionalString(value.id) || (index === 0 ? "answer" : `answer_${index + 1}`);
    const header = normalizeOptionalString(value.header);
    const kind = normalizeAskUserKind(value, index);
    if (kind === "input") {
        return normalizeAskUserInputQuestion(value, index, { header, id, question });
    }
    return normalizeAskUserChoiceQuestion(value, index, { header, id, question });
}
function normalizeAskUserKind(value, index) {
    const kind = normalizeOptionalString(value.kind);
    if (!kind) {
        if (Array.isArray(value.options) && value.options.length > 0) {
            return "choice";
        }
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${index + 1} must provide 2-4 options. If freeform-only input is truly required, set kind to "input" explicitly.`);
    }
    if (kind === "choice" || kind === "input") {
        return kind;
    }
    throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${index + 1} kind must be "choice" or "input".`);
}
function normalizeAskUserChoiceQuestion(value, questionIndex, base) {
    const options = Array.isArray(value.options)
        ? value.options.map((option, optionIndex) => normalizeAskUserOption(option, questionIndex, optionIndex))
        : [];
    if (options.length < 2 || options.length > 4) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} choice questions require 2-4 options.`);
    }
    const normalizedLabels = options.map((option) => option.label.trim().toLowerCase());
    if (new Set(normalizedLabels).size !== normalizedLabels.length) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} option labels must be unique.`);
    }
    if (normalizedLabels.includes("other")) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} must not include an "Other" option; the UI provides that automatically.`);
    }
    if (normalizeOptionalString(value.placeholder)) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} choice questions must not include a placeholder.`);
    }
    if (value.multiline === true) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} choice questions must not enable multiline input.`);
    }
    const multiSelect = value.multiSelect === true;
    const allowCustom = value.allowCustom !== false;
    return {
        ...(base.header ? { header: base.header } : {}),
        ...(allowCustom ? {} : { allowCustom }),
        id: base.id,
        kind: "choice",
        ...(multiSelect ? { multiSelect } : {}),
        options,
        question: base.question,
    };
}
function normalizeAskUserInputQuestion(value, questionIndex, base) {
    if (Array.isArray(value.options) && value.options.length > 0) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} input questions must not include options.`);
    }
    if (value.multiSelect === true) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} input questions must not enable multiSelect.`);
    }
    const placeholder = normalizeOptionalString(value.placeholder);
    const multiline = value.multiline === true;
    return {
        ...(base.header ? { header: base.header } : {}),
        id: base.id,
        kind: "input",
        ...(multiline ? { multiline } : {}),
        ...(placeholder ? { placeholder } : {}),
        question: base.question,
    };
}
function normalizeAskUserOption(value, questionIndex, optionIndex) {
    if (typeof value === "string") {
        const label = value.trim();
        if (!label) {
            throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} option ${optionIndex + 1} must not be empty.`);
        }
        return { label };
    }
    if (!isPlainObject(value)) {
        throw new BestAgentError("TOOL_INPUT_INVALID", `ask-user question ${questionIndex + 1} option ${optionIndex + 1} must be a string or object.`);
    }
    const label = normalizeRequiredString(value.label, `ask-user question ${questionIndex + 1} option ${optionIndex + 1} is missing a label.`);
    const description = normalizeOptionalString(value.description);
    return description ? { description, label } : { label };
}
function normalizeOptionalString(value) {
    const normalized = typeof value === "string" ? value.trim() : "";
    return normalized || undefined;
}
function normalizeRequiredString(value, message) {
    const normalized = normalizeOptionalString(value);
    if (!normalized) {
        throw new BestAgentError("TOOL_INPUT_INVALID", message);
    }
    return normalized;
}
//# sourceMappingURL=input-normalization.js.map