import { loadPdfJs, loadMammoth } from './libs.js';
export function sniffExt(name){const m=name.toLowerCase().match(/\.([a-z0-9]+)$/);return m?m[1]:'txt';}
export function readFileAsText(file){return new Promise((resolve,reject)=>{const fr=new FileReader();fr.onerror=()=>reject(new Error('Lecture fichier échouée'));fr.onload=()=>resolve(fr.result);fr.readAsText(file,'utf-8');});}
export async function extractTextDocx(file){const mammoth=await loadMammoth();const arrayBuffer=await file.arrayBuffer();const res=await mammoth.extractRawText({arrayBuffer});return res.value||'';}
export async function extractTextPdf(file){const {pdfjsLib}=await loadPdfJs();const data=await file.arrayBuffer();const loadingTask=pdfjsLib.getDocument({data});const pdf=await loadingTask.promise;let out=[];for(let i=1;i<=pdf.numPages;i++){const page=await pdf.getPage(i);const content=await page.getTextContent();const strings=content.items.map(it=>it.str||'');out.push(strings.join(' '));}return out.join('\n\n').replace(/\s{2,}/g,' ');}
