<?php

declare(strict_types=1);

namespace KupShop\CatalogBundle\Enum\Reviews;

use KupShop\CatalogBundle\Enum\OrderDirection;

final readonly class ReviewsOrdering
{
    public function __construct(
        public ReviewsOrder $orderBy,
        public OrderDirection $orderDir = OrderDirection::ASC,
    ) {
    }

    public static function fromString(string $order): ?self
    {
        if (!($orderBy = ReviewsOrder::tryFrom(ltrim($order, '-')))) {
            return null;
        }

        $orderDir = str_starts_with($order, '-')
            ? OrderDirection::DESC
            : OrderDirection::ASC;

        return new self($orderBy, $orderDir);
    }
}
