<?php

namespace MintyPHP\Domain\Taxonomy;

trait SupportsStringTaxonomy
{
    /**
     * @return list<string>
     */
    public static function values(): array
    {
        return array_map(
            static fn (self $case): string => $case->value,
            self::cases()
        );
    }

    public static function tryNormalize(string $value): ?self
    {
        $value = strtolower(trim($value));
        if ($value === '') {
            return null;
        }

        foreach (self::cases() as $case) {
            if (strtolower($case->value) === $value) {
                return $case;
            }
        }

        return null;
    }

    public static function normalizeOr(string $value, self $fallback): self
    {
        return self::tryNormalize($value) ?? $fallback;
    }
}
