"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const brackets = require("./bracketPairsTree/brackets.js");
const length = require("./bracketPairsTree/length.js");
const parser = require("./bracketPairsTree/parser.js");
const smallImmutableSet = require("./bracketPairsTree/smallImmutableSet.js");
const tokenizer = require("./bracketPairsTree/tokenizer.js");
function fixBracketsInLine(tokens, languageConfigurationService) {
  const denseKeyProvider = new smallImmutableSet.DenseKeyProvider();
  const bracketTokens = new brackets.LanguageAgnosticBracketTokens(denseKeyProvider, (languageId) => languageConfigurationService.getLanguageConfiguration(languageId));
  const tokenizer$1 = new tokenizer.TextBufferTokenizer(new StaticTokenizerSource([tokens]), bracketTokens);
  const node = parser.parseDocument(tokenizer$1, [], void 0, true);
  let str = "";
  const line = tokens.getLineContent();
  function processNode(node2, offset) {
    if (node2.kind === 2) {
      processNode(node2.openingBracket, offset);
      offset = length.lengthAdd(offset, node2.openingBracket.length);
      if (node2.child) {
        processNode(node2.child, offset);
        offset = length.lengthAdd(offset, node2.child.length);
      }
      if (node2.closingBracket) {
        processNode(node2.closingBracket, offset);
        offset = length.lengthAdd(offset, node2.closingBracket.length);
      } else {
        const singleLangBracketTokens = bracketTokens.getSingleLanguageBracketTokens(node2.openingBracket.languageId);
        const closingTokenText = singleLangBracketTokens.findClosingTokenText(node2.openingBracket.bracketIds);
        str += closingTokenText;
      }
    } else if (node2.kind === 3)
      ;
    else if (node2.kind === 0 || node2.kind === 1) {
      str += line.substring(length.lengthGetColumnCountIfZeroLineCount(offset), length.lengthGetColumnCountIfZeroLineCount(length.lengthAdd(offset, node2.length)));
    } else if (node2.kind === 4) {
      for (const child of node2.children) {
        processNode(child, offset);
        offset = length.lengthAdd(offset, child.length);
      }
    }
  }
  processNode(node, length.lengthZero);
  return str;
}
class StaticTokenizerSource {
  constructor(lines) {
    this.lines = lines;
  }
  getLineCount() {
    return this.lines.length;
  }
  getLineLength(lineNumber) {
    return this.lines[lineNumber - 1].getLineContent().length;
  }
  getLineTokens(lineNumber) {
    return this.lines[lineNumber - 1];
  }
}
exports.fixBracketsInLine = fixBracketsInLine;
