import { syntaxTree } from '@codemirror/language';
import { getDiagnostic } from './linter-builder';
import { PrimitiveManager } from '../primitives/primitives';
import { checkValidIdentifier, getCheckContext } from '../utils/check-identifier';
import { addGlobalsAction } from '../utils/actions';
import { getCodeName } from '../utils/code';
let primitives = PrimitiveManager;
// ExtensionLinter: Checks if extension primitives are used without declaring
// the extension, or invalid extensions are declared
export const ExtensionLinter = (view, preprocessContext, lintContext) => {
    const diagnostics = [];
    let foundExtension = false;
    let extension_index = 0;
    let extension_node = null;
    const context = getCheckContext(view, lintContext, preprocessContext);
    syntaxTree(view.state)
        .cursor()
        .iterate((noderef) => {
        var _a, _b, _c, _d;
        if (noderef.name == 'Extensions') {
            noderef.node.getChildren('Identifier').map((child) => {
                let name = getCodeName(view.state, child);
                if (primitives.GetExtensions().indexOf(name) == -1)
                    diagnostics.push(getDiagnostic(view, child, 'Unsupported extension _', 'warning'));
            });
            noderef.node.getChildren('CloseBracket').map((child) => {
                extension_index = child.from;
                foundExtension = true;
                extension_node = noderef.node;
            });
        }
        else if ((noderef.name.includes('Args') ||
            (noderef.name.includes('Unsupported') &&
                ((_a = noderef.node.parent) === null || _a === void 0 ? void 0 : _a.name) != 'VariableName' &&
                ((_b = noderef.node.parent) === null || _b === void 0 ? void 0 : _b.name) != 'Arguments' &&
                ((_c = noderef.node.parent) === null || _c === void 0 ? void 0 : _c.name) != 'AnonArguments' &&
                ((_d = noderef.node.parent) === null || _d === void 0 ? void 0 : _d.name) != 'ProcedureName' &&
                !checkValidIdentifier(noderef.node, getCodeName(view.state, noderef), context))) &&
            !noderef.name.includes('Special')) {
            const value = getCodeName(view.state, noderef);
            let vals = value.split(':');
            if (vals.length <= 1 || lintContext.Extensions.has(vals[0]))
                return;
            diagnostics.push(addGlobalsAction(getDiagnostic(view, noderef, !noderef.name.includes('Unsupported') ? 'Missing extension _' : 'Unsupported missing extension _', 'error', vals[0]), 'Extensions', [vals[0]]));
        }
    });
    return diagnostics;
};
//# sourceMappingURL=extension-linter.js.map