<?php

declare(strict_types=1);

namespace App\Application\Search\ValueObjects;

final readonly class HighlightFields
{
    /**
     * @param array<int, string> $fields
     */
    public function __construct(
        private array $fields = []
    ) {
    }

    /**
     * @param array<int, string> $fields
     */
    public static function fromArray(array $fields): self
    {
        return new self($fields);
    }

    public static function fromString(string $fieldsString): self
    {
        $fields = array_map('trim', explode(',', $fieldsString));

        return new self($fields);
    }

    public static function default(): self
    {
        return new self(['title', 'content', 'description']);
    }

    public function contains(string $field): bool
    {
        return in_array($field, $this->fields);
    }

    public function isEmpty(): bool
    {
        return empty($this->fields);
    }

    /**
     * @return array<int, string>
     */
    public function toArray(): array
    {
        return $this->fields;
    }

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