<?php

declare(strict_types=1);

namespace apexl\Io\dto\Relationship\Decorators;

use apexl\Io\interfaces\RelationOrderBy;
use apexl\Io\interfaces\RelationshipInterface;
use apexl\Vault\enums\SortDirection;

/**
 * Returns an order-by clause keyed off the relationship's aliased remote table.
 */
final readonly class OrderByRemoteField implements RelationOrderBy
{
    public function __construct(
        public string $field,
        public SortDirection $direction = SortDirection::ASC,
    ) {
    }

    public function clauses(RelationshipInterface $relationship): array
    {
        return [[sprintf('%s.%s', $relationship->getAliasedRemoteTable(), $this->field), $this->direction]];
    }
}
