<?php

declare(strict_types=1);

namespace PpPhp\Core\Assets;

final class Asset
{
    public function __construct(
        public readonly string $type,
        public readonly string $path,
        public readonly string $location = 'head',
        public readonly array $attributes = [],
        public readonly int $priority = 100
    ) {}

    public static function css(string $path, array $attributes = [], int $priority = 100): self
    {
        return new self('css', $path, 'head', $attributes, $priority);
    }

    public static function js(string $path, string $location = 'footer', array $attributes = [], int $priority = 100): self
    {
        return new self('js', $path, $location, $attributes, $priority);
    }

    public static function font(string $path, array $attributes = [], int $priority = 100): self
    {
        return new self('font', $path, 'head', $attributes, $priority);
    }

    public static function image(string $path, array $attributes = [], int $priority = 100): self
    {
        return new self('image', $path, 'media', $attributes, $priority);
    }

    public static function svg(string $path, array $attributes = [], int $priority = 100): self
    {
        return new self('svg', $path, 'media', $attributes, $priority);
    }

    public static function vendor(string $path, string $location = 'footer', array $attributes = [], int $priority = 100): self
    {
        return new self('vendor', $path, $location, $attributes, $priority);
    }

    public function key(): string
    {
        return $this->type . ':' . $this->path;
    }

    public function toArray(): array
    {
        return [
            'type' => $this->type,
            'path' => $this->path,
            'location' => $this->location,
            'attributes' => $this->attributes,
            'priority' => $this->priority,
            'key' => $this->key(),
        ];
    }
}
