import { buildAuthChoiceGroups } from "./auth-choice-options.js";
const BACK_VALUE = "__back";
export async function promptAuthChoiceGrouped(params) {
    const { groups, skipOption } = buildAuthChoiceGroups(params);
    const availableGroups = groups.filter((group) => group.options.length > 0);
    while (true) {
        const providerOptions = [
            ...availableGroups.map((group) => ({
                value: group.value,
                label: group.label,
                hint: group.hint,
            })),
            ...(skipOption ? [skipOption] : []),
        ];
        const providerSelection = (await params.prompter.select({
            message: "模型/认证提供商",
            options: providerOptions,
        }));
        if (providerSelection === "skip") {
            return "skip";
        }
        const group = availableGroups.find((candidate) => candidate.value === providerSelection);
        if (!group || group.options.length === 0) {
            await params.prompter.note("该提供商没有可用的认证方法。", "模型/认证选择");
            continue;
        }
        const methodSelection = (await params.prompter.select({
            message: `${group.label} 认证方法`,
            options: [...group.options, { value: BACK_VALUE, label: "返回" }],
        }));
        if (methodSelection === BACK_VALUE) {
            continue;
        }
        return methodSelection;
    }
}
