namespace NubLang.Syntax;

public sealed class Module
{
    public static Dictionary<string, Module> Collect(List<SyntaxTree> syntaxTrees)
    {
        var modules = new Dictionary<string, Module>();
        foreach (var syntaxTree in syntaxTrees)
        {
            if (!modules.TryGetValue(syntaxTree.ModuleName, out var module))
            {
                module = new Module();
                modules.Add(syntaxTree.ModuleName, module);
            }

            module._definitions.AddRange(syntaxTree.Definitions);
        }

        return modules;
    }

    private readonly List<DefinitionSyntax> _definitions = [];

    public List<StructSyntax> Structs(bool includePrivate)
    {
        return _definitions
            .OfType<StructSyntax>()
            .Where(x => x.Exported || includePrivate)
            .ToList();
    }

    public List<FuncSyntax> Functions(bool includePrivate)
    {
        return _definitions
            .OfType<FuncSyntax>()
            .Where(x => x.Exported || includePrivate)
            .ToList();
    }

    public List<EnumSyntax> Enums(bool includePrivate)
    {
        return _definitions
            .OfType<EnumSyntax>()
            .Where(x => x.Exported || includePrivate)
            .ToList();
    }
}