// utils/dateUtils.js
export const getDateDifferenceInDays = (startDate, endDate) => {
  if (!startDate || !endDate) {
    throw new Error("Both startDate and endDate must be provided");
  }

  // Ensure the dates are valid
  const start = new Date(startDate);
  const end = new Date(endDate);

  if (isNaN(start) || isNaN(end)) {
    throw new Error("Invalid date format");
  }

  // Calculate the difference in milliseconds
  const differenceInMs = start - end;

  // Convert milliseconds to days
  const differenceInDays = Math.floor(differenceInMs / (1000 * 60 * 60 * 24));
  return differenceInDays;
};

export const formatDate = (date) => {
  if (!date) return ""; // Return an empty string if no date
  const dateObj = new Date(date);
  // return dateObj.toISOString().split("T")[0];
  return dateObj.toLocaleDateString('en-CA'); // Get the date in YYYY-MM-DD format
};

export const convertShortTime = (dateString) => {
  const date = new Date(dateString);

  let hours = date.getHours();
  const minutes = String(date.getMinutes()).padStart(2, "0");

  const ampm = hours >= 12 ? "PM" : "AM";
  hours = hours % 12; // Convert to 12-hour format
  hours = hours ? hours : 12; // If hours is 0, set it to 12

  return `${String(hours).padStart(2, "0")}:${minutes} ${ampm}`;
};

export const timeElapsed = (checkInTime, timeElapsedInHours) => {
  const checkInDate = new Date( checkInTime.split( "T" )[ 0 ] );

  const currentTime = new Date();

  const timeDifference = currentTime - checkInDate;

  const timeDifferenceInHours = timeDifference / (1000 * 60 * 60);

  return timeDifferenceInHours >= timeElapsedInHours;
};

export const checkIfCurrentDate = (dateString) => {
  const currentDate = new Date();
  const inputDate = new Date(dateString);

  currentDate.setHours(0, 0, 0, 0);
  inputDate.setHours(0, 0, 0, 0);

  return currentDate.getTime() === inputDate.getTime();
};
