"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var mouse_exports = {};
__export(mouse_exports, {
  ESC: () => import_input.ESC,
  disableMouseEvents: () => disableMouseEvents,
  enableMouseEvents: () => enableMouseEvents,
  getMouseEventName: () => getMouseEventName,
  isIncompleteMouseSequence: () => isIncompleteMouseSequence,
  parseMouseEvent: () => parseMouseEvent,
  parseSGRMouseEvent: () => parseSGRMouseEvent,
  parseX11MouseEvent: () => parseX11MouseEvent
});
module.exports = __toCommonJS(mouse_exports);
var import_input = require("./input.js");
function enableMouseEvents() {
  process.stdout.write("\x1B[?1002h\x1B[?1006h");
}
function disableMouseEvents() {
  process.stdout.write("\x1B[?1006l\x1B[?1002l");
}
function getMouseEventName(buttonCode, isRelease) {
  const isMove = (buttonCode & 32) !== 0;
  if (buttonCode === 66) {
    return "scroll-left";
  } else if (buttonCode === 67) {
    return "scroll-right";
  }
  if ((buttonCode & 64) === 64) {
    if ((buttonCode & 1) === 0) {
      return "scroll-up";
    } else {
      return "scroll-down";
    }
  }
  if (isMove) {
    return "move";
  }
  const button = buttonCode & 3;
  const type = isRelease ? "release" : "press";
  switch (button) {
    case 0:
      return `left-${type}`;
    case 1:
      return `middle-${type}`;
    case 2:
      return `right-${type}`;
    default:
      return null;
  }
}
function getButtonFromCode(code) {
  const button = code & 3;
  switch (button) {
    case 0:
      return "left";
    case 1:
      return "middle";
    case 2:
      return "right";
    default:
      return "none";
  }
}
function parseSGRMouseEvent(buffer) {
  const match = buffer.match(import_input.SGR_MOUSE_REGEX);
  if (match) {
    const buttonCode = parseInt(match[1], 10);
    const col = parseInt(match[2], 10);
    const row = parseInt(match[3], 10);
    const action = match[4];
    const isRelease = action === "m";
    const shift = (buttonCode & 4) !== 0;
    const meta = (buttonCode & 8) !== 0;
    const ctrl = (buttonCode & 16) !== 0;
    const name = getMouseEventName(buttonCode, isRelease);
    if (name) {
      return {
        event: {
          name,
          ctrl,
          meta,
          shift,
          col,
          row,
          button: getButtonFromCode(buttonCode)
        },
        length: match[0].length
      };
    }
  }
  return null;
}
function parseX11MouseEvent(buffer) {
  const match = buffer.match(import_input.X11_MOUSE_REGEX);
  if (!match) return null;
  const b = match[1].charCodeAt(0) - 32;
  const col = match[1].charCodeAt(1) - 32;
  const row = match[1].charCodeAt(2) - 32;
  const shift = (b & 4) !== 0;
  const meta = (b & 8) !== 0;
  const ctrl = (b & 16) !== 0;
  const isMove = (b & 32) !== 0;
  const isWheel = (b & 64) !== 0;
  let name = null;
  if (isWheel) {
    const button = b & 3;
    switch (button) {
      case 0:
        name = "scroll-up";
        break;
      case 1:
        name = "scroll-down";
        break;
    }
  } else if (isMove) {
    name = "move";
  } else {
    const button = b & 3;
    if (button === 3) {
      name = "left-release";
    } else {
      switch (button) {
        case 0:
          name = "left-press";
          break;
        case 1:
          name = "middle-press";
          break;
        case 2:
          name = "right-press";
          break;
      }
    }
  }
  if (name) {
    let button = getButtonFromCode(b);
    if (name === "left-release" && button === "none") {
      button = "left";
    }
    return {
      event: {
        name,
        ctrl,
        meta,
        shift,
        col,
        row,
        button
      },
      length: match[0].length
    };
  }
  return null;
}
function parseMouseEvent(buffer) {
  return parseSGRMouseEvent(buffer) || parseX11MouseEvent(buffer);
}
function isIncompleteMouseSequence(buffer) {
  if (!(0, import_input.couldBeMouseSequence)(buffer)) return false;
  if (parseMouseEvent(buffer)) return false;
  if (buffer.startsWith(import_input.X11_EVENT_PREFIX)) {
    return buffer.length < import_input.X11_EVENT_PREFIX.length + 3;
  }
  if (buffer.startsWith(import_input.SGR_EVENT_PREFIX)) {
    return !/[mM]/.test(buffer) && buffer.length < 50;
  }
  return true;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ESC,
  disableMouseEvents,
  enableMouseEvents,
  getMouseEventName,
  isIncompleteMouseSequence,
  parseMouseEvent,
  parseSGRMouseEvent,
  parseX11MouseEvent
});
