function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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 dayjs from 'dayjs';
/** 将 HH:mm 或 HH:mm:ss 解析为当天该时间的 dayjs（基于 reference 的日期） */
function parseTimeOfDay(timeStr, reference) {
  var _timeStr$trim$split$m = timeStr.trim().split(':').map(Number),
    _timeStr$trim$split$m2 = _slicedToArray(_timeStr$trim$split$m, 3),
    h = _timeStr$trim$split$m2[0],
    m = _timeStr$trim$split$m2[1],
    s = _timeStr$trim$split$m2[2];
  return reference.hour(isNaN(h) ? 0 : h).minute(isNaN(m) ? 0 : m).second(isNaN(s) ? 0 : s).millisecond(0);
}

/** 匹配 HH:mm 或 HH:mm:ss 这类纯时间字符串 */
var TIME_ONLY_REGEX = /^\s*\d{1,2}:\d{2}(:\d{2})?\s*$/;

/**
 * 解析 value 为 Dayjs，无效则返回 null。
 * 支持 dayjs 可解析的日期时间；纯时间字符串（如 '12:00'、'14:30'）会基于 reference 的日期解析。
 */
export function parseValue(value, reference) {
  if (value == null) return null;
  var ref = reference !== null && reference !== void 0 ? reference : dayjs();
  if (typeof value === 'string' && TIME_ONLY_REGEX.test(value)) {
    return parseTimeOfDay(value, ref);
  }
  var d = dayjs(value);
  return d.isValid() ? d : null;
}

/**
 * 将 range 转为 [startDayjs, endDayjs]（reference 为营业锚定日的 0 点）
 * - 同日内：如 08:00–22:00
 * - 跨日营业：结束钟面早于或等于开始（如 02:01–次日 02:00）时，end 解析为 reference 的次日同一时刻
 */
export function getRangeBounds(range, reference) {
  if (!range) {
    return [reference.startOf('day'), reference.endOf('day')];
  }
  var start = parseTimeOfDay(range.start, reference);
  var end = parseTimeOfDay(range.end, reference);
  if (end.isBefore(start) || end.isSame(start)) {
    end = parseTimeOfDay(range.end, reference.add(1, 'day'));
  }
  return [start, end];
}

/**
 * 将时间吸附到 step 分钟刻度（0、15、30、45 等）：四舍五入到最近刻度。
 */
export function snapToStepMinutes(t, stepMinutes) {
  var m = t.minute();
  var snapped = Math.round(m / stepMinutes) * stepMinutes;
  if (snapped >= 60) {
    return t.add(1, 'hour').minute(0).second(0).millisecond(0);
  }
  return t.minute(snapped).second(0).millisecond(0);
}

/**
 * 判断时间是否落在任一禁用区间内（按绝对时间与区间 [start,end) 比较）
 * 注：与主时间轴同 reference 的跨日 range 一致时需传入同一营业锚定日解析出的区间。
 */
export function isInDisabledRanges(t, disabledRanges) {
  if (!(disabledRanges !== null && disabledRanges !== void 0 && disabledRanges.length)) return false;
  var tMin = t.startOf('minute');
  var _iterator = _createForOfIteratorHelper(disabledRanges),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var r = _step.value;
      var _getRangeBounds = getRangeBounds(r, t.startOf('day')),
        _getRangeBounds2 = _slicedToArray(_getRangeBounds, 2),
        s = _getRangeBounds2[0],
        e = _getRangeBounds2[1];
      if ((tMin.isAfter(s) || tMin.isSame(s)) && tMin.isBefore(e)) return true;
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return false;
}

/**
 * 在 [rangeStart, rangeEnd] 内按 stepMinutes 生成刻度时间列表（Dayjs）
 */
export function getTicks(rangeStart, rangeEnd, stepMinutes) {
  var out = [];
  var cur = rangeStart;
  while (!cur.isAfter(rangeEnd)) {
    out.push(cur);
    cur = cur.add(stepMinutes, 'minute');
  }
  return out;
}

/**
 * 横向时间轴「格」数量，与 useTimeAxis 中 totalLengthPx 算法一致（含尾格）
 */
export function getHorizontalAxisSlotCount(range, referenceDate, stepMinutes) {
  var _getRangeBounds3 = getRangeBounds(range, referenceDate),
    _getRangeBounds4 = _slicedToArray(_getRangeBounds3, 2),
    rangeStart = _getRangeBounds4[0],
    rangeEnd = _getRangeBounds4[1];
  var ticks = getTicks(rangeStart, rangeEnd, stepMinutes);
  var lastTick = ticks.length > 0 ? ticks[ticks.length - 1] : null;
  var needTailCell = lastTick != null && rangeEnd.isAfter(lastTick);
  return ticks.length + (needTailCell ? 1 : 0);
}