const timeToMinutes = (time) => {
  if (!time) return 0;

  const [h, m] = time.split(":").map(Number);
  return h * 60 + m;
};

// Convert "09:00 AM" → minutes
const calculateMinutes = (checkIn, checkOut) => {
    if (!checkIn || !checkOut) return 0;

    const start = new Date(`1970-01-01 ${checkIn}`);
    const end = new Date(`1970-01-01 ${checkOut}`);

    const diff = (end - start) / 60000;
    return diff > 0 ? diff : 0;
};

const minutesToHHMM = (mins) => {
    const h = Math.floor(mins / 60);
    const m = mins % 60;
    return `${h}:${m.toString().padStart(2, "0")}`;
};

const formatTime = (t) => {
    if (!t) return null;
    const [h, m] = t.split(":");
    return `${h.padStart(2, "0")}:${m.padStart(2, "0")}`;
};

module.exports = { timeToMinutes,calculateMinutes , minutesToHHMM, formatTime };
