package com.emoon.knowledge.chain.loader;

import com.emoon.knowledge.chain.split.TextSplitter;
import lombok.AllArgsConstructor;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * PDF 文件加载器
 * 对应原 org.ruoyi.chain.loader.PdfFileLoader 的能力迁移
 */
@Component
@AllArgsConstructor
public class PdfFileLoader implements ResourceLoader {

    private final TextSplitter textSplitter;

    @Override
    public String getContent(InputStream inputStream) {
        try (PDDocument document = PDDocument.load(inputStream)) {
            PDFTextStripper textStripper = new PDFTextStripper();
            return textStripper.getText(document);
        } catch (IOException e) {
            throw new RuntimeException("读取 PDF 文件失败", e);
        }
    }

    @Override
    public List<String> getChunkList(String content, String kid) {
        return textSplitter.split(content, kid);
    }
}
