const ESC = "\x1B";
const SGR_EVENT_PREFIX = `${ESC}[<`;
const X11_EVENT_PREFIX = `${ESC}[M`;
const FOCUS_IN = `${ESC}[I`;
const FOCUS_OUT = `${ESC}[O`;
const ESC_CHAR = String.fromCharCode(27);
const SGR_MOUSE_REGEX = new RegExp(`^${ESC_CHAR}\\[<(\\d+);(\\d+);(\\d+)([mM])`);
const X11_MOUSE_REGEX = new RegExp(`^${ESC_CHAR}\\[M([\\s\\S]{3})`);
function couldBeSGRMouseSequence(buffer) {
  if (buffer.length === 0) return true;
  if (SGR_EVENT_PREFIX.startsWith(buffer)) return true;
  if (buffer.startsWith(SGR_EVENT_PREFIX)) return true;
  return false;
}
function couldBeMouseSequence(buffer) {
  if (buffer.length === 0) return true;
  if (SGR_EVENT_PREFIX.startsWith(buffer) || buffer.startsWith(SGR_EVENT_PREFIX)) return true;
  if (X11_EVENT_PREFIX.startsWith(buffer) || buffer.startsWith(X11_EVENT_PREFIX)) return true;
  return false;
}
function getMouseSequenceLength(buffer) {
  const sgrMatch = buffer.match(SGR_MOUSE_REGEX);
  if (sgrMatch) return sgrMatch[0].length;
  const x11Match = buffer.match(X11_MOUSE_REGEX);
  if (x11Match) return x11Match[0].length;
  return 0;
}
export {
  ESC,
  FOCUS_IN,
  FOCUS_OUT,
  SGR_EVENT_PREFIX,
  SGR_MOUSE_REGEX,
  X11_EVENT_PREFIX,
  X11_MOUSE_REGEX,
  couldBeMouseSequence,
  couldBeSGRMouseSequence,
  getMouseSequenceLength
};
