"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 MouseContext_exports = {};
__export(MouseContext_exports, {
  MouseProvider: () => MouseProvider,
  useMouse: () => useMouse,
  useMouseContext: () => useMouseContext
});
module.exports = __toCommonJS(MouseContext_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_ink = require("ink");
var import_react = require("react");
var import_input = require("../utils/input.js");
var import_mouse = require("../utils/mouse.js");
const MAX_MOUSE_BUFFER_SIZE = 4096;
const MouseContext = (0, import_react.createContext)(void 0);
function useMouseContext() {
  const context = (0, import_react.useContext)(MouseContext);
  if (!context) {
    throw new Error("useMouseContext must be used within a MouseProvider");
  }
  return context;
}
function useMouse(handler, { isActive = true } = {}) {
  const { subscribe, unsubscribe } = useMouseContext();
  (0, import_react.useEffect)(() => {
    if (!isActive) {
      return;
    }
    subscribe(handler);
    return () => unsubscribe(handler);
  }, [isActive, handler, subscribe, unsubscribe]);
}
function MouseProvider({ children, mouseEventsEnabled = true }) {
  const { stdin } = (0, import_ink.useStdin)();
  const subscribers = (0, import_react.useRef)(/* @__PURE__ */ new Set()).current;
  const subscribe = (0, import_react.useCallback)(
    (handler) => {
      subscribers.add(handler);
    },
    [subscribers]
  );
  const unsubscribe = (0, import_react.useCallback)(
    (handler) => {
      subscribers.delete(handler);
    },
    [subscribers]
  );
  (0, import_react.useEffect)(() => {
    if (!mouseEventsEnabled) {
      return;
    }
    (0, import_mouse.enableMouseEvents)();
    return () => {
      (0, import_mouse.disableMouseEvents)();
    };
  }, [mouseEventsEnabled]);
  (0, import_react.useEffect)(() => {
    if (!mouseEventsEnabled) {
      return;
    }
    let mouseBuffer = "";
    const broadcast = (event) => {
      for (const handler of subscribers) {
        if (handler(event) === true) {
          break;
        }
      }
    };
    const handleData = (data) => {
      mouseBuffer += typeof data === "string" ? data : data.toString("utf-8");
      if (mouseBuffer.length > MAX_MOUSE_BUFFER_SIZE) {
        mouseBuffer = mouseBuffer.slice(-MAX_MOUSE_BUFFER_SIZE);
      }
      while (mouseBuffer.length > 0) {
        const parsed = (0, import_mouse.parseMouseEvent)(mouseBuffer);
        if (parsed) {
          broadcast(parsed.event);
          mouseBuffer = mouseBuffer.slice(parsed.length);
          continue;
        }
        if ((0, import_mouse.isIncompleteMouseSequence)(mouseBuffer)) {
          break;
        }
        const nextEsc = mouseBuffer.indexOf(import_input.ESC, 1);
        if (nextEsc !== -1) {
          mouseBuffer = mouseBuffer.slice(nextEsc);
        } else {
          mouseBuffer = "";
          break;
        }
      }
    };
    stdin.on("data", handleData);
    return () => {
      stdin.removeListener("data", handleData);
    };
  }, [stdin, mouseEventsEnabled, subscribers]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MouseContext.Provider, { value: { subscribe, unsubscribe }, children });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  MouseProvider,
  useMouse,
  useMouseContext
});
