/*
“Commons Clause” License Condition v1.0
https://commonsclause.com/

The Software is provided to you by the Licensor under the License, as defined below, subject to the following condition.

Without limiting other conditions in the License, the grant of rights under the License will not include, and the License does not grant to you, the right to Sell the Software.

For purposes of the foregoing, “Sell” means practicing any or all of the rights granted to you under the License to provide to third parties, for a fee or other consideration (including without limitation fees for hosting or consulting/ support services related to the Software), a product or service whose value derives, entirely or substantially, from the functionality of the Software. Any license notice or attribution required by the License must also include this Commons Clause License Condition notice.

Software: Open Archival

License: GNU GPL: https://www.gnu.org/licenses/gpl-3.0.en.html

Licensor: Vincent Allen
*/

namespace OpenArchival.Blazor.FileViewer;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public static class FileViewerFactory
{
    private static Dictionary<string, Type> _extensionMapping = [];

    static FileViewerFactory()
    {
        // Image types taken from https://developer.mozilla.org/en-US/docs/Web/Media/Guides/Formats/Image_types
        RegisterViewerComponent<ImageViewer>([".apng", ".png", ".avif", ".gif", ".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", ".svg", ".webp"]);
    }

    public static void RegisterViewerComponent<ComponentType>(IEnumerable<string> extensions) where ComponentType : IFileViewer
    {
        foreach (var extension in extensions)
        {
            _extensionMapping.Add(extension, typeof(ComponentType));
        }
    }

    public static Type GetViewerComponent(string filename)
    {
        if (_extensionMapping.TryGetValue(Path.GetExtension(filename).ToLowerInvariant(), out var componentType))
        {
            return componentType;
        }
        else
        {
            return typeof(UnsupportedFileViewer);
        }
    }
}