<?php

declare(strict_types=1);


use App\Blog\Support\ThemeManager;

if (! function_exists('theme_view')) {
    /**
     * 渲染主题视图：激活主题目录已前置到全局视图路径，主题缺失时自动回退默认视图。
     */
    function theme_view(string $view, array $data = []): \Illuminate\Contracts\View\View
    {
        return view($view, $data);
    }
}

if (! function_exists('theme')) {
    /**
     * 主题辅助函数：theme() / theme('asset', 'style.css') / theme('var', 'key', default)
     */
    function theme(?string $method = null, ...$args): mixed
    {
        $manager = app(ThemeManager::class);

        return match ($method) {
            'asset' => $manager->assetUrl($args[0] ?? '', $args[1] ?? null),
            'var' => $manager->variable($args[0] ?? '', $args[1] ?? null),
            'vars' => $manager->variables(),
            'name' => $manager->active(),
            default => $manager,
        };
    }
}

if (! function_exists('blog_setting')) {
    /**
     * 读取博客设置（Setting 表，带缓存）。
     */
    function blog_setting(string $key, mixed $default = null): mixed
    {
        return \App\Models\Setting::get($key, $default);
    }
}
