import { computed, ref, watchEffect } from 'vue';
const AUTO_INTERVAL = 200;
const STEP_BUCKETS = [[30, 0.05], [70, 0.03], [96, 0.01]];
export const usePercent = (spinning, percent) => {
  const mockPercent = ref(0);
  let mockIntervalRef = null;
  const isAuto = computed(() => percent.value === 'auto');
  watchEffect(cleanup => {
    if (isAuto.value && spinning.value) {
      mockPercent.value = 0;
      mockIntervalRef = setInterval(() => {
        const prev = mockPercent.value;
        const restPTG = 100 - prev;
        for (let i = 0; i < STEP_BUCKETS.length; i += 1) {
          const [limit, stepPtg] = STEP_BUCKETS[i];
          if (prev <= limit) {
            mockPercent.value = prev + restPTG * stepPtg;
            return;
          }
        }
        mockPercent.value = prev;
      }, AUTO_INTERVAL);
    }
    cleanup(() => {
      clearInterval(mockIntervalRef);
    });
  });
  return computed(() => isAuto.value ? mockPercent.value : percent.value);
};