/*
 * 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.word;

public class MSOfficeHelper {

    public static boolean checkExtension(String fileName) {
        String ext = com.pullenti.unisharp.Utils.getFileExt(fileName).toUpperCase();
        return (com.pullenti.unisharp.Utils.stringsEq(ext, ".DOC") || com.pullenti.unisharp.Utils.stringsEq(ext, ".DOCX") || com.pullenti.unisharp.Utils.stringsEq(ext, ".PPTX")) || com.pullenti.unisharp.Utils.stringsEq(ext, ".XLSX");
    }

    private static com.pullenti.unitext.UnitextDocument _uniFromWord97(CompoundFileSystem cf) throws Exception, java.io.IOException, IllegalArgumentException, UnsupportedOperationException {
        WordDocument _word = new WordDocument();
        com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new47(com.pullenti.unitext.FileFormat.DOC);
        _word.load(cf, doc);
        return doc;
    }

    public static com.pullenti.unitext.UnitextDocument _uniFromWord97Arr(byte[] data) throws Exception, java.io.FileNotFoundException, java.io.IOException, IllegalArgumentException, UnsupportedOperationException {
        if (data.length > 0x300 && data[0] == ((byte)0xD0) && data[1] == ((byte)0xCF)) {
            int i0 = 0x50;
            for (; i0 < data.length; i0++) {
                if (data[i0] != ((byte)0xFF)) 
                    break;
            }
            if ((i0 + 0x200) <= data.length && data[i0 + 1] == ((byte)0xA5) && data[i0] == ((byte)0xDC)) {
                byte[] buf = new byte[data.length - i0];
                for (int i = 0; i < buf.length; i++) {
                    buf[i] = data[i0 + i];
                }
                com.pullenti.unitext.UnitextDocument doc = _uniFromWord6orEarly(buf);
                if (doc != null) {
                    doc.sourceFormat = com.pullenti.unitext.FileFormat.DOC;
                    doc.attrs.put("word", "6");
                }
                return doc;
            }
        }
        try (CompoundFileSystem cf = new CompoundFileSystem(null, data, null)) {
            return _uniFromWord97(cf);
        }
    }

    public static com.pullenti.unitext.UnitextDocument _uniFromWord6orEarly(byte[] buf) {
        int pos0 = java.nio.ByteBuffer.wrap(buf, 0x18, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
        int pos1 = java.nio.ByteBuffer.wrap(buf, 0x1C, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
        if (pos1 <= buf.length && pos0 >= 0x20 && pos1 > pos0) {
        }
        else 
            return null;
        int v0 = 0;
        int v1 = 0;
        for (int i = pos0; i < pos1; i++) {
            if (buf[i] == ((byte)0) || buf[i] == ((byte)4)) {
                if (((((i - pos0)) & 1)) == 0) 
                    v1++;
                else 
                    v0++;
            }
        }
        String str = null;
        if (v0 > (v1 * 3)) 
            str = com.pullenti.util.MiscHelper.decodeStringUnicode(buf, pos0, pos1 - pos0);
        else 
            str = com.pullenti.util.MiscHelper.decodeString1251(buf, pos0, pos1 - pos0);
        com.pullenti.unitext.UnitextDocument ddd = com.pullenti.unitext.UnitextService.createDocumentFromText(str);
        return ddd;
    }

    public static com.pullenti.unitext.UnitextDocument _uniFromWord97File(String fileName) throws Exception, java.io.FileNotFoundException, java.io.IOException, IllegalArgumentException, UnsupportedOperationException {
        try (CompoundFileSystem cf = new CompoundFileSystem(fileName, null, null)) {
            return _uniFromWord97(cf);
        }
    }

    private static String _correctContent(StringBuilder txt) {
        StringBuilder res = new StringBuilder();
        int off = 0;
        int lf = 0;
        StringBuilder offTmp = new StringBuilder();
        for (int j = 0; j < txt.length(); j++) {
            char ch = txt.charAt(j);
            if (ch == ':') {
            }
            if (((int)ch) == 127) 
                ch = ' ';
            if (off > 0) {
                if (((int)ch) == 0x15) {
                    if ((--off) == 0) {
                        String ttt = extractSpecText(offTmp.toString().trim());
                        if (ttt != null) 
                            res.append(ttt);
                        offTmp.setLength(0);
                    }
                    else 
                        offTmp.append(ch);
                }
                else {
                    offTmp.append(ch);
                    if (((int)ch) == 0x13) 
                        off++;
                }
                continue;
            }
            if (((int)ch) == 0x13) {
                if (off == 0) 
                    offTmp.setLength(0);
                off++;
            }
            else if (((int)ch) == 0xD || ch == '\u000B') {
                res.append("\r\n");
                lf++;
            }
            else if (((int)ch) == 0xC) {
                res.append("\r\n\r\n\r\n");
                lf += 3;
            }
            else if (com.pullenti.unisharp.Utils.isWhitespace(ch) || ((int)ch) > 0x20 || ((int)ch) == 7) {
                if (ch == '\n') {
                    if (res.length() == 0 || res.charAt(res.length() - 1) != '\r') 
                        res.append('\r');
                }
                res.append(ch);
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                    lf = 0;
            }
            else if (((int)ch) == 3 || ((int)ch) == 4) 
                break;
            else {
            }
        }
        return res.toString();
    }

    public static String extractSpecText(String txt) {
        txt = com.pullenti.unisharp.Utils.trimStart(txt);
        if (txt.startsWith("HYPERLINK")) {
            int i1 = txt.indexOf("PAGEREF");
            if (i1 > 0) {
                for (int ii = i1 - 1; ii >= 0; ii--) {
                    if (((int)txt.charAt(ii)) == 0x14 || ((int)txt.charAt(ii)) == 0x1) {
                        txt = txt.substring(ii + 1, ii + 1 + i1 - ii - 1).trim();
                        return txt + "\n";
                    }
                }
            }
            int i = txt.lastIndexOf((char)0x14);
            if (i < 0) 
                i = txt.lastIndexOf((char)0x1);
            if (i > 0 && (i < txt.length())) {
                txt = txt.substring(i + 1);
                return txt;
            }
            return null;
        }
        if (txt.startsWith("TOC")) {
            int ii = txt.indexOf("HYPERLINK");
            if (ii > 0) 
                return extractSpecText(txt.substring(ii));
            StringBuilder res = new StringBuilder();
            boolean treg = false;
            for (char ch : txt.toCharArray()) {
                if (((int)ch) == 0x14 || ((int)ch) == 0x15) 
                    treg = true;
                else if (((int)ch) == 0x13) 
                    treg = false;
                else if (treg) {
                    if (((int)ch) == 0xD) 
                        res.append("\r\n");
                    else if (ch == '\u000B' || ((int)ch) == 7) 
                        res.append("  ");
                    else 
                        res.append(ch);
                }
            }
            return res.toString();
        }
        return null;
    }
    public MSOfficeHelper() {
    }
}
