<?php

namespace Main\Project\Controller;

use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Error;
use Main\Project\Service\CatalogSortService;

class CatalogSortController extends Controller
{
    protected function getDefaultPreFilters(): array
    {
        return [];
    }

    public function applyAction(CatalogSortService $service): array
    {
        $request = $this->getRequest();
        $field = (string)$request->get('field');
        $order = (string)$request->get('order');

        if ($field === '') {
            $this->addError(new Error('Не указано поле сортировки'));
            return [];
        }

        $service->setSort($field, $order);

        return ['success' => true];
    }
}
