<?php

declare(strict_types=1);

$mainClass = 'Status';

class Status extends \Frame
{
    protected $template = 'board/status.tpl';

    public function get_vars()
    {
        $pageVars = parent::get_vars();

        $pageVars['title'] = 'Status';

        $pageVars['body']['assets'] = self::getAssets();
        $pageVars['body']['project'] = self::getProject();

        return $pageVars;
    }

    public static function getAssets()
    {
        $assets = json_decode(
            file_get_contents(
                './admin/static/build/status-dashboard/manifest.json',
                false,
                null
            ),
            true
        );

        $assetEntryPoint = $assets['src/main.tsx'];
        if ($assetEntryPoint === null && isDevelopment()) {
            throw new \RuntimeException("Action needed: in wpj container run command: 'update_status-dashboard.sh'");
        }

        return [
            'js' => ltrim($assetEntryPoint['file'], 'assets/'),
            'css' => array_map(function ($css) {
                return ltrim($css, 'assets/');
            }, $assetEntryPoint['css'], ),
        ];
    }

    public static function getProject()
    {
        global $cfg;
        $domain = str_replace(['www.', '/'], '', $cfg['Addr']['print_original'] ?? $cfg['Addr']['print']);

        return $domain;
    }
}
