<?php

namespace App\Modules\Shared\Domain\QueryBuilder\Operator;

class OperatorOr implements Operator
{
    private array $others;
    public function __construct(
        Operator ...$elements,
    ) {
        $this->others = $elements;
    }

    public function append(Operator $operator)
    {
        $this->others[] = $operator;
    }


    public function query(): string
    {
        $values = [...$this->others];
        return '(' . (count($values) >= 1 ? implode(' OR ', array_map(fn(Operator $operator) => '(' . $operator->query() . ')', $values)) : '1') . ')';
    }

    public function values(): array
    {
        return [...array_reduce($this->others, fn(array $carry, Operator $operator) => [...$carry, ...$operator->values()], [])];
    }
}
