import { TaskActionTypeType } from '../../models';

/**
 * Checks if a task action type value matches a given type, including its RETOURE_ variant.
 *
 * This helper normalizes both values by stripping the 'RETOURE_' prefix before comparison,
 * allowing flexible matching between base types and their return variants.
 *
 * @param value - The task action type value to check (can be a base type or RETOURE_ variant)
 * @param type - The task action type to compare against (can be a base type or RETOURE_ variant)
 * @returns `true` if the normalized values match, `false` otherwise
 *
 * @example
 * // All of these return true:
 * isTaskType('OK', TaskActionTypes.OK)              // 'OK' === 'OK'
 * isTaskType('RETOURE_OK', TaskActionTypes.OK)      // 'OK' === 'OK'
 * isTaskType('OK', TaskActionTypes.RETOURE_OK)      // 'OK' === 'OK'
 * isTaskType('RETOURE_OK', TaskActionTypes.RETOURE_OK) // 'OK' === 'OK'
 *
 * @example
 * // Returns false:
 * isTaskType('OK', TaskActionTypes.NOK)             // 'OK' !== 'NOK'
 * isTaskType('RETOURE_OK', TaskActionTypes.UNKNOWN) // 'OK' !== 'UNKNOWN'
 * isTaskType(undefined, TaskActionTypes.OK)         // value is falsy
 */
export const isTaskType = (
  value: TaskActionTypeType | string | undefined | null,
  type: TaskActionTypeType,
): boolean => {
  if (!value) return false;
  const normalizedValue = value.replace('RETOURE_', '');
  const normalizedType = type.replace('RETOURE_', '');
  return normalizedValue === normalizedType;
};
