export const debounce = (func, delay) => {
  let timeoutId

  return (...args) => {
    clearTimeout(timeoutId)
    timeoutId = setTimeout(() => func(...args), delay)
  }
}

export const throttle = (func, limit) => {
  let inThrottle

  return (...args) => {
    if (!inThrottle) {
      func.apply(this, args)
      inThrottle = true
      setTimeout(() => (inThrottle = false), limit)
    }
  }
}

// // Generate a 32-character hash code (64 hex characters = 32 bytes)
// export const generateLicenseCode = () => {
//   const array = new Uint8Array(32)
//   crypto.getRandomValues(array)
//   return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('')
// }

export const generateLicenseCode = () => {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
  const array = new Uint8Array(15)
  crypto.getRandomValues(array)

  return Array.from(array, byte => chars[byte % chars.length]).join('')
}

// Get today's date in YYYY-MM-DD format
export const getTodayDate = () => {
  const today = new Date()
  return today.toISOString().split('T')[0]
}
