<?php

declare(strict_types=1);

namespace App\Framework\Database;

use App\Framework\Database\Metadata\MetadataExtractor;
use App\Framework\Database\Metadata\MetadataRegistry;
use App\Framework\Database\TypeCaster\TypeCasterRegistry;

final class EntityManagerFactory
{
    public static function create(DatabaseManager $databaseManager): EntityManager
    {
        $metadataExtractor = new MetadataExtractor();
        $metadataRegistry = new MetadataRegistry($metadataExtractor);
        $casterRegistry = new TypeCasterRegistry();
        $typeConverter = new TypeConverter($casterRegistry);
        #$hydrator = new Hydrator($typeConverter);
        $identityMap = new IdentityMap();

        $lazyLoader = new LazyLoader(
            databaseManager: $databaseManager,
            typeConverter: $typeConverter,
            identityMap: $identityMap,
            metadataRegistry: $metadataRegistry
        );

        return new EntityManager(
            databaseManager: $databaseManager,
            metadataRegistry: $metadataRegistry,
            typeConverter: $typeConverter,
            identityMap: $identityMap,
            lazyLoader: $lazyLoader
        );
    }
}
