<?php

namespace MintyPHP\Repository\Search;

use MintyPHP\DB;

class SearchQueryRepository implements SearchQueryRepositoryInterface
{
    /**
     * @param array<int, mixed> $params
     * @return array<int, mixed>
     */
    public function selectRows(string $sql, array $params = []): array
    {
        if (trim($sql) === '') {
            return [];
        }

        $rows = DB::select($sql, ...$params);

        return is_array($rows) ? $rows : [];
    }

    /**
     * @param array<int, mixed> $params
     */
    public function selectCount(string $sql, array $params = []): int
    {
        if (trim($sql) === '') {
            return 0;
        }

        $value = DB::selectValue($sql, ...$params);

        return (int) ($value ?? 0);
    }
}
