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; }
/**
 * 顶栏日程与时间轴：`schedule` 立即反映用户操作；`debouncedSchedule` 在停手后（或 flush）再更新，
 * 供接口与宿主调用，避免拖动时连续打 API。
 */
import { useCallback, useEffect, useRef, useState } from 'react';
import dayjs from 'dayjs';
import { RESERVATION_SCHEDULE_DEBOUNCE_MS } from "../reservationConstants";
function defaultScheduleValue() {
  return {
    date: dayjs().startOf('day'),
    at: dayjs()
  };
}

/**
 * @returns `schedule` 绑定日程带 UI；`debouncedSchedule` 绑定 sales / patch / 外部 onSearch。
 * `queueCommittedSchedule`：防抖；`flushCommittedSchedule`：立即同步（如「跟随当前时间」）。
 */
export function useReservationScheduleDebounced(options) {
  var scheduleValueProp = options.scheduleValueProp;
  var _useState = useState(defaultScheduleValue),
    _useState2 = _slicedToArray(_useState, 2),
    internalSchedule = _useState2[0],
    setInternalSchedule = _useState2[1];
  var schedule = scheduleValueProp !== null && scheduleValueProp !== void 0 ? scheduleValueProp : internalSchedule;
  var scheduleRef = useRef(schedule);
  scheduleRef.current = schedule;
  var _useState3 = useState(function () {
      return scheduleValueProp !== null && scheduleValueProp !== void 0 ? scheduleValueProp : defaultScheduleValue();
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    debouncedSchedule = _useState4[0],
    setDebouncedSchedule = _useState4[1];
  var scheduleCommitTimerRef = useRef(null);
  var flushCommittedSchedule = useCallback(function (next) {
    if (scheduleCommitTimerRef.current != null) {
      window.clearTimeout(scheduleCommitTimerRef.current);
      scheduleCommitTimerRef.current = null;
    }
    setDebouncedSchedule(next);
  }, []);
  var queueCommittedSchedule = useCallback(function (next) {
    if (scheduleCommitTimerRef.current != null) {
      window.clearTimeout(scheduleCommitTimerRef.current);
    }
    scheduleCommitTimerRef.current = window.setTimeout(function () {
      scheduleCommitTimerRef.current = null;
      setDebouncedSchedule(next);
    }, RESERVATION_SCHEDULE_DEBOUNCE_MS);
  }, []);
  useEffect(function () {
    return function () {
      if (scheduleCommitTimerRef.current != null) {
        window.clearTimeout(scheduleCommitTimerRef.current);
      }
    };
  }, []);
  var scheduleAtMs = schedule.at != null && dayjs(schedule.at).isValid() ? dayjs(schedule.at).valueOf() : null;
  var scheduleDayMs = dayjs(schedule.date).startOf('day').valueOf();

  /** 受控 `scheduleValue` 从父传入时，日期/时刻变化同样走防抖，避免依赖不稳定的对象引用 */
  useEffect(function () {
    if (scheduleValueProp === undefined) return;
    queueCommittedSchedule(scheduleRef.current);
  }, [scheduleAtMs, scheduleDayMs, scheduleValueProp, queueCommittedSchedule]);
  return {
    internalSchedule: internalSchedule,
    setInternalSchedule: setInternalSchedule,
    schedule: schedule,
    debouncedSchedule: debouncedSchedule,
    scheduleRef: scheduleRef,
    flushCommittedSchedule: flushCommittedSchedule,
    queueCommittedSchedule: queueCommittedSchedule
  };
}