<?php

namespace App\Domain\Scraping\Domain\Model\ValueObject;

use InvalidArgumentException;

class ChapterUrl
{
    private string $chapterUrlFormat;
    private string $mangaSlug;
    private float $chapterNumber;

    public function __construct(
        string $chapterUrlFormat,
        string $mangaSlug,
        float $chapterNumber
    ) {
        $this->chapterUrlFormat = $this->validateUrlFormat($chapterUrlFormat);
        $this->mangaSlug = $mangaSlug;
        $this->chapterNumber = $chapterNumber;
    }

    public function getUrl(): string
    {
        $placeholders = [
            '{chapterNumber}' => $this->chapterNumber,
            '{slug}' => $this->mangaSlug,
        ];

        return str_replace(array_keys($placeholders), array_values($placeholders), $this->chapterUrlFormat);
    }

    private function validateUrlFormat(string $format): string
    {
        if (!str_contains($format, '{slug}')) {
            throw new InvalidArgumentException("The URL format must contain {slug} placeholder.");
        }

        return $format;
    }

}
