import { type Key } from 'react';

export const normalizeText = (text: string) => {
  return text
    .toLowerCase()
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '');
};

export const isEqualText = (textA: string, textB: string) => {
  return normalizeText(textA) === normalizeText(textB);
};

export const isCreatableOption = (value: Key): value is string =>
  typeof value === 'string' && /^__(.*?)__$/.test(value);

export const getCreatableValue = (value: string) => `__${value}__`;

export const getCreatableInputText = (value: string) => {
  const match = value.match(/^__(.*?)__$/);
  return match ? match[1] : value;
};
