import { getFileType } from '../getFileType';

export function isTypeValid(file: Blob, rule: string, file64?: string) {
  if (!rule) {
    return false;
  }

  const allowedTypes = rule.replace(/\s/g, '').split(',');
  const fileType = getFileType(file, file64);

  if (rule === '*' || allowedTypes.includes(fileType)) {
    return true;
  }

  return allowedTypes.some((type) => {
    const [typeAllowed, extensionAllowed] = type.split('/');
    return extensionAllowed === '*' && fileType.includes(typeAllowed);
  });
}
