export interface NormalizedToken {
  rawToken: string;
  numericValue: number | null;
  valid: boolean;
}

const VALID_TOKENS = ['H', 'O', '0', '.5', '0.5', '1'];

export function normalizeToken(
  raw: string,
  isWeekend: boolean = false,
  isHoliday: boolean = false
): NormalizedToken {
  const trimmed = raw.trim();

  if (!VALID_TOKENS.includes(trimmed)) {
    return {
      rawToken: trimmed,
      numericValue: null,
      valid: false,
    };
  }

  let rawToken = trimmed;
  let numericValue: number;

  switch (trimmed) {
    case 'H':
    case 'O':
      numericValue = 0;
      break;
    case '0':
      if (isWeekend) {
        rawToken = 'O';
      } else if (isHoliday) {
        rawToken = 'H';
      }
      numericValue = 0;
      break;
    case '.5':
      rawToken = '0.5';
      numericValue = 0.5;
      break;
    case '0.5':
      numericValue = 0.5;
      break;
    case '1':
      numericValue = 1;
      break;
    default:
      return {
        rawToken: trimmed,
        numericValue: null,
        valid: false,
      };
  }

  return {
    rawToken,
    numericValue,
    valid: true,
  };
}
