"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseScriptRanges = parseScriptRanges;
const shared_1 = require("../utils/shared");
const utils_1 = require("./utils");
function parseScriptRanges(ts, ast, hasScriptSetup) {
    let exportDefault;
    const bindings = hasScriptSetup ? (0, utils_1.parseBindingRanges)(ts, ast) : [];
    ts.forEachChild(ast, raw => {
        if (ts.isExportAssignment(raw)) {
            let node = raw;
            while (isAsExpression(node.expression) || ts.isParenthesizedExpression(node.expression)) { // fix https://github.com/vuejs/language-tools/issues/1882
                node = node.expression;
            }
            let obj;
            if (ts.isObjectLiteralExpression(node.expression)) {
                obj = node.expression;
            }
            else if (ts.isCallExpression(node.expression) && node.expression.arguments.length) {
                const arg0 = node.expression.arguments[0];
                if (ts.isObjectLiteralExpression(arg0)) {
                    obj = arg0;
                }
            }
            if (obj) {
                let componentsOptionNode;
                let directivesOptionNode;
                let nameOptionNode;
                let inheritAttrsOption;
                ts.forEachChild(obj, node => {
                    if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) {
                        const name = _getNodeText(node.name);
                        if (name === 'components' && ts.isObjectLiteralExpression(node.initializer)) {
                            componentsOptionNode = node.initializer;
                        }
                        else if (name === 'directives' && ts.isObjectLiteralExpression(node.initializer)) {
                            directivesOptionNode = node.initializer;
                        }
                        else if (name === 'name') {
                            nameOptionNode = node.initializer;
                        }
                        else if (name === 'inheritAttrs') {
                            inheritAttrsOption = _getNodeText(node.initializer);
                        }
                    }
                });
                exportDefault = {
                    ..._getStartEnd(raw),
                    expression: _getStartEnd(node.expression),
                    args: _getStartEnd(obj),
                    argsNode: obj,
                    componentsOption: componentsOptionNode ? _getStartEnd(componentsOptionNode) : undefined,
                    componentsOptionNode,
                    directivesOption: directivesOptionNode ? _getStartEnd(directivesOptionNode) : undefined,
                    nameOption: nameOptionNode ? _getStartEnd(nameOptionNode) : undefined,
                    inheritAttrsOption,
                };
                const comment = (0, utils_1.getClosestMultiLineCommentRange)(ts, raw, [], ast);
                if (comment) {
                    exportDefault.start = comment.start;
                }
            }
        }
    });
    return {
        exportDefault,
        bindings,
    };
    function _getStartEnd(node) {
        return (0, shared_1.getStartEnd)(ts, node, ast);
    }
    function _getNodeText(node) {
        return (0, shared_1.getNodeText)(ts, node, ast);
    }
    // isAsExpression is missing in tsc
    function isAsExpression(node) {
        return node.kind === ts.SyntaxKind.AsExpression;
    }
}
//# sourceMappingURL=scriptRanges.js.map