import { Mask, MethodReturn } from '@/types/utils';

export const snils: Mask = (text: string): string => {
  if (!/^\d{11}$/.test(text)) return '';

  const [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11] = text;

  return `${d1}${d2}${d3}-${d4}${d5}${d6}-${d7}${d8}${d9} ${d10}${d11}`;
};

export const passportSeries: Mask = (text: string): string => {
  const matches = text.match(/\d{1,4}/);

  if (!matches?.length) return '';

  return matches[0];
};

export const passportNumber: Mask = (text: string): string => {
  const matches = text.match(/\d{1,6}/);

  if (!matches?.length) return '';

  return matches[0];
};

export const maskName: Mask = (value: string) => {
  return value.replace(/[^а-яА-ЯёЁ\s-]/g, '');
};

export const maskNumbers: MethodReturn<number, Mask> = (size: number) => {
  return function (value: string) {
    return value
      .replace(/[^0-9]/g, '')
      .split('')
      .splice(0, size)
      .join('');
  };
};

export const maskCyrillic: MethodReturn<number, Mask> = (size: number) => {
  return function (value: string) {
    return value
      .replace(/[^а-яА-ЯёЁ]/g, '')
      .split('')
      .splice(0, size)
      .join('');
  };
};