/**
 * 输入：后端的原始题目数组（含 answerlist / 或 answer 的 | 分隔）
 * 输出：[{ id, title, options: [{ value, label }] }]
 */
export function adaptQuestions(rawList) {
    return (rawList || []).map(q => {
        // 选项优先用 answerlist；没有就用 answer 按 | 切分
        const list = Array.isArray(q.answerlist) && q.answerlist.length
            ? q.answerlist
            : String(q.answer || '')
                .split('|')
                .map(s => s.trim())
                .filter(Boolean);

        const options = list.map(txt => {
            const clean = String(txt).replace(/\r?\n/g, '').trim();
            return { value: clean, label: clean };
        });

        return {
            id: q.id,
            title: String(q.title || '').trim(),
            options
        };
    });
}



/**
 * 输入：后端原始题目数组（兼容两种结构）
 *   新：{ qid, title, list: string[] }
 *   旧：{ id, title, answerlist?: string[], answer?: "A|B|C" }
 * 输出：[{ id, title, options: [{ value, label }] }]
 */
export function adaptQuestionsHome(rawList) {
    return (rawList || []).map(q => {

        const id = q.id;

        // 2) 取选项数组：优先 list，其次 answerlist，其次用 answer 拆 |
        let arr = [];
        if (Array.isArray(q.list) && q.list.length) {
            arr = q.list;
        } else if (Array.isArray(q.answerlist) && q.answerlist.length) {
            arr = q.answerlist;
        } else if (q.answer) {
            arr = String(q.answer)
                .split('|')
                .map(s => s.trim())
                .filter(Boolean);
        }

        // 3) 规范化为 options
        const options = arr.map(txt => {
            const clean = String(txt).replace(/\r?\n/g, '').trim();
            return { value: clean, label: clean };
        });

        return {
            id,
            title: String(q.title || '').trim(),
            options
        };
    });
}
