<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  Joomla_Administrator_Component
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */

namespace Mefi\Component\MefisAssetBridge\Administrator\Service;

defined('_JEXEC') or die;

use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\CMS\Extension\ComponentInterface;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Extension\Service\Provider\MVCFactory;
use Joomla\CMS\Component\Router\RouterFactoryInterface;
use Joomla\CMS\Extension\Service\Provider\RouterFactory;
use Joomla\CMS\Dispatcher\ComponentDispatcherFactoryInterface;
use Joomla\CMS\Extension\Service\Provider\ComponentDispatcherFactory;
use Mefi\Component\MefisAssetBridge\Administrator\Helper\AuthHelper;
use Mefi\Component\MefisAssetBridge\Administrator\Helper\CoreBridgeHelper;
use Mefi\Component\MefisAssetBridge\Administrator\Extension\MefisAssetBridgeComponent;

/**
 * The MefisAssetBridge service provider.
 *
 * @since  1.0.0
 */
return new class implements ServiceProviderInterface
{
    /**
     * Registers the service provider with a DI container.
     *
     * @param   Container  $container  The DI container.
     *
     * @return  void
     *
     * @since   1.0.0
     */
    public function register(Container $container)
    {
        $container->registerServiceProvider(new MVCFactory('\\Mefi\\Component\\MefisAssetBridge'));
        $container->registerServiceProvider(new ComponentDispatcherFactory('\\Mefi\\Component\\MefisAssetBridge'));
        $container->registerServiceProvider(new RouterFactory('\\Mefi\\Component\\MefisAssetBridge'));
        $container->registerServiceProvider(new ComponentDispatcherFactory('\\Mefi\\Component\\MefisAssetBridge\\Administrator\\Dispatcher'));

        // Register the auth helper
        $container->share(
            AuthHelper::class,
            function (Container $container) {
                return new AuthHelper(
                    $container->get('app')
                );
            },
            true
        );
        // Register the core bridge helper
        $container->share(
            CoreBridgeHelper::class,
            function (Container $container) {
                return new CoreBridgeHelper(
                    $container->get('app')
                );
            },
            true
        );

        $container->set(
            ComponentInterface::class,
            function (Container $container) {
                $component = new MefisAssetBridgeComponent($container->get(ComponentDispatcherFactoryInterface::class));
                $component->setMVCFactory($container->get(MVCFactoryInterface::class));
                $component->setRouterFactory($container->get(RouterFactoryInterface::class));

                return $component;
            }
        );
    }
};