import Chip from "@/components/Chip";
import filterData from '@/lib/assets/data/currentFilter.json';


type ServiceTagOptions = {
  text: string,
  style: string
}

const filters = filterData.filter;

const formatLabel = (text: string) => {
    const label = 0;
    const value = 1;
    if (text.includes(':')){
        const splittedLabel = text.split(':');
        return `${translateLabel(splittedLabel[label])}: ${translateValue(splittedLabel[label], splittedLabel[value])}`
    }
    return text;
  };

  function translateLabel(label: string): string {
      const filterCategoriesForTranslation = filters.filter(filterCategory => {
          return filterCategory.id == label
        });
    return filterCategoriesForTranslation.length > 0 ? filterCategoriesForTranslation[0].label : '';
  }

  function translateValue(label: string, value: string): string {
    const filterCategoriesForTranslation = filters.filter(filterCategory => {
      return filterCategory.id == label
    });
    if (filterCategoriesForTranslation.length > 0) {
        const filterCategory = filterCategoriesForTranslation[0];
        const filterValues = filterCategory.options.filter(option => {
            return option.id == value
        });
        return filterValues.length > 0 ? filterValues[0].label : '';
    } else {
        return '';
    }
  }

export default ({ text, style }: ServiceTagOptions) => {
  return (
    <Chip text={formatLabel(text)} style={`${style} capitalize ${text == 'label:external' ? 'hidden' : '' }`} />
  );
};