<?php

namespace Lucent\Query\BuilderConverter;

use Illuminate\Database\Query\Builder;
use Lucent\Query\Filter\Argument;

readonly class NotIn implements IBuilderConverter
{

    public function __construct(private Argument $argument)
    {
    }

    public function toAndQueryBuilder(Builder $builder): Builder
    {
        return $builder->whereNotIn($this->argument->field, $this->formatValue());
    }

    public function toOrQueryBuilder(Builder $builder): Builder
    {
        return $builder->orWhereNotIn($this->argument->field, $this->formatValue());
    }

    private function formatValue(): array
    {
        $value = $this->argument->value;
        if (is_string($value)) {
            $value = explode(",", $value);
        }
        return array_map(fn($v) => trim($v), $value);
    }
}