// hooks/useSafeNavigate.js
import { ref } from 'vue';

// 模块顶层的全局状态锁
const globalIsNavigating = ref(false);

export function useSafeNavigate(options = {}) {
  const defaultOptions = {
    wait: 300, // 导航冷却时间（ms）
  };
  const config = { ...defaultOptions, ...options };

  const safeNavigateTo = (url, navigateOptions = {}) => {
    if (globalIsNavigating.value) {
      console.log('正在跳转...');
      return;
    }

    globalIsNavigating.value = true;
    uni.navigateTo({
      url,
      ...navigateOptions,
      success: () => {
        setTimeout(() => {
          globalIsNavigating.value = false;
        }, config.wait);
      },
      fail: (err) => {
        console.error('Navigation failed:', err);
        globalIsNavigating.value = false;
      },
    });
  };

  return {
    isNavigating: globalIsNavigating, // 共享的全局状态
    safeNavigateTo,
  };
}