<?php

declare(strict_types=1);

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

namespace Amasty\ImageOptimizer\Model\ImageProcessor;

use Amasty\ImageOptimizer\Api\Data\QueueInterface;
use Amasty\ImageOptimizer\Exceptions\SkippableOptimizationException;

class Strategy
{
    /**
     * @var string[]
     */
    private array $extensions;

    /**
     * @var ImageProcessorInterface[]
     */
    private array $strategy;

    public function __construct(
        array $extensions = [],
        array $strategy = []
    ) {
        $this->extensions = $extensions;
        $this->strategy = $strategy;
    }

    public function getStrategy(): array
    {
        return $this->strategy;
    }
    
    public function applyStrategies(QueueInterface $queue): array
    {
        $errors = [];

        foreach ($this->strategy as $processor) {
            try {
                $processor->process($queue);
            } catch (SkippableOptimizationException $e) {
                $errors[] = $e->getMessage();
            }
        }
        
        return $errors;
    }

    public function getExtensions(): array
    {
        return $this->extensions;
    }
}
