<?php
declare(strict_types=1);

namespace PpPhp\Resources\Icons\BootstrapIcons;

use PpPhp\Core\IconManager\IconDefinition;
use PpPhp\Core\IconManager\IconSearch;
use PpPhp\Core\Resources\Contracts\ResourcePackInterface;

final class Pack implements ResourcePackInterface
{
    public static function id(): string { return 'bootstrap-icons'; }
    public static function label(): string { return 'Bootstrap Icons'; }
    public static function type(): string { return 'icons'; }
    public static function version(): string { return '1.0.0'; }

    public static function metadata(): array
    {
        return ['family' => 'bootstrap'];
    }

    public static function assets(): array
    {
        return [
            'css' => ['assets/icons/bootstrap-icons.min.css'],   // served locally from public/assets (no CDN)
            'js' => [],
        ];
    }

    /**
     * All Bootstrap Icons, generated from the local name map
     * (public/assets/icons/bootstrap-icons.json, copied here for server-side use).
     *
     * @return array<int, IconDefinition>
     */
    public static function icons(): array
    {
        $file = __DIR__ . '/bootstrap-icons.json';
        if (!is_file($file)) {
            return [];
        }
        $map = json_decode((string) file_get_contents($file), true) ?: [];
        $out = [];
        foreach (array_keys($map) as $name) {
            $name = (string) $name;
            $out[] = new IconDefinition(
                name: 'bi-' . $name,
                label: ucwords(str_replace('-', ' ', $name)),
                pack: 'bootstrap-icons',
                family: 'bootstrap',
                style: 'font',
                className: 'bi bi-' . $name,
                categories: [],
                aliases: [],
                keywords: array_values(array_filter(explode('-', $name))),
            );
        }
        return $out;
    }

    /** @return array<int, IconDefinition> */
    public static function search(string $query, int $limit = 100): array
    {
        return IconSearch::search(self::icons(), $query, $limit);
    }
}
