<?php

declare(strict_types=1);

namespace App\Framework\Vite\ValueObjects;

/**
 * Value Object representing a single Vite manifest entry
 *
 * Represents an asset entry from Vite's manifest.json file,
 * including its compiled file path, source, dependencies, and metadata.
 */
final readonly class ViteEntry
{
    /**
     * @param string $file Compiled output file path (e.g., "assets/main-abc123.js")
     * @param string $src Source file path (e.g., "resources/js/main.js")
     * @param bool $isEntry Whether this is an entry point file
     * @param array<string> $css Associated CSS files
     * @param array<string> $imports Import dependencies (other entry names)
     * @param string|null $integrity Subresource integrity hash
     */
    public function __construct(
        public string $file,
        public string $src,
        public bool $isEntry,
        public array $css = [],
        public array $imports = [],
        public ?string $integrity = null
    ) {
    }

    /**
     * Create ViteEntry from manifest entry array
     */
    public static function fromManifestEntry(string $name, array $entry): self
    {
        return new self(
            file: $entry['file'] ?? '',
            src: $entry['src'] ?? $name,
            isEntry: $entry['isEntry'] ?? false,
            css: $entry['css'] ?? [],
            imports: $entry['imports'] ?? [],
            integrity: $entry['integrity'] ?? null
        );
    }

    /**
     * Get the full URL path for this entry
     */
    public function getUrl(string $baseUrl = ''): string
    {
        return rtrim($baseUrl, '/') . '/' . ltrim($this->file, '/');
    }

    /**
     * Check if this entry has CSS dependencies
     */
    public function hasCss(): bool
    {
        return ! empty($this->css);
    }

    /**
     * Check if this entry has import dependencies
     */
    public function hasImports(): bool
    {
        return ! empty($this->imports);
    }
}
