<?php

namespace App\Http\Controllers\Concerns;

use Illuminate\Http\Request;

trait ParsesDataTableQuery
{
    protected function parseDataTableQuery(Request $request): array
    {
        return [
            'search' => $request->string('search')->trim()->toString(),
            'sort' => $request->string('sort')->toString(),
            'direction' => $request->string('direction')->toString() === 'desc' ? 'desc' : 'asc',
        ];
    }

    protected function dataTableFilters(array $query, array $extra = []): array
    {
        return array_merge([
            'search' => $query['search'],
            'sort' => $query['sort'],
            'direction' => $query['sort'] !== '' ? $query['direction'] : null,
        ], $extra);
    }
}
