import React, { useCallback } from 'react';
/**
 * 交互层：点击刻度跳转、键盘左右/上下 + 回车
 */
export function GestureLayer(_ref) {
  var orientation = _ref.orientation,
    cellSizePx = _ref.cellSizePx,
    axisStartMarginPx = _ref.axisStartMarginPx,
    offsetToTime = _ref.offsetToTime,
    offsetToTimeContinuous = _ref.offsetToTimeContinuous,
    onSelectTime = _ref.onSelectTime,
    isInDisabledRanges = _ref.isInDisabledRanges,
    snapToStep = _ref.snapToStep,
    stepMinutes = _ref.stepMinutes,
    snapFn = _ref.snapFn,
    scrollPosition = _ref.scrollPosition,
    viewportSize = _ref.viewportSize,
    onPointerDown = _ref.onPointerDown,
    onPointerMove = _ref.onPointerMove,
    onPointerUp = _ref.onPointerUp,
    onPointerLeave = _ref.onPointerLeave,
    onUserInteraction = _ref.onUserInteraction,
    children = _ref.children;
  var isHorizontal = orientation === 'horizontal';
  var handleClick = useCallback(function (e) {
    onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
    var viewport = e.currentTarget.parentElement;
    if (!viewport) return;
    var vRect = viewport.getBoundingClientRect();
    var scroll = isHorizontal ? viewport.scrollLeft : viewport.scrollTop;
    var scrollContentPos = isHorizontal ? scroll + (e.clientX - vRect.left) : scroll + (e.clientY - vRect.top);
    var offset = Math.max(0, scrollContentPos - axisStartMarginPx);
    /**
     * snapToStep=false：点击直接跳到指针对应的连续时间，避免被 offsetToTime
     * 内部的 Math.round 吸附到最近的 tick（会让点击在 6:50 落到 7:00 这种）；
     * snapToStep=true：维持原有「吸到最近 step 网格」语义。
     */
    var t = snapToStep ? offsetToTime(offset) : offsetToTimeContinuous(offset);
    if (t == null) return;
    var candidate = snapToStep ? snapFn(t) : t;
    if (isInDisabledRanges(candidate)) return;
    onSelectTime(candidate);
  }, [isHorizontal, axisStartMarginPx, offsetToTime, offsetToTimeContinuous, onSelectTime, isInDisabledRanges, snapToStep, snapFn, onUserInteraction]);
  var handleKeyDown = useCallback(function (e) {
    var scrollContentCenter = orientation === 'horizontal' ? scrollPosition.scrollLeft + viewportSize.w / 2 : scrollPosition.scrollTop + viewportSize.h / 2;
    var centerOffset = Math.max(0, scrollContentCenter - axisStartMarginPx);
    // 方向键同样在 snapToStep=false 时走连续时间，否则键盘步进会被 round 吸回 tick
    var currentTime = snapToStep ? offsetToTime(centerOffset) : offsetToTimeContinuous(centerOffset);
    if (currentTime == null) return;
    if (e.key === 'Enter') {
      var candidate = snapToStep ? snapFn(currentTime) : currentTime;
      if (!isInDisabledRanges(candidate)) {
        onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
        onSelectTime(candidate);
      }
      e.preventDefault();
      return;
    }
    var step = stepMinutes * 60 * 1000;
    var next;
    if (orientation === 'horizontal') {
      if (e.key === 'ArrowLeft') next = currentTime.subtract(step, 'ms');else if (e.key === 'ArrowRight') next = currentTime.add(step, 'ms');else return;
    } else {
      if (e.key === 'ArrowUp') next = currentTime.subtract(step, 'ms');else if (e.key === 'ArrowDown') next = currentTime.add(step, 'ms');else return;
    }
    e.preventDefault();
    var snapped = snapToStep ? snapFn(next) : next;
    if (!isInDisabledRanges(snapped)) {
      onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
      onSelectTime(snapped);
    }
  }, [orientation, axisStartMarginPx, offsetToTime, offsetToTimeContinuous, onSelectTime, isInDisabledRanges, snapToStep, snapFn, stepMinutes, scrollPosition.scrollLeft, scrollPosition.scrollTop, viewportSize.w, viewportSize.h, onUserInteraction]);
  var handlePointerDown = useCallback(function (e) {
    onUserInteraction === null || onUserInteraction === void 0 || onUserInteraction();
    if (onPointerDown) {
      e.currentTarget.setPointerCapture(e.pointerId);
      onPointerDown(e);
    }
  }, [onPointerDown, onUserInteraction]);
  var handlePointerUp = useCallback(function (e) {
    if (onPointerUp) {
      e.currentTarget.releasePointerCapture(e.pointerId);
      onPointerUp(e);
    }
  }, [onPointerUp]);
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-time-navigator-gesture pisell-time-navigator-gesture--".concat(orientation),
    onClick: handleClick,
    onKeyDown: handleKeyDown,
    onPointerDown: handlePointerDown,
    onPointerMove: onPointerMove,
    onPointerUp: handlePointerUp,
    onPointerLeave: onPointerLeave,
    role: "slider",
    tabIndex: 0,
    "aria-label": "Time"
  }, children);
}