"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const acorn_1 = require("acorn");
const acorn_walk_1 = require("acorn-walk");
function inspectDefine(e, dependencies) {
    const id = e.callee;
    if (!(id && id.type === "Identifier" && id.name === "define")) {
        return;
    }
    const a = e.arguments[0];
    if (!(a && a.type === "ArrayExpression")) {
        return;
    }
    const [req, exp] = a.elements;
    if (!(req && req.type === "Literal" && req.value === "require")) {
        return;
    }
    if (!(exp && exp.type === "Literal" && exp.value === "exports")) {
        return;
    }
    dependencies.push(...a.elements
        .filter((el, i) => i > 1)
        .map((el) => el.type === "Literal" ? el.value.toString() : undefined));
}
function inspectRegister(e, dependencies) {
    const id = e.callee;
    if (!(id && id.type === "MemberExpression"
        && id.property?.name === "register"
        && id.object?.name === "System")) {
        return;
    }
    const a = e.arguments[0];
    if (!(a && a.type === "ArrayExpression")) {
        return;
    }
    dependencies.push(...a.elements
        .map((el) => el.type === "Literal" ? el.value.toString() : undefined));
}
function inspectDynamicImport(e, dependencies) {
    const id = e.callee;
    if (!(id && id.type === "MemberExpression"
        && id.property?.name === "import"
        && id.object?.name === "_context")) {
        return;
    }
    const a = e.arguments[0];
    if (a?.type === "Literal") {
        dependencies.push(a.value.toString());
    }
}
class DefineVisitor {
    static parse(tree) {
        if (typeof tree === "string") {
            tree = (0, acorn_1.parse)(tree, { ecmaVersion: 2019 });
        }
        const result = [];
        (0, acorn_walk_1.simple)(tree, {
            CallExpression(e) {
                inspectDefine(e, result);
                inspectRegister(e, result);
                inspectDynamicImport(e, result);
            }
        });
        return result;
    }
}
exports.default = DefineVisitor;
//# sourceMappingURL=DefineVisitor.js.map