const normalize = value => value.normalize('NFC').replace(/\s+/g, ' ').trim(); /** Preserve useful draft text, while making missing requested sections explicit. */ export function qualifyOfferDraft(result, requested) { if (!Array.isArray(result?.bolumler) || !result.bolumler.length) throw new Error('Model bölüm üretmedi; tekrar deneyin.'); const byTitle = new Map(); for (const row of result.bolumler) { if (typeof row?.baslik !== 'string' || typeof row?.icerik !== 'string' || !row.baslik.trim()) throw new Error('Model geçersiz bir taslak bölümü döndürdü. Yeniden deneyin.'); const key = normalize(row.baslik); if (byTitle.has(key)) throw new Error('Model aynı taslak bölümünü birden fazla kez döndürdü. Yeniden deneyin.'); byTitle.set(key, {baslik:row.baslik.trim(), icerik:row.icerik.trim()}); } const missingSections = []; const bolumler = requested.map(baslik => { const key = normalize(baslik), found = byTitle.get(key); byTitle.delete(key); if (found?.icerik) return {...found, baslik}; missingSections.push(baslik); return {baslik, icerik:`[DOLDURUN: ${baslik} bölümü model tarafından tamamlanmadı. Kaynak belgelerle hazırlayın.]`}; }); // Keep unexpected useful content; it cannot substitute for a missing section. const extraSections = [...byTitle.values()].filter(row => row.icerik); return {bolumler:[...bolumler, ...extraSections], draftReview:{ status:missingSections.length || extraSections.length ? 'partial' : 'draft', requestedSections:requested, missingSections, extraSections:extraSections.map(row=>row.baslik), note:'Bu çıktı düzenlenebilir bir taslaktır. Bölümlerin bulunması, bilgilerin doğru veya resmi teklif dosyasının hazır olduğunu göstermez. Kaynak, firma bilgileri, fiyatlar ve zorunlu belgeler ayrıca kontrol edilmelidir.', }}; }