import { LogUtility } from './log';
// TODO: Need rename calendar-date as vega-date to manage all date operation in vega project
// https://gethired.atlassian.net/browse/VD-5918
/**
 * It returns true if the input is a valid date string, and false otherwise.
 * Note:
 *  This is not a strict date validation, it just use Date.parse to check the input string can be parsed to a valid date or not.
 *  For strict date validation, please use CalendarDate.fromString method in calendar-date.ts
 *  Example:
 *      For Chrome: Date.parse('Jan/08/2026') => 1767801600000
 *      For Safari: Date.parse('Jan/08/2026') => NaN
 *      Format is DD/MM/YYYY: Date.parse('20/11/2024') => NaN
 *
 * @param {string} input - The string to check.
 * @returns {boolean} is a valid date string or not
 */
export const isDateStr = (input) => !isNaN(Date.parse(input));
/**
 * Compare if the firstDate is after the secondDate
 *
 * @param {Date} firstDate - The first date to compare.
 * @param {Date} secondDate - The date to compare to.
 * @returns {boolean} Return true if the firstDate is after the secondDate, otherwise it returns false.
 */
export const compareIsAfter = (firstDate, secondDate) => {
  return firstDate.getTime() > secondDate.getTime();
};
/**
 * Compare if the firstDate is before the secondDate
 *
 * @param {Date} firstDate - The first date to compare.
 * @param {Date} secondDate - The date to compare to.
 * @returns {boolean} Return true if the firstDate is before the secondDate, otherwise it returns false.
 */
export const compareIsBefore = (firstDate, secondDate) => {
  return firstDate.getTime() < secondDate.getTime();
};
/**
 * Compare if the first date is before or the same as the second date.
 *
 * @param {Date} firstDate - The first date to compare.
 * @param {Date} secondDate - The date to compare against.
 * @returns {boolean} Return true if the firstDate is before or the same as the secondDate, otherwise it returns false.
 */
export const compareIsSameOrBefore = (firstDate, secondDate) => {
  return firstDate.getTime() <= secondDate.getTime();
};
/**
 * It returns true if the input string is a valid time string in the format specified by the timeFormat parameter
 *
 * @param {string} input - The string to be tested.
 * @param {TimeFormat} timeFormat - '12-hour' | '24-hour'
 * @returns {boolean} A boolean value.
 */
export const isTimeStr = (input, timeFormat) => {
  return timeFormat === '12-hour'
    ? new RegExp(/^(1[012]|0[1-9]):[0-5][0-9] ([AP][M])$/).test(input)
    : new RegExp(/^([0-1][0-9]|2[0-3]):[0-5][0-9]$/).test(input);
};
/**
 * It takes a number and returns a string
 *
 * @param {number} number - number - The number to pad
 * @param {number} [length=2] - The length of the resulting string.
 * @param {string} [padChar='0'] - The character to pad the number with.
 * @returns {Nullable<string>} A function that takes a number as an argument and returns a nullable string.
 */
export const pad = (number, length = 2, padChar = '0') => {
  if (number || number === 0) {
    return number.toString().padStart(length, padChar);
  }
  else {
    LogUtility.error(`The input should be a number`);
  }
};
/**
 * The `convert12HourTo24HourFormat` function takes a time string in 12-hour format and converts it to 24-hour format.
 *
 * @param {string} time the time string need to convert, the time string should be 12-hour or 24-hour format
 * @returns {string} 24 hour format time string
 */
export const convert12HourTo24HourFormat = (time) => {
  if (isTimeStr(time, '12-hour')) {
    const [hour, minute, amPm] = time.replace(' ', ':').split(':');
    let convertedHour = parseInt(hour, 10);
    if (amPm === 'PM' && convertedHour !== 12) {
      convertedHour += 12;
    }
    else if (amPm === 'AM' && convertedHour === 12) {
      convertedHour = 0;
    }
    return `${pad(convertedHour)}:${minute}`;
  }
  return time;
};
