export const lowerFirstLetter = (str: string) => {
  return str.charAt(0).toLowerCase() + str.slice(1);
};

export const getDateAsString = (date: Date, withTime = false) => {
  const time = date.toLocaleTimeString('cs-CZ');
  const timeWithoutSeconds = time.substring(0, time.lastIndexOf(':'));
  return `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}${
    withTime ? ` ${timeWithoutSeconds}` : ''
  }`;
};

export const getTimeAsString = (time: Date) => {
  return `PT${time.getHours()}H${time.getMinutes() !== 0 ? time.getMinutes() + 'M' : ''}`;
};

export const getTimeFromString = (time: string) => {
  if (!time) {
    return new Date();
  }
  const hours = parseInt(time.split('H')[0]?.replace('PT', ''), 10);
  const minutes = parseInt(time.split('H')[1]?.replace('M', ''), 10);
  const date = new Date();
  date.setHours(hours);
  date.setMinutes(!isNaN(minutes) ? minutes : 0);
  return date;
};

export const setColorOpacity = (hex: string, alpha: number) =>
  `${hex}${Math.floor(alpha * 255)
    .toString(16)
    .padStart(2, '0')}`;
