<?php
declare(strict_types=1);

namespace PpPhp\Resources\Icons\MaterialSymbols;

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 'material-symbols'; }
    public static function label(): string { return 'Material Symbols'; }
    public static function type(): string { return 'icons'; }
    public static function version(): string { return '1.0.0'; }

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

    public static function assets(): array
    {
        return [
            'css' => ['assets/vendor/material-symbols/material-symbols.css'],   // localized (no CDN)
            'js' => [],
        ];
    }

    /**
     * All Material Symbols (Outlined), generated from the local codepoints file.
     * The glyph is rendered from the icon name (ligature) by the front-end.
     *
     * @return array<int, IconDefinition>
     */
    public static function icons(): array
    {
        $file = __DIR__ . '/material-symbols.codepoints';
        if (!is_file($file)) {
            return [];
        }
        $out = [];
        foreach (file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
            $parts = preg_split('/\s+/', trim($line));
            $name = $parts[0] ?? '';
            if ($name === '') {
                continue;
            }
            $out[] = new IconDefinition(
                name: $name,
                label: ucwords(str_replace('_', ' ', $name)),
                pack: 'material-symbols',
                family: 'material',
                style: 'font',
                className: 'material-symbols-outlined ' . $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);
    }
}
