<?php

declare(strict_types=1);

namespace Loupe\Matcher;

class Locale implements \Stringable
{
    private function __construct(private readonly string $locale)
    {
    }

    public function __toString(): string
    {
        return $this->locale;
    }

    public static function fromString(string $locale): self
    {
        $canonical = \Locale::canonicalize($locale);

        if (!$canonical || '' === \Locale::getDisplayName($canonical, $canonical)) {
            throw new \InvalidArgumentException(\sprintf('Invalid locale: %s', $locale));
        }

        return new self($canonical);
    }

    public function getPrimaryLanguage(): string
    {
        return (string) \Locale::getPrimaryLanguage($this->locale);
    }

    public function matches(self $other): bool
    {
        return $other->toString() === $this->toString();
    }

    public function toString(): string
    {
        return $this->locale;
    }
}
