<?php
declare(strict_types=1);

namespace PpPhp\Plugins;

use PpPhp\Core\IconManager\IconDefinition;
use PpPhp\Core\IconManager\Contracts\IconPackInterface;
use PpPhp\Core\IconManager\Loaders\FontAwesomeIndexLoader;

class FontAwesomePro implements IconPackInterface
{
    public static function id(): string
    {
        return 'FontAwesomePro';
    }

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

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

    public static function family(): string
    {
        return 'fontawesome';
    }

    public static function assets(): array
    {
        return [
            'css' => [
                'plugins/IconManager/FontAwesomePro/assets/7.2/css/fontawesome.min.css',
                'plugins/IconManager/FontAwesomePro/assets/7.2/css/solid.min.css',
                'plugins/IconManager/FontAwesomePro/assets/7.2/css/regular.min.css',
                'plugins/IconManager/FontAwesomePro/assets/7.2/css/brands.min.css',
            ],
            'js' => [],
        ];
    }

    public static function metadata(): array
    {
        return [
            'index' => 'plugins/IconManager/FontAwesomePro/assets/7.2/index/fontawesome-pro-7.2-icons.json',
            'license' => 'FontAwesome Pro license required. Do not publish package files without a valid license.',
        ];
    }

    /** @return array<int, IconDefinition> */
    public static function icons(): array
    {
        return FontAwesomeIndexLoader::load(
            constant('BASE_PATH') . '/plugins/IconManager/FontAwesomePro/assets/7.2/index/fontawesome-pro-7.2-icons.json'
        );
    }

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

        if ($term === '') {
            return array_slice($icons, 0, $limit);
        }

        $results = [];

        foreach ($icons as $icon) {
            $haystack = mb_strtolower(implode(' ', array_merge(
                [$icon->name, $icon->label, $icon->className, $icon->style],
                $icon->categories,
                $icon->aliases,
                $icon->keywords
            )));

            if (str_contains($haystack, $term)) {
                $results[] = $icon;
            }

            if (count($results) >= $limit) {
                break;
            }
        }

        return $results;
    }
}
