<?php

declare(strict_types=1);

namespace PpPhp\Resources\Icons\FontAwesomePro;

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

final class Pack implements ResourcePackInterface
{
    public static function id(): string
    {
        return 'fontawesome-pro';
    }

    public static function label(): string
    {
        return 'FontAwesome Pro';
    }

    public static function type(): string
    {
        return 'icons';
    }

    public static function version(): string
    {
        return '7.2';
    }

    public static function metadata(): array
    {
        return [
            'family' => 'fontawesome',
            'license' => 'FontAwesome Pro license required.',
            'index' => 'resources/Icons/FontAwesomePro/assets/7.2/index/fontawesome-pro-7.2-icons.json',
            'default_style' => 'solid',
            'supported_styles' => [
                'solid',
                'regular',
                'brands',
            ],
        ];
    }

    public static function assets(): array
    {
        // Served from the web root (public/assets); the raw pack under resources/ is
        // outside the document root, so its files must be published to be reachable.
        // Publish with: cp -R resources/Icons/FontAwesomePro/assets/7.2/{css,webfonts}
        //   public/assets/vendor/fontawesome/7.2/   (only solid/regular/brands fonts needed)
        return [
            'css' => [
                'assets/vendor/fontawesome/7.2/css/fontawesome.min.css',
                'assets/vendor/fontawesome/7.2/css/solid.min.css',
                'assets/vendor/fontawesome/7.2/css/regular.min.css',
                'assets/vendor/fontawesome/7.2/css/brands.min.css',
            ],
            'js' => [],
        ];
    }

    /**
     * @return IconDefinition[]
     */
    public static function icons(): array
    {
        return FontAwesomeIndexLoader::load(
            self::absolutePath('assets/7.2/index/fontawesome-pro-7.2-icons.json')
        );
    }

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

    private static function absolutePath(string $relative): string
    {
        return __DIR__ . '/' . ltrim($relative, '/');
    }
}
