<?php
/*
 * @package    ECLCDEK shipping plugin СДЭК to RadicalMart
 * @subpackage    ECLCDEK
 * @version    1.0.1
 * @author Econsult Lab.
 * @copyright  Copyright (c) 2025 Econsult Lab. All rights reserved.
 * @license    GNU/GPL license: https://www.gnu.org/copyleft/gpl.html
 * @link       https://econsultlab.ru
 */

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Database\DatabaseDriver;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\RadicalMartShipping\ECLCDEK\Extension\ECLCDEK;

return new class implements ServiceProviderInterface {

    /**
     * Registers the service provider with a DI container.
     *
     * @param Container $container The DI container.
     *
     * @since   1.0.0
     */
    public function register(Container $container): void
    {
        $container->set(PluginInterface::class,
            function (Container $container) {
                // Create plugin class
                $subject = $container->get(DispatcherInterface::class);
                $config = (array)PluginHelper::getPlugin('radicalmart_payment', 'eclcdek');
                $plugin = new ECLCDEK($subject, $config);

                // Set application
                $app = Factory::getApplication();
                $plugin->setApplication($app);

                // Set database
                $db = $container->get(DatabaseDriver::class);
                $plugin->setDatabase($db);

                return $plugin;
            }
        );
    }
};
