// Допустимые типы и расширения файлов для медиа
export const ALLOWED_MEDIA_TYPES = {
  image: {
    extensions: [".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".svg"],
    mimeTypes: [
      "image/jpeg",
      "image/png",
      "image/gif",
      "image/webp",
      "image/bmp",
      "image/svg+xml",
    ],
    accept: "image/*",
  },
  video: {
    extensions: [".mp4", ".webm", ".ogg", ".mov", ".avi"],
    mimeTypes: [
      "video/mp4",
      "video/webm",
      "video/ogg",
      "video/quicktime",
      "video/x-msvideo",
    ],
    accept: "video/*",
  },
  model3d: {
    extensions: [".glb", ".gltf"],
    mimeTypes: ["model/gltf-binary", "model/gltf+json"],
    accept: ".glb,.gltf",
  },
  panorama: {
    extensions: [".jpg", ".jpeg", ".png"],
    mimeTypes: ["image/jpeg", "image/png"],
    accept: "image/*",
  },
} as const;

export const getAllAllowedExtensions = (): string[] => {
  return [
    ...ALLOWED_MEDIA_TYPES.image.extensions,
    ...ALLOWED_MEDIA_TYPES.video.extensions,
    ...ALLOWED_MEDIA_TYPES.model3d.extensions,
  ];
};

export const getAllAcceptString = (): string => {
  return `${ALLOWED_MEDIA_TYPES.image.accept},${ALLOWED_MEDIA_TYPES.video.accept},${ALLOWED_MEDIA_TYPES.model3d.accept}`;
};

export const validateFileExtension = (
  file: File
): { valid: boolean; error?: string } => {
  const fileName = file.name.toLowerCase();
  const extension = fileName.substring(fileName.lastIndexOf("."));
  const allowedExtensions = getAllAllowedExtensions();

  if (!allowedExtensions.includes(extension)) {
    return {
      valid: false,
      error: `Неподдерживаемый формат файла "${extension}". Допустимые форматы: ${allowedExtensions.join(
        ", "
      )}`,
    };
  }

  return { valid: true };
};

export const filterValidFiles = (
  files: File[]
): { validFiles: File[]; errors: string[] } => {
  const validFiles: File[] = [];
  const errors: string[] = [];

  files.forEach((file) => {
    const validation = validateFileExtension(file);
    if (validation.valid) {
      validFiles.push(file);
    } else {
      errors.push(`${file.name}: ${validation.error}`);
    }
  });

  return { validFiles, errors };
};
