using Elwig.Documents;
using NReco.PdfRenderer;
using System.Text.RegularExpressions;

namespace Tests.UnitTests.DocumentTests {
    public static class Utils {

        private static readonly string FileName = Path.Combine(Path.GetTempPath(), "test_document.pdf");

        public static async Task<string> GeneratePdfText(Document doc, bool preserveLayout = false) {
            await doc.Generate();
            try {
                doc.SaveTo(FileName);
                var conv = new PdfToTextConverter { CustomArgs = preserveLayout ? "-layout " : "-raw " };
                return conv.GenerateText(FileName);
            } finally {
                File.Delete(FileName);
            }
        }

        public static string[][] ExtractTable(string text) {
            return text.Split('\n')
                .Select(row => Regex.Split(row, @"\s{2,}").Select(c => c.Trim()).Where(c => c.Length > 0).ToArray())
                .Where(row => row.Length >= 3)
                .Skip(1)
                .ToArray();
        }
    }
}
