package com.njcn.gather.report.util;

import com.njcn.gather.tools.report.util.BookmarkUtil;
import com.njcn.gather.tools.report.util.Docx4jUtil;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.P;

import javax.xml.bind.JAXBElement;
import java.util.ArrayList;
import java.util.List;

/** 单测辅助：读块文本 / 收集书签名 */
final class Docx4jUtilTextHelper {

    private Docx4jUtilTextHelper() {
    }

    static String textOf(MainDocumentPart mdp, int blockIndex) {
        Object o = mdp.getContent().get(blockIndex);
        Object v = (o instanceof JAXBElement) ? ((JAXBElement<?>) o).getValue() : o;
        return (v instanceof P) ? Docx4jUtil.getTextFromP((P) v) : "";
    }

    /** 直接复用生产扫描器 BookmarkUtil.findAllBookmarks，保证与生产"可见书签"语义一致（含表格内书签） */
    static List<String> allBookmarkNames(MainDocumentPart mdp) {
        List<String> names = new ArrayList<>();
        for (BookmarkUtil.BookmarkInfo info : BookmarkUtil.findAllBookmarks(mdp)) {
            names.add(info.bookmark.getName());
        }
        return names;
    }
}
