import { Data } from '../../../../types';
import { FilterValue, SearcheableOptionsFilter } from '../../Filters.model';

export function renderSearcheableOptionsFilterValue<T extends Data>(
  value: FilterValue,
  stringValue: string | undefined,
  setStringValue: (val: string) => void,
  options: SearcheableOptionsFilter<T>,
): string {
  if (stringValue !== undefined) {
    return stringValue;
  }

  const selectedVal = options
    .optionsProvider('', value)
    .find((option) => option.value === value)?.label;

  if (selectedVal !== undefined) {
    setStringValue(selectedVal);
    return selectedVal;
  }

  return String(value);
}
