<?php

namespace Autogedal\Labels\Block\Product;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\StoreManagerInterface;

abstract class AbstractLabels extends Template
{
    private $storeManager;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->storeManager = $storeManager;
    }

    public function getMediaBaseUrl(): string
    {
        return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
    }

    public function normalizeMediaImagePath(?string $imagePath): string
    {
        $imagePath = trim((string)$imagePath);
        if ($imagePath === '') {
            return '';
        }

        return strpos($imagePath, 'autogedal_label/') === 0
            ? $imagePath
            : 'autogedal_label/' . ltrim($imagePath, '/');
    }
}
