import dayjs from 'dayjs';

export const isInTimeRange = (startTime?: string, endTime?: string) => {
  const now = dayjs();
  const start = dayjs(startTime);
  const end = dayjs(endTime);

  return now.isAfter(start) && now.isBefore(end);
};

export function showTime(time?: string, num?: number) {
  return time?.substring(0, num || 5) || '';
}

export function useCurrentDayTime() {
  let currentTime = dayjs().format('HH:mm:ss');
  let intervalId: NodeJS.Timeout;

  const start = () => {
    intervalId = setInterval(() => {
      currentTime = dayjs().format('HH:mm:ss');
    }, 1000);
  };

  const stop = () => {
    if (intervalId) {
      clearInterval(intervalId);
    }
  };

  const getTime = () => {
    return currentTime;
  };

  return {
    start,
    stop,
    getTime,
  };
}

// 保持原有函数兼容性
export function ShowCurrentDayTime() {
  const timeString = dayjs().format('HH:mm:ss');
  return timeString;
}

export function GetFromNow(time: string) {
  //获取时间距离现在的时间
  return time ? dayjs(time).fromNow() : '';
}

export function getTodayDate(): string {
  return dayjs().format('YYYY-MM-DD');
}
