const isValidExtension = (filename: string, fileType: string) => (
  fileAccept: string
): boolean => {
  const mimeType = fileType.toLowerCase();
  const baseMimeType = mimeType.replace(/\/.*$/, '');
  const normalizedFileAccept = fileAccept.toLowerCase();
  // fileAccept has filename extension. Eg: ".jpg"
  if (normalizedFileAccept.charAt(0) === '.') {
    return filename.toLowerCase().endsWith(normalizedFileAccept);
  }
  // fileAccept as mime type wild card. Eg: "image/*"
  if (normalizedFileAccept.endsWith('/*')) {
    return baseMimeType === normalizedFileAccept.replace(/\/.*$/, '');
  }
  // fileAccept as mine type & filename extension. Eg: "image/png"
  return mimeType === normalizedFileAccept;
};

export { isValidExtension };
