function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useMemo } from 'react';
import { getAxisCells, getRangeBounds } from "../utils";
/**
 * 时间轴刻度与时间↔像素换算。
 *
 * 格子拆分策略见 {@link getAxisCells}：当 `range.start` / `range.end` 不在 `stepMinutes` 网格上时，
 * 首尾出现宽度按时间比例缩小的 head/tail 格，中间格保持 step 整步并落在网格整点上。
 *
 * 时间→像素仍是线性映射 `dotOffsetPx + (t - rangeStart) / step × cellSize`，与 head/tail 是否存在无关：
 * `dotOffsetPx`（横向 = `cellSize/2`，竖向 = 0）为 rangeStart 在轴内的像素位置；轴两端各保留 `dotOffsetPx`
 * 留白以居中 rangeStart / rangeEnd。
 */
export function useTimeAxis(options) {
  var range = options.range,
    stepMinutes = options.stepMinutes,
    referenceDate = options.referenceDate,
    orientation = options.orientation,
    cellSizePx = options.cellSizePx;
  return useMemo(function () {
    var _getRangeBounds = getRangeBounds(range, referenceDate),
      _getRangeBounds2 = _slicedToArray(_getRangeBounds, 2),
      rangeStart = _getRangeBounds2[0],
      rangeEnd = _getRangeBounds2[1];
    var safeStep = Math.max(1, stepMinutes);
    var cells = getAxisCells(rangeStart, rangeEnd, safeStep);
    var ticks = cells.map(function (c) {
      return c.start;
    });
    var cellWidthsPx = cells.map(function (c) {
      return c.widthMinutes / safeStep * cellSizePx;
    });
    var cellsTotalPx = cellWidthsPx.reduce(function (s, w) {
      return s + w;
    }, 0);
    /**
     * 横向：每格中心为「时间下面的点」，dotOffsetPx = cellSizePx/2，rangeStart / rangeEnd
     * 在轴上分别留 cellSize/2 留白以便居中。
     * 竖向：对齐整点上面的粗线（格顶），dotOffsetPx = 0，无两端留白。
     */
    var dotOffsetPx = orientation === 'vertical' ? 0 : cellSizePx / 2;
    var rawTotalPx = orientation === 'horizontal' ? cellsTotalPx + cellSizePx : cellsTotalPx;
    /** 防止异常 range/step 导致轴长爆炸（如竖向出现 33M 高度），上限约 2 天 15 分钟步进 */
    var totalLengthPx = Math.min(rawTotalPx, 500000);
    /** rangeEnd 在轴上的像素位置；横向 = dotOffsetPx + cellsTotalPx，竖向 = cellsTotalPx */
    var maxOffsetPx = orientation === 'vertical' ? cellsTotalPx : dotOffsetPx + cellsTotalPx;
    var timeToOffset = function timeToOffset(t) {
      var diffMs = t.diff(rangeStart);
      var diffMin = diffMs / (60 * 1000);
      var index = diffMin / safeStep;
      var px = dotOffsetPx + index * cellSizePx;
      return Math.max(dotOffsetPx, Math.min(px, maxOffsetPx));
    };

    /**
     * 吸附到最近的「格起点」：
     * head/tail 长度可能小于 step 整步，传统的「按 cellSize 等分四舍五入」无法直接定位 tick；
     * 这里按 ticks 实际像素位置线性扫描求最近邻，ticks 数组通常 ≤ ~200，开销可忽略。
     */
    var offsetToTime = function offsetToTime(offset) {
      if (ticks.length === 0) return null;
      var bestIdx = 0;
      var bestDist = Infinity;
      for (var i = 0; i < ticks.length; i += 1) {
        var px = timeToOffset(ticks[i]);
        var d = Math.abs(px - offset);
        if (d < bestDist) {
          bestDist = d;
          bestIdx = i;
        }
      }
      return ticks[bestIdx];
    };

    /** 偏移对应连续时间；横向以格点为中心，竖向以格顶为基准 */
    var offsetToTimeContinuous = function offsetToTimeContinuous(offset) {
      var index = (offset - dotOffsetPx) / cellSizePx;
      var minutesFromStart = index * safeStep;
      var t = rangeStart.add(minutesFromStart, 'minute');
      if (t.isBefore(rangeStart)) return rangeStart;
      if (t.isAfter(rangeEnd)) return rangeEnd;
      return t;
    };
    return {
      rangeStart: rangeStart,
      rangeEnd: rangeEnd,
      ticks: ticks,
      cellWidthsPx: cellWidthsPx,
      timeToOffset: timeToOffset,
      offsetToTime: offsetToTime,
      offsetToTimeContinuous: offsetToTimeContinuous,
      totalLengthPx: totalLengthPx
    };
  }, [range === null || range === void 0 ? void 0 : range.start, range === null || range === void 0 ? void 0 : range.end, stepMinutes, referenceDate.valueOf(), orientation, cellSizePx]);
}