<?php
/**
 * @intranet-file common/composer_loader.php
 * @intranet-file-version 2026.04.09.1
 * @intranet-file-date 2026-04-09
 * @intranet-file-owner CSIF Intranet
 */

declare(strict_types=1);

if (!function_exists('intranet_composer_package_map')) {
    function intranet_composer_package_map(): array
    {
        return [
            'phpmailer/phpmailer' => [
                'PHPMailer\\PHPMailer\\PHPMailer',
                'PHPMailer\\PHPMailer\\Exception',
            ],
            'phpoffice/phpspreadsheet' => [
                'PhpOffice\\PhpSpreadsheet\\IOFactory',
                'PhpOffice\\PhpSpreadsheet\\Spreadsheet',
            ],
        ];
    }
}

if (!function_exists('intranet_composer_symbol_exists')) {
    function intranet_composer_symbol_exists(string $symbol): bool
    {
        return class_exists($symbol)
            || interface_exists($symbol)
            || trait_exists($symbol);
    }
}

if (!function_exists('intranet_composer_packages_ready')) {
    function intranet_composer_packages_ready(array $packages): bool
    {
        if ($packages === []) {
            return true;
        }

        $packageMap = intranet_composer_package_map();
        foreach ($packages as $package) {
            $symbols = $packageMap[$package] ?? [];
            if ($symbols === []) {
                continue;
            }

            $ready = false;
            foreach ($symbols as $symbol) {
                if (intranet_composer_symbol_exists($symbol)) {
                    $ready = true;
                    break;
                }
            }

            if (!$ready) {
                return false;
            }
        }

        return true;
    }
}

if (!function_exists('intranet_composer_autoload_candidates')) {
    function intranet_composer_autoload_candidates(array $extraCandidates = []): array
    {
        $root = dirname(__DIR__);
        $candidates = array_merge([
            $root . '/vendor/autoload.php',
            __DIR__ . '/vendor/autoload.php',
            $root . '/base/enlaces/vendor/autoload.php',
            $root . '/apps/entregas/vendor/autoload.php',
            $root . '/plugins/entregas/apps/entregas/vendor/autoload.php',
        ], $extraCandidates);

        $unique = [];
        foreach ($candidates as $candidate) {
            $candidate = str_replace('\\', '/', (string)$candidate);
            if ($candidate === '' || isset($unique[$candidate])) {
                continue;
            }
            $unique[$candidate] = true;
        }

        return array_keys($unique);
    }
}

if (!function_exists('intranet_require_composer')) {
    function intranet_require_composer(array $packages = [], array $extraCandidates = []): void
    {
        static $attempted = [];

        if (intranet_composer_packages_ready($packages)) {
            return;
        }

        foreach (intranet_composer_autoload_candidates($extraCandidates) as $autoloadFile) {
            if (!is_file($autoloadFile) || !is_readable($autoloadFile)) {
                continue;
            }

            if (!isset($attempted[$autoloadFile])) {
                require_once $autoloadFile;
                $attempted[$autoloadFile] = true;
            }

            if (intranet_composer_packages_ready($packages)) {
                return;
            }
        }

        $packageList = $packages !== [] ? implode(', ', $packages) : 'dependencias compartidas';
        throw new RuntimeException(
            'Faltan dependencias Composer (' . $packageList . '). ' .
            'La ruta canonica es /vendor en la raiz de la intranet.'
        );
    }
}
