<?php

declare(strict_types=1);

namespace apexl\Io\modules\tabulator\Tabulatable\LoaderConditionCallback;

use apexl\Io\collections\LoaderConditionCollection;
use apexl\Io\dto\LoaderCondition;
use apexl\Io\interfaces\LoaderConditionInterface;
use apexl\Io\modules\tabulator\Dto\TabulatorFilter;
use apexl\Io\modules\tabulator\Interfaces\TabulatableLoaderConditionCallback;

final readonly class Truthy implements TabulatableLoaderConditionCallback
{
    public function __invoke(
        TabulatorFilter $filter,
    ): LoaderConditionInterface {

        if ($filter->value === false) {
            return LoaderConditionCollection::createFromArray([
                LoaderCondition::or(LoaderCondition::isNull($filter->field)),
                LoaderCondition::or(LoaderCondition::equals($filter->field, false)),
            ]);
        }

        return LoaderConditionCollection::createFromArray([
            LoaderCondition::notIn($filter->field, [true, '1']),
            LoaderCondition::notNull($filter->field),
        ]);
    }
}
