import "../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/parser/parser.js";
import { Lexer as l } from "../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/scan/lexer_public.js";
import "../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/scan/tokens_public.js";
import "../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/grammar/lookahead.js";
import "../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/errors_public.js";
import "../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/exceptions_public.js";
import { isTerminalRule as a, isParserRule as c, isKeyword as d } from "../languages/generated/ast.js";
import { streamAllContents as f } from "../utils/ast-utils.js";
import { getAllReachableRules as p, terminalRegex as m } from "../utils/grammar-utils.js";
import { isWhitespace as u, getCaseInsensitivePattern as h, partialMatches as T } from "../utils/regexp-utils.js";
import { stream as g } from "../utils/stream.js";
class N {
  constructor() {
    this.diagnostics = [];
  }
  buildTokens(e, r) {
    const i = g(p(e, !1)), t = this.buildTerminalTokens(i), n = this.buildKeywordTokens(i, t, r);
    return t.forEach((o) => {
      const s = o.PATTERN;
      typeof s == "object" && s && "test" in s && u(s) ? n.unshift(o) : n.push(o);
    }), n;
  }
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  flushLexingReport(e) {
    return { diagnostics: this.popDiagnostics() };
  }
  popDiagnostics() {
    const e = [...this.diagnostics];
    return this.diagnostics = [], e;
  }
  buildTerminalTokens(e) {
    return e.filter(a).filter((r) => !r.fragment).map((r) => this.buildTerminalToken(r)).toArray();
  }
  buildTerminalToken(e) {
    const r = m(e), i = this.requiresCustomPattern(r) ? this.regexPatternFunction(r) : r, t = {
      name: e.name,
      PATTERN: i
    };
    return typeof i == "function" && (t.LINE_BREAKS = !0), e.hidden && (t.GROUP = u(r) ? l.SKIPPED : "hidden"), t;
  }
  requiresCustomPattern(e) {
    return e.flags.includes("u") || e.flags.includes("s") ? !0 : !!(e.source.includes("?<=") || e.source.includes("?<!"));
  }
  regexPatternFunction(e) {
    const r = new RegExp(e, e.flags + "y");
    return (i, t) => (r.lastIndex = t, r.exec(i));
  }
  buildKeywordTokens(e, r, i) {
    return e.filter(c).flatMap((t) => f(t).filter(d)).distinct((t) => t.value).toArray().sort((t, n) => n.value.length - t.value.length).map((t) => this.buildKeywordToken(t, r, !!(i != null && i.caseInsensitive)));
  }
  buildKeywordToken(e, r, i) {
    const t = this.buildKeywordPattern(e, i), n = {
      name: e.value,
      PATTERN: t,
      LONGER_ALT: this.findLongerAlt(e, r)
    };
    return typeof t == "function" && (n.LINE_BREAKS = !0), n;
  }
  buildKeywordPattern(e, r) {
    return r ? new RegExp(h(e.value)) : e.value;
  }
  findLongerAlt(e, r) {
    return r.reduce((i, t) => {
      const n = t == null ? void 0 : t.PATTERN;
      return n != null && n.source && T("^" + n.source + "$", e.value) && i.push(t), i;
    }, []);
  }
}
export {
  N as DefaultTokenBuilder
};
