<?php

namespace AutoAdmin\Bundle\Media\Library;

class MediaResolver
{
    public function __construct(
        protected MediaLibrary $library,
        protected MediaPathGenerator $pathGenerator,
    ) {
    }

    public function resolve(string $stringMedia): ?object
    {
        $parts = explode('#', $stringMedia);
        if (count($parts) != 2) {
            return null;
        }

        $slug = $parts[0];
        $id = $parts[1];

        return $this->library->get($slug, $id);
    }

    public function resolveUrl(string $stringMedia): ?string
    {
        $url = null;
        $media = $this->resolve($stringMedia);
        if ($media) {
            $url = $this->pathGenerator->generateWebPath($media);
        }

        return $url;
    }
}