using System.IO.Compression;
using System.Xml.Linq;

namespace KiMailOrderAgent.Attachments;

public class OdfTextExtractor
{
    public string Extract(byte[] content)
    {
        using var stream = new MemoryStream(content);
        using var archive = new ZipArchive(stream, ZipArchiveMode.Read);

        var entry = archive.GetEntry("content.xml");

        if (entry == null)
            return "";

        using var entryStream = entry.Open();
        var doc = XDocument.Load(entryStream);

        return string.Join(" ", doc.DescendantNodes().OfType<XText>().Select(x => x.Value));
    }
}
