import { syntaxTree } from '@codemirror/language';
import { Log } from '../../utils/debug-utils';
import { getDiagnostic } from './linter-builder';
import { reserved } from '../keywords';
import { checkUndefinedBreed, getCheckContext, checkUnrecognizedWithSuggestions } from '../utils/check-identifier';
import { getCodeName } from '../utils/code';
import { stateExtension } from 'src/codemirror/extension-state-netlogo';
import { ParseMode } from 'src/editor-config';
// UnrecognizedLinter: Checks for anything that can't be parsed by the grammar
export const UnrecognizedLinter = (view, preprocessContext, lintContext) => {
    const diagnostics = [];
    const context = getCheckContext(view, lintContext, preprocessContext);
    syntaxTree(view.state)
        .cursor()
        .iterate((node) => {
        var _a, _b, _c, _d, _e;
        if ((node.name == '⚠' || node.name == 'Error' || node.name == 'Misplaced' || node.name == 'WorseParentheticals') &&
            node.to != node.from) {
            let curr = node.node;
            let parents = [];
            while (curr.parent) {
                parents.push(curr.parent.name);
                curr = curr.parent;
            }
            const value = getCodeName(view.state, node);
            Log(value, node.name, parents);
            if (node.name == 'Misplaced') {
                let sum = 0;
                let types = ['Extensions', 'Globals', 'Breed', 'BreedsOwn', 'Procedure'];
                types.map((name) => {
                    var _a, _b;
                    sum += (_b = (_a = node.node.parent) === null || _a === void 0 ? void 0 : _a.getChildren(name).length) !== null && _b !== void 0 ? _b : 0;
                });
                if (sum == 0 && view.state.field(stateExtension).Mode != ParseMode.Normal) {
                    return;
                }
            }
            if (((_a = node.node.parent) === null || _a === void 0 ? void 0 : _a.name) == 'Arguments') {
                // Arguments should not be reserved words or command/reporter names
                let child = node.node.firstChild;
                if (reserved.includes(value) ||
                    (child && (child.name.startsWith('Command') || child.name.startsWith('Reporter')))) {
                    diagnostics.push(getDiagnostic(view, node, 'Argument is reserved _'));
                }
                else {
                    diagnostics.push(getDiagnostic(view, node, 'Argument is invalid _'));
                }
            }
            else if (((_b = node.node.parent) === null || _b === void 0 ? void 0 : _b.name) == 'SetVariable') {
                if (((_c = node.node.prevSibling) === null || _c === void 0 ? void 0 : _c.name) == 'Set') {
                    diagnostics.push(getDiagnostic(view, node.node, 'Term _ reserved', 'error', value, 'Local variable'));
                }
                else {
                    diagnostics.push(getDiagnostic(view, node, 'Unrecognized statement _'));
                }
            }
            else if (!['[', ']', ')', '(', '"'].includes(value) &&
                !checkUndefinedBreed(diagnostics, context.preprocessState, view, node.node)) {
                // Check if a suggestion exists
                if (checkUnrecognizedWithSuggestions(diagnostics, view, node))
                    return;
                // Anything else could be an unrecognized statement
                if (((_d = node.node.parent) === null || _d === void 0 ? void 0 : _d.name) == 'Normal') {
                    diagnostics.push(getDiagnostic(view, node, 'Unrecognized global statement _'));
                }
                else {
                    diagnostics.push(getDiagnostic(view, node, 'Unrecognized statement _'));
                }
            }
            else if (['[', ']', ')', '(', '"'].includes(value) && ((_e = node.node.parent) === null || _e === void 0 ? void 0 : _e.name) == 'Breed') {
                diagnostics.push(getDiagnostic(view, node.node.parent, 'Missing breed names _'));
            }
        }
    });
    return diagnostics;
};
//# sourceMappingURL=unrecognized-linter.js.map