<?php
declare(strict_types=1);

namespace PpPhp\Core\IconManager\Loaders;

use PpPhp\Core\IconManager\IconDefinition;

final class FontAwesomeIndexLoader
{
    /** @return array<int, IconDefinition> */
    public static function load(string $jsonPath): array
    {
        if (!is_file($jsonPath)) {
            return [];
        }

        $payload = json_decode((string) file_get_contents($jsonPath), true);

        if (!is_array($payload) || !isset($payload['icons']) || !is_array($payload['icons'])) {
            return [];
        }

        $icons = [];

        foreach ($payload['icons'] as $item) {
            if (!is_array($item)) {
                continue;
            }

            $icons[] = new IconDefinition(
                name: (string) ($item['name'] ?? ''),
                label: (string) ($item['label'] ?? $item['name'] ?? ''),
                pack: (string) ($item['pack'] ?? 'fontawesome-pro'),
                family: 'fontawesome',
                style: (string) ($item['style'] ?? 'solid'),
                className: (string) ($item['className'] ?? ''),
                categories: is_array($item['categories'] ?? null) ? $item['categories'] : [],
                aliases: is_array($item['aliases'] ?? null) ? $item['aliases'] : [],
                keywords: is_array($item['keywords'] ?? null)
                    ? $item['keywords']
                    : (is_array($item['searchTerms'] ?? null) ? $item['searchTerms'] : [])
            );
        }

        return $icons;
    }
}
