<?php

/**
 * JCOGS Image Pro - CP JS End Extension
 * =====================================
 * Injects update-status badge scripts into CP pages.
 *
 * @category   ExpressionEngine Add-on
 * @package    JCOGS Image Pro
 * @author     JCOGS Design <contact@jcogs.net>
 * @copyright  2026 JCOGS Design
 * @license    https://jcogs.net/add-ons/license/jcogs_img_pro
 * @version    2.0.6
 * @link       https://JCOGS.net/
 */

namespace JCOGSDesign\JCOGSImagePro\Extensions;

use ExpressionEngine\Service\Addon\Controllers\Extension\AbstractRoute;

class CpJsEnd extends AbstractRoute
{
    /**
     * Handle cp_js_end hook.
     *
     * @return string
     */
    public function process(): string
    {
        $scripts = [];
        if (ee()->extensions->last_call !== false) {
            $scripts[] = (string) ee()->extensions->last_call;
        }

        try {
            $updateService = ee('jcogs_img_pro:UpdateCheckService');
            $badgeScript = $updateService->getCpBadgeScript();
            if ($badgeScript !== '') {
                $scripts[] = $badgeScript;
            }
        } catch (\Throwable $e) {
            // Soft-fail: never block CP due to update checks.
        }

        return implode('', $scripts);
    }
}
