import { VegaInternalTranslation } from '../translation/internal-translation-controller';
/**
 * CalendarTime represents hours/minutes in 24-hour format and centralizes parsing/formatting logic.
 */
export class CalendarTime {
  constructor(hours, minutes) {
    this._hours = hours;
    this._minutes = minutes;
  }
  /**
   * Parse a raw time string into a CalendarTime instance.
   *
   * @param {string} rawTime - Source time value such as "9:00" or "9am".
   * @returns {CalendarTime} Parsed time.
   */
  static fromString(rawTime) {
    const normalized = this.convertTimeTo24Hour(rawTime);
    const [hourPart, minutePart] = normalized.split(':');
    return new CalendarTime(Number(hourPart), Number(minutePart));
  }
  /**
   * Build a CalendarTime from a Date instance while preserving hours/minutes.
   *
   * @param {Date} date - The source date.
   * @returns {CalendarTime} Time representation extracted from the date.
   */
  static fromDate(date) {
    return new CalendarTime(date.getHours(), date.getMinutes());
  }
  /**
   * Calculate the duration in the requested precision between two CalendarTime values, accounting for overnight spans.
   *
   * @param {CalendarTime} start - Start time of the interval.
   * @param {CalendarTime} end - End time of the interval.
   * @param {'minutes' | 'hours'} precision - Desired unit of measure for the result (defaults to minutes).
   * @returns {number} Duration expressed in the requested precision.
   */
  static calculateDuration(start, end, precision = 'minutes') {
    const startMinutes = start.totalMinutes;
    let endMinutes = end.totalMinutes;
    if (end.hours === 0 && start.hours !== 0) {
      endMinutes += 24 * 60;
    }
    const durationMinutes = endMinutes - startMinutes;
    return precision === 'hours' ? durationMinutes / 60 : durationMinutes;
  }
  /**
   * Normalize various time inputs (e.g. 9am, 9:30 PM) to a 24-hour HH:mm string.
   *
   * @param {string} timeString - User supplied time value.
   * @returns {string} 24-hour formatted string.
   */
  static convertTimeTo24Hour(timeString) {
    const normalized = timeString.toLocaleLowerCase();
    if (normalized.includes('am') || normalized.includes('pm')) {
      const isAm = normalized.includes('am');
      const cleaned = normalized.replace('am', '').replace('pm', '').trim();
      const [rawHour, rawMinutes] = cleaned.split(':');
      const minutes = rawMinutes.padStart(2, '0');
      if (isAm) {
        return `${rawHour === '12' ? '00' : rawHour}:${minutes}`;
      }
      else {
        const convertedHour = rawHour === '12' ? '12' : `${Number(rawHour) + 12}`;
        return `${convertedHour}:${minutes}`;
      }
    }
    return timeString;
  }
  /**
   * Hour component in 24-hour clock.
   *
   * @returns {number} Hours between 0-23.
   */
  get hours() {
    return this._hours;
  }
  /**
   * Minute component.
   *
   * @returns {number} Minutes between 0-59.
   */
  get minutes() {
    return this._minutes;
  }
  /**
   * Total minutes elapsed since midnight for this time value.
   *
   * @returns {number} Minutes from 00:00.
   */
  get totalMinutes() {
    return this._hours * 60 + this._minutes;
  }
  /**
   * Format the time into a string using a simple formatter.
   *
   * @param {TimeStringFormat} format - time string Format. Defaults to 'hh:mm'.
   * @returns {string} Formatted time string.
   */
  toString(format = 'hh:mm') {
    const defaultHours = this._hours.toString().padStart(2, '0');
    const defaultMinutes = this._minutes.toString().padStart(2, '0');
    const normalizedFormat = format.toLowerCase();
    switch (normalizedFormat) {
      case 'hh:m':
        return `${defaultHours}:${this._minutes}`;
      case 'h:mm':
        return `${this._hours}:${defaultMinutes}`;
      case 'h:m':
        return `${this._hours}:${this._minutes}`;
      case '12-hour': {
        const amPm = this._hours >= 12 ? 'pm' : 'am';
        const hour12 = this._hours % 12 || 12;
        return `${hour12}:${defaultMinutes} ${VegaInternalTranslation.t(amPm)}`;
      }
      default:
        return `${defaultHours}:${defaultMinutes}`;
    }
  }
}
