import { isCompositeCstNode as s, isRootCstNode as d, isLeafCstNode as l } from "../syntax-tree.js";
import { TreeStreamImpl as a } from "./stream.js";
function O(e) {
  return new a(e, (t) => s(t) ? t.content : [], { includeRoot: !0 });
}
function I(e, t) {
  for (; e.container; )
    if (e = e.container, e === t)
      return !0;
  return !1;
}
function m(e) {
  return {
    start: {
      character: e.startColumn - 1,
      line: e.startLine - 1
    },
    end: {
      character: e.endColumn,
      // endColumn uses the correct index
      line: e.endLine - 1
    }
  };
}
function x(e) {
  if (!e)
    return;
  const { offset: t, end: n, range: r } = e;
  return {
    range: r,
    offset: t,
    end: n,
    length: n - t
  };
}
var i;
(function(e) {
  e[e.Before = 0] = "Before", e[e.After = 1] = "After", e[e.OverlapFront = 2] = "OverlapFront", e[e.OverlapBack = 3] = "OverlapBack", e[e.Inside = 4] = "Inside", e[e.Outside = 5] = "Outside";
})(i || (i = {}));
function o(e, t) {
  if (e.end.line < t.start.line || e.end.line === t.start.line && e.end.character <= t.start.character)
    return i.Before;
  if (e.start.line > t.end.line || e.start.line === t.end.line && e.start.character >= t.end.character)
    return i.After;
  const n = e.start.line > t.start.line || e.start.line === t.start.line && e.start.character >= t.start.character, r = e.end.line < t.end.line || e.end.line === t.end.line && e.end.character <= t.end.character;
  return n && r ? i.Inside : n ? i.OverlapBack : r ? i.OverlapFront : i.Outside;
}
function B(e, t) {
  return o(e, t) > i.After;
}
const w = /^[\w\p{L}]$/u;
function A(e, t) {
  if (e) {
    const n = h(e, !0);
    if (n && f(n, t))
      return n;
    if (d(e)) {
      const r = e.content.findIndex((u) => !u.hidden);
      for (let u = r - 1; u >= 0; u--) {
        const c = e.content[u];
        if (f(c, t))
          return c;
      }
    }
  }
}
function f(e, t) {
  return l(e) && t.includes(e.tokenType.name);
}
function h(e, t = !0) {
  for (; e.container; ) {
    const n = e.container;
    let r = n.content.indexOf(e);
    for (; r > 0; ) {
      r--;
      const u = n.content[r];
      if (t || !u.hidden)
        return u;
    }
    e = n;
  }
}
export {
  w as DefaultNameRegexp,
  i as RangeComparison,
  o as compareRange,
  A as findCommentNode,
  h as getPreviousNode,
  B as inRange,
  I as isChildNode,
  f as isCommentNode,
  O as streamCst,
  x as toDocumentSegment,
  m as tokenToRange
};
