export function isEmptyObject(obj: any) {
  return obj && Object.keys(obj).length === 0 && obj.constructor === Object;
}

export function getYear(date = new Date()) {
  if (!(date instanceof Date)) {
    throw new TypeError('Invalid date object');
  }
  return date.getFullYear();
}

export function debounce<T extends (...args: any[]) => any>(fn: T, delay = 300) {
  let timer: ReturnType<typeof setTimeout>;
  return (...args: Parameters<T>) => {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      fn(...args);
    }, delay);
  };
}

/**
 * 获取当前年、月、日、时、分、秒，并格式化为适用于文件名的字符串。
 * 格式为：YYYYMMDD_HHmmss (例如：20251029_172406)
 * @returns {string} 格式化后的日期时间字符串。
 */
export function getDateTimeForFilename() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hour = String(now.getHours()).padStart(2, '0');
  const minute = String(now.getMinutes()).padStart(2, '0');
  const second = String(now.getSeconds()).padStart(2, '0');

  return `${year}${month}${day}_${hour}${minute}${second}`;
}
