<?php

declare(strict_types=1);

/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Lazy Load for Magento 2 (System)
 */

namespace Amasty\LazyLoad\Plugin\BannerSlider\Model\ImageProcessor;

use Amasty\BannerSlider\Api\Data\BannerInterface;
use Amasty\BannerSlider\Api\Data\SliderInterface;
use Amasty\BannerSlider\Model\ImageProcessor;
use Amasty\LazyLoad\Model\Output\LazyConfig\LazyConfig;
use Amasty\LazyLoad\Plugin\Catalog\Block\Product\View\Gallery\ProductGalleryReplace;
use Amasty\PageSpeedTools\Model\Image\ReplaceByPatternApplier;

class ReplaceBannerSlider
{
    /**
     * @var ReplaceByPatternApplier
     */
    private ReplaceByPatternApplier $replaceByPatternApplier;

    /**
     * @var LazyConfig
     */
    private LazyConfig $lazyConfig;

    public function __construct(
        ReplaceByPatternApplier $replaceByPatternApplier,
        LazyConfig $lazyConfig
    ) {
        $this->replaceByPatternApplier = $replaceByPatternApplier;
        $this->lazyConfig = $lazyConfig;
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetImages(
        ImageProcessor $subject,
        array $result,
        BannerInterface $banner,
        SliderInterface $slider
    ): array {
        if (!empty($result) && $this->lazyConfig->getData(LazyConfig::IS_LAZY)) {
            $this->replaceByPatternApplier->execute(
                ProductGalleryReplace::REPLACE_PATTERN_GROUP,
                $result
            );
        }

        return $result;
    }
}
