<?php

declare(strict_types=1);

namespace Gls\GlsPoland\Translation;

use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

if (
    defined('_PS_VERSION_')
    && class_exists(\Tools::class)
    && \Tools::version_compare(_PS_VERSION_, '8.0.0', '>=')
    && interface_exists(TranslatorInterface::class)
) {
    trait TranslatorAwareTrait
    {
        /**
         * @var TranslatorInterface
         */
        protected $translator;

        /**
         * @required
         */
        public function setTranslator(TranslatorInterface $translator)
        {
            $this->translator = $translator;
        }

        protected function getTranslator(): TranslatorInterface
        {
            return $this->translator ?? ($this->translator = new IdentityTranslator());
        }
    }
} else {
    trait TranslatorAwareTrait
    {
        /**
         * @var LegacyTranslatorInterface
         */
        protected $translator;

        /**
         * @required
         */
        public function setTranslator(LegacyTranslatorInterface $translator)
        {
            $this->translator = $translator;
        }

        protected function getTranslator(): LegacyTranslatorInterface
        {
            return $this->translator ?? ($this->translator = new IdentityTranslator());
        }
    }
}
