/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.misc;

public class CsvHelper {

    public static char checkDelim(String text) {
        char[] delims = new char[] {',', ';', ' ', '\t', '|'};
        int[] stat = new int[delims.length];
        int[] rows = new int[delims.length];
        int[] columns = new int[delims.length];
        java.util.ArrayList<String> cells = new java.util.ArrayList<String>();
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; (i < delims.length) && (i < 100000); i++) {
            rows[i] = 0;
            int cols = 0;
            for (int j = 0; j < text.length(); ) {
                cells.clear();
                int jj = readRecord(text, j, delims[i], cells, null);
                if (jj < 0) 
                    break;
                if (cells.size() > 1) {
                    if (cols == 0) 
                        cols = cells.size();
                    else if (cols == cells.size()) 
                        stat[i]++;
                    else 
                        stat[i]--;
                    rows[i]++;
                }
                if (jj > j) 
                    j = jj;
                else 
                    break;
            }
            columns[i] = cols;
        }
        char delim = (char)0;
        int bestcou = 0;
        int bestrows = 0;
        for (int i = 0; i < delims.length; i++) {
            if (stat[i] > 0 && (stat[i] + 1) >= ((int)((((double)rows[i]) * 0.96)))) {
                if (bestcou == 0 || columns[i] > bestcou || ((columns[i] == bestcou && rows[i] > ((int)((((double)bestrows) * 1.5)))))) {
                    bestcou = columns[i];
                    delim = delims[i];
                    bestrows = rows[i];
                }
            }
        }
        return delim;
    }

    public static com.pullenti.unitext.UnitextDocument create(String text) {
        char delim = checkDelim(text);
        java.util.ArrayList<String> cells = new java.util.ArrayList<String>();
        StringBuilder tmp = new StringBuilder();
        com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new47(com.pullenti.unitext.FileFormat.CSV);
        com.pullenti.unitext.UnitextTable tab = new com.pullenti.unitext.UnitextTable();
        com.pullenti.unitext.internal.uni.UnitextGen gen = new com.pullenti.unitext.internal.uni.UnitextGen();
        doc.content = tab;
        int row = 0;
        for (int j = 0; j < text.length(); ) {
            cells.clear();
            int jj = readRecord(text, j, delim, cells, tmp);
            if (jj < 0) 
                break;
            if (cells.size() > 0) {
                for (int i = 0; i < cells.size(); i++) {
                    com.pullenti.unitext.UnitextTablecell cel = tab.addCell(row, row, i, i, null, false);
                    String val = cells.get(i);
                    if (com.pullenti.unisharp.Utils.isNullOrEmpty(val)) 
                        continue;
                    gen.clearAll();
                    gen.appendText(val, false);
                    cel.content = gen.finish(true, null);
                }
                row++;
            }
            if (jj > j) 
                j = jj;
            else 
                break;
        }
        int r;
        for (r = 0; r < tab.getRowsCount(); r++) {
            com.pullenti.unitext.UnitextTablecell cel = tab.getCell(r, tab.getColsCount() - 1);
            if (cel != null && cel.content != null) 
                break;
        }
        if (r >= tab.getRowsCount()) 
            tab.removeLastColumn();
        return doc;
    }

    public static int readRecord(String text, int pos, char delim, java.util.ArrayList<String> res, StringBuilder tmp) {
        if (pos >= text.length()) 
            return -1;
        if (tmp != null) 
            tmp.setLength(0);
        boolean isQu = false;
        boolean newCell = false;
        int len = 0;
        int i;
        for (i = pos; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (isQu) {
                if (ch == '"') {
                    if (((i + 1) < text.length()) && text.charAt(i + 1) == '"') {
                        if (tmp != null) 
                            tmp.append('"');
                        i++;
                        len++;
                        continue;
                    }
                    isQu = false;
                    continue;
                }
                if (tmp != null) 
                    tmp.append(ch);
                len++;
                continue;
            }
            if (ch == delim && ((int)delim) != 0) {
                if (tmp != null) {
                    res.add(tmp.toString().trim());
                    tmp.setLength(0);
                }
                else 
                    res.add(null);
                newCell = true;
                len = 0;
                continue;
            }
            if (((int)ch) == 0xD || ((int)ch) == 0xA) {
                i++;
                if (i < text.length()) {
                    ch = text.charAt(i);
                    if (((int)ch) == 0xD || ((int)ch) == 0xA) 
                        i++;
                }
                break;
            }
            if (ch == '"' && len == 0) {
                if (((i + 1) < text.length()) && text.charAt(i + 1) == '"') 
                    i++;
                else 
                    isQu = true;
            }
            else {
                if (tmp != null) 
                    tmp.append(ch);
                len++;
            }
        }
        if (len > 0 || newCell) {
            if (tmp != null) 
                res.add(tmp.toString().trim());
            else 
                res.add(null);
        }
        return i;
    }
    public CsvHelper() {
    }
}
