/*
 * SDK Pullenti Lingvo, 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#.NET 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 RefOutArgWrapper = require("./../../../unisharp/RefOutArgWrapper");
const StringBuilder = require("./../../../unisharp/StringBuilder");

const ResourceHelper2 = require("./../../core/internal/ResourceHelper2");

class PhoneHelper {
    
    static initialize() {
        if (PhoneHelper.m_PhoneRoot !== null) 
            return;
        PhoneHelper.m_PhoneRoot = new PhoneHelper.PhoneNode();
        PhoneHelper.m_AllCountryCodes = new Hashtable();
        let str = ResourceHelper2.getString("CountryPhoneCodes.txt");
        if (str === null) 
            throw new Error(("Can't file resource file " + "CountryPhoneCodes.txt" + " in Organization analyzer"));
        for (const line0 of Utils.splitString(str, '\n', false)) {
            let line = line0.trim();
            if (Utils.isNullOrEmpty(line)) 
                continue;
            if (line.length < 2) 
                continue;
            let country = line.substring(0, 0 + 2);
            let cod = line.substring(2).trim();
            if (cod.length < 1) 
                continue;
            if (!PhoneHelper.m_AllCountryCodes.containsKey(country)) 
                PhoneHelper.m_AllCountryCodes.put(country, cod);
            let tn = PhoneHelper.m_PhoneRoot;
            for (let i = 0; i < cod.length; i++) {
                let dig = cod[i];
                let nn = null;
                let wrapnn3484 = new RefOutArgWrapper();
                let inoutres3485 = tn.children.tryGetValue(dig, wrapnn3484);
                nn = wrapnn3484.value;
                if (!inoutres3485) {
                    nn = new PhoneHelper.PhoneNode();
                    nn.pref = cod.substring(0, 0 + i + 1);
                    tn.children.put(dig, nn);
                }
                tn = nn;
            }
            if (tn.countries === null) 
                tn.countries = new Array();
            tn.countries.push(country);
        }
    }
    
    static getAllCountryCodes() {
        return PhoneHelper.m_AllCountryCodes;
    }
    
    static getCountryPrefix(fullNumber) {
        if (fullNumber === null) 
            return null;
        let nod = PhoneHelper.m_PhoneRoot;
        let maxInd = -1;
        for (let i = 0; i < fullNumber.length; i++) {
            let dig = fullNumber[i];
            let nn = null;
            let wrapnn3486 = new RefOutArgWrapper();
            let inoutres3487 = nod.children.tryGetValue(dig, wrapnn3486);
            nn = wrapnn3486.value;
            if (!inoutres3487) 
                break;
            if (nn.countries !== null && nn.countries.length > 0) 
                maxInd = i;
            nod = nn;
        }
        if (maxInd < 0) 
            return null;
        else 
            return fullNumber.substring(0, 0 + maxInd + 1);
    }
    
    static static_constructor() {
        PhoneHelper.m_AllCountryCodes = null;
        PhoneHelper.m_PhoneRoot = null;
    }
}


PhoneHelper.PhoneNode = class  {
    
    constructor() {
        this.pref = null;
        this.children = new Hashtable();
        this.countries = null;
    }
    
    toString() {
        if (this.countries === null) 
            return this.pref;
        let res = new StringBuilder(this.pref);
        for (const c of this.countries) {
            res.append(" ").append(c);
        }
        return res.toString();
    }
}


PhoneHelper.static_constructor();

module.exports = PhoneHelper