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 { useState, useEffect } from 'react';
import dayjs from 'dayjs';
import { getDayjsLocale } from "../utils/localeUtils";
import usePisellConfig from "../../pisell-config-provider/hooks/usePisellConfig";
import useEngineContext from "../../../hooks/useEngineContext";

/**
 * useCurrentTime Hook 配置项
 */

/**
 * 管理当前时间状态的自定义 Hook
 *
 * @description
 * - 自动创建定时器刷新时间
 * - 支持自定义刷新间隔
 * - 支持国际化（locale）：优先级为 用户指定 > 全局配置 > i18n 当前语言
 * - 组件卸载时自动清除定时器
 *
 * @example
 * const currentTime = useCurrentTime({
 *   refreshInterval: 1000,
 *   locale: 'zh-CN',
 *   onUpdate: (time) => console.log(time.format())
 * });
 *
 * @param options 配置项
 * @returns 当前时间（Dayjs 对象）
 */
export function useCurrentTime(options) {
  var _context$engine;
  var _ref = options || {},
    _ref$refreshInterval = _ref.refreshInterval,
    refreshInterval = _ref$refreshInterval === void 0 ? 60000 : _ref$refreshInterval,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    userLocale = _ref.locale,
    onUpdate = _ref.onUpdate;
  var globalConfig = usePisellConfig();
  var context = useEngineContext();
  var globalLocale = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.locale;
  var engineLocale = context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale;

  // 优先级：用户指定 > 全局配置 > engine.props.locale > 'en'
  var dayjsLocale = getDayjsLocale(userLocale || globalLocale || engineLocale || 'en');

  // 初始化时间
  var _useState = useState(function () {
      var time = dayjs();
      return time.locale(dayjsLocale);
    }),
    _useState2 = _slicedToArray(_useState, 2),
    currentTime = _useState2[0],
    setCurrentTime = _useState2[1];
  useEffect(function () {
    // 如果禁用自动刷新，只设置一次
    if (disabled) {
      return;
    }

    // 设置定时器
    var timer = setInterval(function () {
      var time = dayjs();
      var localizedTime = time.locale(dayjsLocale);
      setCurrentTime(localizedTime);
      onUpdate === null || onUpdate === void 0 || onUpdate(localizedTime);
    }, refreshInterval);

    // 清除定时器
    return function () {
      clearInterval(timer);
    };
  }, [refreshInterval, disabled, dayjsLocale, onUpdate]);

  // 当 locale 改变时，立即更新时间的 locale
  useEffect(function () {
    setCurrentTime(function (prevTime) {
      return prevTime.locale(dayjsLocale);
    });
  }, [dayjsLocale]);
  return currentTime;
}