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 { getRangeBounds, getTicks } from "../utils";
/**
 * 时间轴刻度与时间↔像素换算
 */
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 ticks = getTicks(rangeStart, rangeEnd, stepMinutes);
    /**
     * 仅当范围结束晚于最后一个刻度时（如全天 23:59）才多一格尾段，使最后一刻可滚到视口中心。
     * 自定义范围如 08:00–20:00 的最后一格已是 20:00，不再多一格。
     */
    var lastTick = ticks.length > 0 ? ticks[ticks.length - 1] : null;
    /** 横向：仅当范围结束晚于最后刻度时多一格尾段 */
    var needTailCell = orientation === 'horizontal' && lastTick != null && rangeEnd.isAfter(lastTick);
    /** 竖向：范围结束恰在最后刻度时（如 08:00–20:00 的 20:00）不保留最后一格高度，与横向自定义范围一致 */
    var noTailVertical = orientation === 'vertical' && lastTick != null && !rangeEnd.isAfter(lastTick);
    var effectiveTicks = noTailVertical && ticks.length > 0 ? ticks.slice(0, -1) : ticks;
    var rawTotalPx = orientation === 'vertical' ? effectiveTicks.length * cellSizePx : (ticks.length + (needTailCell ? 1 : 0)) * cellSizePx;
    /** 防止异常 range/step 导致轴长爆炸（如竖向出现 33M 高度），上限约 2 天 15 分钟步进 */
    var totalLengthPx = Math.min(rawTotalPx, 500000);
    /**
     * 横向：每格中心为「时间下面的点」，dotOffsetPx = cellSizePx/2。
     * 竖向：对齐整点上面的粗线（格顶），dotOffsetPx = 0。
     */
    var dotOffsetPx = orientation === 'vertical' ? 0 : cellSizePx / 2;
    /** 最大偏移：竖向为最后一格顶；横向有尾格时为最后一格中心延伸，无尾格时为最后一格中心 */
    var maxOffsetPx = orientation === 'vertical' ? totalLengthPx : dotOffsetPx + (needTailCell ? ticks.length : Math.max(0, ticks.length - 1)) * cellSizePx;
    var timeToOffset = function timeToOffset(t) {
      var diffMs = t.diff(rangeStart);
      var diffMin = diffMs / (60 * 1000);
      var index = diffMin / stepMinutes;
      var px = dotOffsetPx + index * cellSizePx;
      return Math.max(dotOffsetPx, Math.min(px, maxOffsetPx));
    };
    var offsetToTime = function offsetToTime(offset) {
      var index = Math.round((offset - dotOffsetPx) / cellSizePx);
      var tick = effectiveTicks[index];
      return tick !== null && tick !== void 0 ? tick : null;
    };

    /** 偏移对应连续时间；横向以格点为中心，竖向以格顶为基准 */
    var offsetToTimeContinuous = function offsetToTimeContinuous(offset) {
      var index = (offset - dotOffsetPx) / cellSizePx;
      var minutesFromStart = index * stepMinutes;
      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: effectiveTicks,
      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]);
}