import { type LikertSliderProps } from './types';

export const isSingleValue = (
  value: LikertSliderProps['value'],
): value is number => {
  return typeof value === 'number';
};

export const isRangeValue = (
  value: LikertSliderProps['value'],
): value is [number, number] => {
  return Array.isArray(value) && value.length === 2;
};

export const formatPropsValue = (
  value: LikertSliderProps['value'],
): LikertSliderProps['value'] => {
  if (isSingleValue(value)) {
    return value + 1;
  }

  if (isRangeValue(value)) {
    return [value[0] + 1, value[1] + 1];
  }

  return value;
};

export const formatInternalValue = (
  value: NonNullable<LikertSliderProps['value']>,
): NonNullable<LikertSliderProps['value']> => {
  if (isSingleValue(value)) {
    return value - 1;
  }

  if (isRangeValue(value)) {
    return [value[0] - 1, value[1] - 1];
  }

  return value;
};

export const buildGetPercentage =
  (min: number, max: number) => (value: number) => {
    return ((value - min) / (max - min)) * 100;
  };

export const isBarredValue = (
  value: LikertSliderProps['value'],
  min: number,
  max: number,
) =>
  (isSingleValue(value) && (value === min || value === max)) ||
  (isRangeValue(value) &&
    (value[0] === min || value[1] === max || value[0] >= value[1])) ||
  value === undefined;
