<?php

declare(strict_types=1);

namespace apexl\Io\services;

use apexl\Io\collections\LoaderSortCollection;
use apexl\Vault\enums\SortDirection;

final readonly class LoaderSortResolver
{
    public function resolve(array|string|LoaderSortCollection $sorts): LoaderSortCollection
    {
        if ($sorts instanceof LoaderSortCollection) {
            return $sorts->unique();
        }

        if (is_string($sorts)) {
            $sorts = [$sorts];
        }

        if (is_array($sorts)) {
            if ($sorts[1] ?? null instanceof SortDirection) {
                $sorts = [$sorts];
            }

            $sorts = LoaderSortCollection::createFromArray($sorts);
        }

        return $sorts->unique();
    }
}