import { i18n } from '@plugins/i18n';

const CHAR_LIMIT = 210;

export function resDocNameLengthStringRule(val: string): boolean | string {
  return (
    val.length < CHAR_LIMIT ||
    String(i18n.t('validation.documents.invalidResDocNameLength'))
  );
}

export function resDocNameLengthFileRule(val: File[]): boolean | string {
  const file: File = val[0];
  if (file) {
    const name = file?.name?.split('.').slice(0, -1).join('.') || '';
    return (
      name.length < CHAR_LIMIT ||
      String(i18n.t('validation.documents.invalidResDocNameLength'))
    );
  }
  return true;
}

export function resDocNameFileRule(val: File[]): boolean | string {
  const file: File = val[0];
  const name = file?.name?.split('.').slice(0, -1).join('.') || '';
  if (file && !/^[-\sA-Za-zА-Яа-я0-9]+(\.[A-Za-z]{3,4})?$/.test(name)) {
    return String(i18n.t('validation.documents.invalidResDocName'));
  }
  return true;
}
