// Mirrors the PHP TimeslotHelper::generateSlots() logic so the admin
// preview matches exactly what customers will see at checkout.

const toMinutes = (time) => {
  const parts = String(time).split(':')
  return (parseInt(parts[0], 10) || 0) * 60 + (parseInt(parts[1], 10) || 0)
}

const toTime = (minutes) => {
  const capped = Math.min(1440, minutes)
  const h = Math.floor(capped / 60)
  const m = capped % 60
  return String(h).padStart(2, '0') + ':' + String(m).padStart(2, '0')
}

export default function generateSlots(startTime, endTime, durationHours) {
  const duration = Math.max(1, Math.min(12, parseInt(durationHours, 10) || 0)) * 60
  const start = toMinutes(startTime || '00:00')
  const end = endTime === '24:00' ? 1440 : toMinutes(endTime || '24:00')

  const slots = []
  let cursor = start
  while (cursor + duration <= end) {
    slots.push(toTime(cursor) + '-' + toTime(cursor + duration))
    cursor += duration
  }
  return slots
}
