/*
 * 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
 */

const Utils = require("./../../../unisharp/Utils");
const Hashtable = require("./../../../unisharp/Hashtable");
const StringBuilder = require("./../../../unisharp/StringBuilder");

class MhtPart {
    
    constructor(src) {
        this.contentType = null;
        this.contentEncoding = null;
        this.contentLocation = null;
        this.attrs = new Hashtable();
        this.data = null;
        this.stringData = null;
        let i = 0;
        let crlf = 0;
        for (i = 0; i < src.length; i++) {
            if ((src.charCodeAt(i)) === 0xD) {
                crlf++;
                if (((i + 1) < src.length) && (src.charCodeAt(i + 1)) === 0xA) 
                    i++;
            }
            else if ((src.charCodeAt(i)) === 0xA) 
                crlf++;
            else 
                crlf = 0;
            if (crlf >= 2) 
                break;
        }
        if ((crlf < 2) || i >= src.length) 
            return;
        let head = src.substring(0, 0 + i).trim();
        let j = 0;
        while (head.length > 0) {
            if (!head.startsWith("Content-")) 
                break;
            head = head.substring(8);
            j = head.indexOf("Content-");
            let rec = (j < 0 ? head : head.substring(0, 0 + j).trim());
            if (rec.startsWith("Location:")) 
                this.contentLocation = rec.substring(9).trim();
            else if (rec.startsWith("Transfer-Encoding:")) 
                this.contentEncoding = rec.substring("Transfer-Encoding:".length).trim();
            else if (rec.startsWith("Type:")) 
                this.contentType = rec.substring(5).trim();
            if (j < 0) 
                break;
            head = head.substring(j);
        }
        if (Utils.compareStrings(this.contentEncoding, "base64", true) === 0) 
            this.data = Utils.decodeBase64(src.substring(i).trim());
        else if (Utils.compareStrings(this.contentEncoding, "quoted-printable", true) === 0) {
            let tmp = new StringBuilder();
            for (j = i; j < src.length; j++) {
                if (src[j] !== '=') 
                    tmp.append(src[j]);
                else {
                    if (((j + 2) < src.length) && MhtPart._toInt(src[j + 1]) >= 0 && MhtPart._toInt(src[j + 2]) >= 0) {
                        let k = (MhtPart._toInt(src[j + 1]) * 16) + MhtPart._toInt(src[j + 2]);
                        tmp.append(String.fromCharCode(k));
                        j += 2;
                        continue;
                    }
                    for (++j; j < src.length; j++) {
                        if (src[j] !== ' ') 
                            break;
                    }
                    for (; j < src.length; j++) {
                        if ((src.charCodeAt(j)) !== 0xD && (src.charCodeAt(j)) !== 0xA) 
                            break;
                    }
                    j--;
                }
            }
            this.stringData = tmp.toString();
        }
    }
    
    static _toInt(ch) {
        if (ch >= '0' && ch <= '9') 
            return (ch.charCodeAt(0)) - ('0'.charCodeAt(0));
        if (ch >= 'A' && ch <= 'F') 
            return 10 + (((ch.charCodeAt(0)) - ('A'.charCodeAt(0))));
        if (ch >= 'a' && ch <= 'f') 
            return 10 + (((ch.charCodeAt(0)) - ('a'.charCodeAt(0))));
        return -1;
    }
    
    static parseAllStr(all, i0, bound, first) {
        let res = new Array();
        let beg = 0;
        for (let i = i0; i < all.length; i++) {
            let j = 0;
            for (j = 0; j < bound.length; j++) {
                if (bound[j] !== all[i + j]) 
                    break;
            }
            if (j < bound.length) 
                continue;
            if (beg > 0) {
                let pa = new MhtPart(all.substring(beg, beg + i - beg - 2).trim());
                if (pa.contentLocation !== null) 
                    res.push(pa);
                if (first) 
                    break;
            }
            i += bound.length;
            for (; i < all.length; i++) {
                if (!Utils.isWhitespace(all[i])) 
                    break;
            }
            beg = i;
        }
        return res;
    }
    
    static parseAll(fs, bound) {
        return null;
    }
}


module.exports = MhtPart