/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { Base, Number_$type, String_$type, markType } from "./type";
import { DescriptionPath } from "./DescriptionPath";
import { List$1 } from "./List$1";
import { DescriptionPathSegment } from "./DescriptionPathSegment";
import { DescriptionPathIndexer } from "./DescriptionPathIndexer";
import { stringContains, stringReplace } from "./string";
import { stringSplit } from "./stringExtended";
/**
 * @hidden
 */
export let DescriptionPathParser = /*@__PURE__*/ (() => {
    class DescriptionPathParser extends Base {
        parse(a) {
            if (a == null) {
                return null;
            }
            if (stringContains(a, "|")) {
                let b = a.split('|');
                let c = new List$1(DescriptionPath.$, 0);
                for (let e = 0; e < b.length; e++) {
                    let d = b[e];
                    let f = this.parse(d.trim());
                    c.add(f);
                }
                let g = this.b(c);
                return g;
            }
            let h = new List$1(DescriptionPathSegment.$, 0);
            this.d(h, a);
            let i = new DescriptionPath();
            i.rootSegments = [h.toArray()];
            return i;
        }
        d(a, b) {
            if (b == null) {
                return;
            }
            let c = stringSplit(b, ['.', '['], 0);
            for (let d = 0; d < c.length; d++) {
                let e = c[d];
                let f = c.length > 1;
                let g = false;
                let h = new List$1(Number_$type, 0);
                let i = new List$1(String_$type, 0);
                let j = null;
                if (stringContains(e, "]")) {
                    g = true;
                    e = stringReplace(e, "]", "");
                    let k = e.split(',');
                    for (let l = 0; l < k.length; l++) {
                        let m = k[l];
                        if (stringContains(m, "as")) {
                            let n = m.indexOf("as");
                            let o = m.substr(0, n).trim();
                            let p = m.substr(n + 2).trim();
                            if (o.trim() == "*") {
                                h.add(-1);
                                i.add(p);
                            }
                            else {
                                h.add(parseInt(o));
                                i.add(p);
                            }
                            j = p;
                            e = o;
                        }
                        else {
                            h.add(parseInt(m.trim()));
                            i.add(null);
                        }
                    }
                }
                if (g) {
                    if (a.count > 0) {
                        let q = a._inner[a.count - 1];
                        q.indexers = this.a(h, i);
                    }
                    else {
                        a.add(((() => {
                            let $ret = new DescriptionPathSegment();
                            $ret.indexers = this.a(h, i);
                            return $ret;
                        })()));
                    }
                }
                else {
                    a.add(((() => {
                        let $ret = new DescriptionPathSegment();
                        $ret.elementName = e.trim();
                        return $ret;
                    })()));
                }
            }
        }
        a(a, b) {
            let c = new Array(a.count);
            for (let d = 0; d < a.count; d++) {
                let e = new DescriptionPathIndexer();
                if (a._inner[d] == -1) {
                    e.a = true;
                    e.c = b._inner[d];
                }
                c[d] = e;
            }
            return c;
        }
        b(a) {
            let b = new DescriptionPath();
            b.rootSegments = new Array(a.count);
            for (let c = 0; c < a.count; c++) {
                b.rootSegments[c] = a._inner[c].rootSegments[0];
                b.operator = 1;
            }
            return b;
        }
    }
    DescriptionPathParser.$t = markType(DescriptionPathParser, 'DescriptionPathParser');
    return DescriptionPathParser;
})();
