import { TimeOffRequest } from 'src/types/timeTracking';
import { LokaliseTFunction } from 'src/utils/i18n';
import {
  processHoursForDisplay,
  sliceTimeToHoursAndMinutes,
} from 'src/utils/timeTracking';

type TimeOffRequestWordings = {
  typeDescription: string;
  additionalDetails?: string;
};

export const getTimeOffRequestWordings = (
  timeOffRequest: TimeOffRequest,
  t: LokaliseTFunction,
  formatDate: (date: Date | string) => string,
): TimeOffRequestWordings => {
  const {
    activityType,
    consumptionType,
    fromDate,
    fromTime,
    hours,
    toDate,
    toTime,
  } = timeOffRequest;
  const spanSeveralDays = fromDate && toDate && fromDate !== toDate;
  if (consumptionType === 'HALF_DAY') {
    return {
      typeDescription: spanSeveralDays
        ? t('time_tracking_common:dates_from_to', {
            startDate: formatDate(fromDate),
            endDate: formatDate(toDate),
          })
        : t('time_tracking_common:time_offs_permits.half_day'),
    };
  }
  if (consumptionType === 'HOURS') {
    const completeHourRange = fromTime && toTime && fromTime !== toTime;
    if (completeHourRange) {
      return {
        typeDescription: t('time_tracking_common:hours_from_to', {
          startHour: sliceTimeToHoursAndMinutes(fromTime),
          endHour: sliceTimeToHoursAndMinutes(toTime),
        }),
      };
    }
    const { hours: displayHours, minutes: displayMinutes } =
      processHoursForDisplay(hours ?? 0);
    const duration = t('time_tracking_common:hours_and_minutes_abbreviated', {
      hours: displayHours,
      minutes: displayMinutes,
    });
    return {
      typeDescription: `${duration} ${t('time_tracking_common:of_permission')}`,
    };
  }
  if (consumptionType === 'FULL_DAY') {
    const workableTimeOff = activityType === 'WORK';
    return {
      typeDescription: spanSeveralDays
        ? t('time_tracking_common:dates_from_to', {
            startDate: formatDate(fromDate),
            endDate: formatDate(toDate),
          })
        : t('time_tracking_common:time_offs_permits.full_day'),
      additionalDetails: workableTimeOff
        ? t(
            'time_tracking_common:time_offs_permits.full_day_workable_description',
          )
        : undefined,
    };
  }
  return {
    typeDescription: t('time_tracking_common:time_offs_permits.full_day'),
  };
};
