<?php

namespace AutoAdmin\Bundle\Media\Namer;

use AutoAdmin\Bundle\Core\Slugger\Slugger;
use AutoAdmin\Bundle\Media\Helper\FileHelper;

class DefaultNamer implements NamerInterface
{
    private Slugger $slugger;

    public function __construct(Slugger $slugger)
    {
        $this->slugger = $slugger;
    }

    public function generateName(string $originalName): string
    {
        $extension = FileHelper::extractExtension($originalName);
        $fileName = FileHelper::extractName($originalName);
        $fileName = $this->slugger->slugify($fileName);

        return substr(md5(uniqid('', true)), 0, 8) . '-' . $fileName . '.' . $extension;
    }
}
