/* Copyright © 2020 Splunk Inc.
SPLUNK CONFIDENTIAL – Use or disclosure of this material in whole or
in part without a valid written license from Splunk Inc. is PROHIBITED. */
import {
  exists,
  getSeriesKey
} from "./chunk-6N4JNZ5S.js";
import {
  react_exports
} from "./chunk-V3S5WUKN.js";
import {
  __export,
  __reExport,
  __spreadValues
} from "./chunk-XC4WXK3L.js";

// esm-externals:lodash/debounce
var debounce_exports = {};
__export(debounce_exports, {
  default: () => debounce_default
});
__reExport(debounce_exports, debounce_star);
import * as defaultImport from "lodash/debounce";
import * as debounce_star from "lodash/debounce";
var debounce_default = "default" in defaultImport ? defaultImport.default : defaultImport;

// src/cursorPlugin.ts
function cursorPluginFactory({
  id,
  plotUtilitiesFn,
  plotRenderer,
  cursorEl,
  onChange,
  onCrosshairChange,
  onCrosshairClick,
  onRangeSelection
}) {
  let state = {
    state: "off",
    cursor: null,
    selection: null,
    seriesIndex: null,
    active: false
  };
  const fireOnChange = debounce_default((nextState) => onChange(nextState), 10);
  function change(nextState) {
    state = Object.assign(state, nextState);
    fireOnChange(state);
  }
  function fireCrosshairChange(cursor) {
    const plotUtilities = plotUtilitiesFn();
    if (!onCrosshairChange || !plotUtilities) {
      return;
    }
    if (!cursor) {
      onCrosshairChange(void 0);
      return;
    }
    if (!state.active) {
      return;
    }
    onCrosshairChange({
      time: plotUtilities.posToVal(cursor.left, "x"),
      seriesKey: getSeriesKey(state.seriesIndex, plotUtilities.allSeries)
    });
  }
  function setCrosshairValue(crosshairValue) {
    const plotUtilities = plotUtilitiesFn();
    if (state.active || !plotUtilities) {
      return;
    }
    if (crosshairValue) {
      plotUtilities.setCursor(
        {
          left: plotUtilities.valToPos(crosshairValue.time, "x"),
          // using `undefined` top will prevent uplot from highlighting a "closest series"
          top: void 0
        },
        true
      );
    } else {
      plotUtilities.setCursor({ left: -10, top: -10 }, true);
    }
  }
  function fireRangeSelection(e, selection) {
    const plotUtilities = plotUtilitiesFn();
    if (!state.active || !plotUtilities || !onRangeSelection || !selection || !exists(selection.left) || !exists(selection.width)) {
      return;
    }
    const start = selection.width > 0 ? selection.left : selection.left - selection.width;
    const end = selection.width > 0 ? selection.left + selection.width : selection.left;
    onRangeSelection(
      {
        start: plotUtilities.posToVal(start, "x"),
        end: plotUtilities.posToVal(end, "x"),
        vizId: id
      },
      e
    );
  }
  const mouseenter = () => {
    change({ active: true });
  };
  const mouseleave = () => {
    const plotUtilities = plotUtilitiesFn();
    change({ active: false, cursor: null });
    plotUtilities == null ? void 0 : plotUtilities.setCursor({ left: -10, top: -10 });
    fireCrosshairChange(void 0);
  };
  let clientX;
  let clientY;
  let selecting = false;
  const mousedown = (e) => {
    clientX = e.clientX;
    clientY = e.clientY;
  };
  const mouseup = (e) => {
    const plotUtilities = plotUtilitiesFn();
    if (onCrosshairClick && e.clientX === clientX && e.clientY === clientY) {
      const { seriesIndex, cursor } = state;
      if (plotUtilities && cursor && exists(seriesIndex)) {
        onCrosshairClick({
          time: plotUtilities.posToVal(cursor.left, "x"),
          index: cursor.index,
          seriesIndex,
          originalEvent: e,
          seriesKey: plotUtilities.allSeries[seriesIndex].field
        });
      }
    }
    if (selecting && exists(clientX) && exists(clientY)) {
      selecting = false;
      fireRangeSelection(e, state.selection);
      change({
        selection: null
      });
    }
    clientX = void 0;
    clientY = void 0;
  };
  const mousemove = (e) => {
    const plotUtilities = plotUtilitiesFn();
    if (!cursorEl || !plotUtilities) {
      return;
    }
    if (exists(clientX) && exists(clientY)) {
      selecting = true;
      const overRect2 = plotUtilities.dimensions.over;
      const left2 = Math.max(
        0,
        e.clientX - clientX > 0 ? clientX - overRect2.left : e.clientX - overRect2.left
      );
      const right = e.clientX > clientX ? e.clientX - overRect2.left : clientX - overRect2.left;
      const width = Math.min(overRect2.width, right - left2);
      change({
        selection: { left: Math.max(0, left2), width: Math.max(0, width) }
      });
    }
    const overRect = plotUtilities.dimensions.over;
    const top = e.clientY - overRect.top;
    const left = e.clientX - overRect.left;
    plotUtilities.setCursor({ left, top }, true);
  };
  const setupEventListeners = () => {
    if (cursorEl) {
      cursorEl.addEventListener("mouseenter", mouseenter);
      cursorEl.addEventListener("mouseleave", mouseleave);
      cursorEl.addEventListener("mousedown", mousedown);
      cursorEl.addEventListener("mouseup", mouseup);
      cursorEl.addEventListener("mousemove", mousemove);
    }
  };
  plotRenderer.onSetCursor((cursor) => {
    if (!cursor) {
      change({
        state: "off",
        cursor: null,
        seriesIndex: null
      });
    } else {
      change({
        state: "hover",
        cursor
      });
      fireCrosshairChange(state.cursor);
    }
  });
  plotRenderer.onFocusSeries((seriesIndex) => {
    change({
      seriesIndex
    });
  });
  const cleanupEventListeners = () => {
    if (cursorEl) {
      cursorEl.removeEventListener("mouseenter", mouseenter);
      cursorEl.removeEventListener("mouseleave", mouseleave);
      cursorEl.removeEventListener("mousedown", mousedown);
      cursorEl.removeEventListener("mouseup", mouseup);
      cursorEl.removeEventListener("mousemove", mousemove);
    }
  };
  return {
    // Receive input from external system
    setCrosshairValue,
    setupEventListeners,
    cleanupEventListeners
  };
}
var useCursorPlugin = ({
  id,
  plotUtilitiesFn,
  plotRenderer,
  cursorEl,
  crosshairValue,
  onCrosshairChange,
  onCrosshairClick,
  onRangeSelection
}) => {
  const [cursorState, setCursorState] = (0, react_exports.useState)();
  const { setCrosshairValue, setupEventListeners, cleanupEventListeners } = (0, react_exports.useMemo)(
    () => cursorPluginFactory({
      id,
      plotUtilitiesFn,
      plotRenderer,
      cursorEl,
      onChange: (nextCursorState) => {
        setCursorState(__spreadValues({}, nextCursorState));
      },
      onCrosshairChange,
      onCrosshairClick,
      onRangeSelection
    }),
    [id, plotUtilitiesFn, plotRenderer, cursorEl, onCrosshairChange, onCrosshairClick, onRangeSelection]
  );
  (0, react_exports.useEffect)(() => setCrosshairValue(crosshairValue), [setCrosshairValue, crosshairValue]);
  (0, react_exports.useEffect)(() => {
    setupEventListeners();
    return cleanupEventListeners;
  }, [setupEventListeners, cleanupEventListeners]);
  return { cursorState };
};

export {
  cursorPluginFactory,
  useCursorPlugin
};
//# sourceMappingURL=chunk-5RMFCCVG.js.map
