import { matchBrackets, syntaxTree } from '@codemirror/language';
import { getDiagnostic } from './linter-builder';
// BracketLinter: Checks if all brackets/parentheses have matches
export const BracketLinter = (view, preprocessContext, lintContext) => {
    const diagnostics = [];
    syntaxTree(view.state)
        .cursor()
        .iterate((node) => {
        // Match the bracket/paren
        // if (node.name=='Value'){
        //   console.log(node.node.firstChild)
        // }
        if (['OpenParen', 'CloseParen', 'OpenBracket', 'CloseBracket'].includes(node.name)) {
            let parents = [];
            let curr = node.node;
            while (curr.parent) {
                parents.push(curr.parent.name);
                curr = curr.parent;
            }
            // console.log(getCodeName(view.state, node),parents)
            if (['OpenBracket', 'OpenParen'].includes(node.name)) {
                let match = matchBrackets(view.state, node.from, 1);
                if (match && match.matched)
                    return;
            }
            let current = '', expected = '';
            // [ need ]
            if (test_function(node, 'OpenBracket', 'CloseBracket')) {
                // node.name == 'OpenBracket' && node.node.parent?.getChildren('CloseBracket').length != 1) {
                // if (node.node.parent?.name=='Breed'){
                //   diagnostics.push(getDiagnostic(view, node, 'Missing breed names _', 'error','breed'))
                //   return false
                // } else{
                current = '[';
                expected = ']';
                // }
            }
            // ] need [
            else if (test_function(node, 'CloseBracket', 'OpenBracket')) {
                // if (node.name == 'CloseBracket' && node.node.parent?.getChildren('OpenBracket').length != 1) {
                current = ']';
                expected = '[';
            }
            // ( need )
            else if (test_function(node, 'OpenParen', 'CloseParen')) {
                //  if (node.name == 'OpenParen' && node.node.parent?.getChildren('CloseParen').length != 1) {
                current = '(';
                expected = ')';
            }
            // ) need (
            else if (test_function(node, 'CloseParen', 'OpenParen')) {
                // if (node.name == 'CloseParen' && node.node.parent?.getChildren('OpenParen').length != 1) {
                current = ')';
                expected = '(';
            }
            // Push the diagnostic
            // console.log(current)
            if (current != '')
                diagnostics.push(getDiagnostic(view, node, 'Unmatched item _', 'error', current, expected));
        }
    });
    return diagnostics;
};
const test_function = function (node, name, match_name) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    return (node.name == name &&
        ((((_a = node.node.parent) === null || _a === void 0 ? void 0 : _a.name) != '⚠' &&
            ((_b = node.node.parent) === null || _b === void 0 ? void 0 : _b.getChildren(match_name).length) != 1 &&
            ((_d = (_c = node.node.parent) === null || _c === void 0 ? void 0 : _c.getChild('⚠')) === null || _d === void 0 ? void 0 : _d.getChildren(match_name).length) != 1) ||
            (((_e = node.node.parent) === null || _e === void 0 ? void 0 : _e.name) == '⚠' &&
                ((_f = node.node.parent) === null || _f === void 0 ? void 0 : _f.getChildren(match_name).length) != 1 &&
                ((_h = (_g = node.node.parent) === null || _g === void 0 ? void 0 : _g.parent) === null || _h === void 0 ? void 0 : _h.getChildren(match_name).length) != 1)));
};
//# sourceMappingURL=bracket-linter.js.map