<?php
declare(strict_types=1);

namespace FreshCloud\Mail\BaseUtils;

final readonly class StringList
{
    public function __construct(
        public array $items = []
    ) {
        foreach ($items as $item) {
            if (!is_string($item)) {
                throw new InvalidArgumentException('All items must be strings');
            }
        }
    }

    public function add(string $item): self
    {
        $items = $this->items;
        $items[] = $item;
        return new self($items);
    }

    public function remove(string $item): self
    {
        $items = array_diff($this->items, [$item]);
        return new self(array_values($items));
    }

    public function contains(string $item): bool
    {
        return in_array($item, $this->items, true);
    }

    public function __toString(): string
    {
        return implode(', ', $this->items);
    }
}