"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const EsAst = __importStar(require("@babel/parser"));
class ESParser {
    constructor() {
        this.parsers = Object.create(null);
        this.register();
    }
    parserExpression(expression) {
        const ast = EsAst.parseExpression(expression);
        return this.parserNode(ast);
    }
    convertExpession(expression, converts) {
        const placeholder = (conv) => {
            return `^^CUBE_PLUS_FALCON_${conv.start}_${conv.end}^^`;
        };
        const matched = (index) => {
            let conv = undefined;
            if (converts) {
                converts.every(ele => {
                    if (ele.start !== null && ele.end !== null && (index >= ele.start && index < ele.end)) {
                        conv = ele;
                        return false;
                    }
                    return true;
                });
            }
            return conv ? placeholder(conv) : undefined;
        };
        let result = '';
        const list = [...expression];
        for (let index = 0; index < list.length; index++) {
            const char = list[index];
            const md = matched(index);
            if (md) {
                if (result.indexOf(md) === -1) {
                    result = `${result}${md}`;
                }
            }
            else {
                result = `${result}${char}`;
            }
        }
        if (converts) {
            converts.forEach(ele => {
                const md = placeholder(ele);
                result = result.split(md).join(ele.value);
            });
        }
        return result;
    }
    register() {
        this.parsers['CallExpression'] = this.falcon4CallExpression;
        this.parsers['Identifier'] = this.falcon4Identifier;
        this.parsers['ArrayExpression'] = this.falcon4ArrayExpression;
        this.parsers['ObjectExpression'] = this.falcon4ObjectExpression;
        this.parsers['BinaryExpression'] = this.falcon4BinaryExpression;
        this.parsers['SequenceExpression'] = this.falcon4SequenceExpression;
        this.parsers['ObjectProperty'] = this.falcon4ObjectProperty;
        this.parsers['ConditionalExpression'] = this.falcon4ConditionalExpression;
        this.parsers['LogicalExpression'] = this.falcon4LogicalExpression;
        this.parsers['MemberExpression'] = this.falcon4MemberExpression;
        this.parsers['UnaryExpression'] = this.falcon4UnaryExpression;
        this.parsers['NumericLiteral'] = this.falcon4NumericLiteral;
    }
    find(node) {
        return this.parsers[node.type];
    }
    parserNode(node) {
        const func = this.find(node);
        if (func) {
            return func(node, this);
        }
        return undefined;
    }
    //  Falcon Parser Function
    falcon4CallExpression(node, that) {
        let convs = new Array();
        const callee = that.parserNode(node.callee);
        if (callee) {
            convs = [...convs, ...callee];
        }
        node.arguments.forEach(ele => {
            const argument = that.parserNode(ele);
            if (argument) {
                convs = [...convs, ...argument];
            }
        });
        return convs;
    }
    falcon4Identifier(node, that) {
        return [{
                start: node.start,
                end: node.end,
                value: `this.${node.name}`
            }];
    }
    falcon4LogicalExpression(node, that) {
        let convs = new Array();
        const left = that.parserNode(node.left);
        if (left) {
            convs = [...convs, ...left];
        }
        const right = that.parserNode(node.right);
        if (right) {
            convs = [...convs, ...right];
        }
        return convs;
    }
    falcon4ConditionalExpression(node, that) {
        let convs = new Array();
        const test = that.parserNode(node.test);
        if (test) {
            convs = [...convs, ...test];
        }
        const consequent = that.parserNode(node.consequent);
        if (consequent) {
            convs = [...convs, ...consequent];
        }
        const alternate = that.parserNode(node.alternate);
        if (alternate) {
            convs = [...convs, ...alternate];
        }
        return convs;
    }
    falcon4UnaryExpression(node, that) {
        return node.argument ? that.parserNode(node.argument) : undefined;
    }
    falcon4ArrayExpression(node, that) {
        let convs = new Array();
        node.elements.forEach(ele => {
            if (ele) {
                const item = that.parserNode(ele);
                if (item) {
                    convs = [...convs, ...item];
                }
            }
        });
        return convs;
    }
    falcon4MemberExpression(node, that) {
        let convs = new Array();
        const object = that.parserNode(node.object);
        if (object) {
            convs = [...convs, ...object];
        }
        //  适配 param1.param2.param3
        if (node.property.type !== 'Identifier') {
            const property = that.parserNode(node.property);
            if (property) {
                convs = [...convs, ...property];
            }
        }
        return convs;
    }
    falcon4NumericLiteral(node, that) {
        return [{
                start: node.start,
                end: node.end,
                value: `${node.value}`
            }];
    }
    falcon4ObjectExpression(node, that) {
        let convs = new Array();
        node.properties.forEach(ele => {
            const prop = that.parserNode(ele);
            if (prop) {
                convs = [...convs, ...prop];
            }
        });
        return convs;
    }
    falcon4ObjectProperty(node, that) {
        return that.parserNode(node.value);
    }
    falcon4BinaryExpression(node, that) {
        let convs = new Array();
        const left = that.parserNode(node.left);
        if (left) {
            convs = [...convs, ...left];
        }
        const right = that.parserNode(node.right);
        if (right) {
            convs = [...convs, ...right];
        }
        return convs;
    }
    falcon4SequenceExpression(node, that) {
        let convs = new Array();
        node.expressions.forEach(ele => {
            const expr = that.parserNode(ele);
            if (expr) {
                convs = [...convs, ...expr];
            }
        });
        return convs;
    }
}
exports.default = ESParser;
//# sourceMappingURL=esparser.js.map