<?php

namespace App\Support;

final class CategoryCatalog
{
    /** @return array{name: string, color: string, icon: string} */
    public static function get(string $id): array
    {
        $all = config('budgets_categories', []);
        if (! isset($all[$id])) {
            $id = 'other';
        }
        $row = $all[$id];
        $row['name'] = self::label($id);

        return $row;
    }

    public static function label(string $id): string
    {
        $key = 'budgets.categories.'.$id;
        $line = (string) __($key);

        if ($line !== $key) {
            return $line;
        }

        $all = config('budgets_categories', []);

        return (string) (($all[$id] ?? $all['other'])['name'] ?? $id);
    }

    /**
     * @return array<string, array{name: string, color: string, icon: string}>
     */
    public static function all(): array
    {
        $out = [];
        foreach (config('budgets_categories', []) as $id => $row) {
            $out[$id] = $row;
            $out[$id]['name'] = self::label((string) $id);
        }

        return $out;
    }
}
