(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var viewModelParseWatchCache = {};
    function parsePath(f, parseThis) {
        var str = f.toString().trim();
        str = str.split("\n").filter(function (s) { return !/^\/\//.test(s.trim()); }).join("\n");
        var key = str;
        var px1 = viewModelParseWatchCache[key];
        if (px1) {
            return px1;
        }
        if (str.endsWith("}")) {
            str = str.substr(0, str.length - 1);
        }
        if (str.startsWith("function (")) {
            str = str.substr("function (".length);
        }
        if (str.startsWith("function(")) {
            str = str.substr("function(".length);
        }
        str = str.trim();
        var index = str.indexOf(")");
        var isThis = index === 0 || parseThis;
        var p = isThis ? "\\_this|this" : str.substr(0, index);
        str = str.substr(index + 1);
        var regExp = "(?:(" + p + ")(?:(\\.[a-zA-Z_][a-zA-Z_0-9]*)+)\\s?(?:(\\(|\\=\\=\\=|\\=\\=|\\=)?))";
        var re = new RegExp(regExp, "gi");
        var path = [];
        var ms = str.replace(re, function (m) {
            // console.log(`m: ${m}`);
            var px = m;
            if (px.startsWith("this.")) {
                px = px.substr(5);
            }
            else if (px.startsWith("_this.")) {
                px = px.substr(6);
            }
            else {
                px = px.substr(p.length + 1);
            }
            // console.log(px);
            if (!path.find(function (y) { return y === px; })) {
                path.push(px);
            }
            path = path.filter(function (f1) { return f1.endsWith("==") || !(f1.endsWith("(") || f1.endsWith("=")); });
            path = path.map(function (px2) { return px2.endsWith("===") ? px2.substr(0, px2.length - 3) :
                (px2.endsWith("==") ? px2.substr(0, px2.length - 2) : px2); })
                .map(function (px2) { return px2.trim(); });
            return m;
        });
        path = path.sort(function (a, b) { return b.localeCompare(a); });
        var dups = path;
        path = [];
        var _loop_1 = function (iterator) {
            if (path.find(function (px2) { return px2 === iterator; })) {
                return "continue";
            }
            path.push(iterator);
        };
        for (var _i = 0, dups_1 = dups; _i < dups_1.length; _i++) {
            var iterator = dups_1[_i];
            _loop_1(iterator);
        }
        var rp = [];
        var _loop_2 = function (rpitem) {
            if (rp.find(function (x) { return x.startsWith(rpitem); })) {
                return "continue";
            }
            rp.push(rpitem);
        };
        for (var _a = 0, path_1 = path; _a < path_1.length; _a++) {
            var rpitem = path_1[_a];
            _loop_2(rpitem);
        }
        // console.log(`Watching: ${path.join(", ")}`);
        var pl = path.map(function (p1) { return p1.split("."); });
        viewModelParseWatchCache[key] = pl;
        return pl;
    }
    exports.parsePath = parsePath;
});
//# sourceMappingURL=ExpressionParser.js.map