<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Dependency\Resolver\Abstraction\DependencyResolverInterface;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\CSV\CustomCsvWriter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Exception\WriterException;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\Generator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\XML\CeneoXmlWriter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\XML\CustomXmlWriter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\XML\GmcXmlWriter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\XML\HomebookXmlWriter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\XML\NokautXmlWriter;
class WriterFactory
{
    /**
     * @var DependencyResolverInterface
     */
    private $resolver;
    public function __construct(DependencyResolverInterface $dependency_resolver)
    {
        $this->resolver = $dependency_resolver;
    }
    public function create_writer_by_extension(string $extension): Generator
    {
        foreach ($this->get_all_writers() as $writer) {
            if ($writer::get_supported_extension() === \strtoupper($extension)) {
                return $this->resolver->resolve($writer);
            }
        }
        throw new WriterException('Extension ' . $extension . ' is not supported by the writer');
    }
    public function get_writers(): array
    {
        $result = [];
        foreach ($this->get_all_writers() as $writer) {
            $result[] = $this->resolver->resolve($writer);
        }
        return $result;
    }
    public function create_writer_by_class_name(string $class_name): Generator
    {
        foreach ($this->get_all_writers() as $writer) {
            if ($writer === $class_name) {
                return $this->resolver->resolve($writer);
            }
        }
        throw new WriterException('Class ' . $class_name . ' is not supported');
    }
    private function get_all_writers(): array
    {
        return [CustomCsvWriter::class, CustomXmlWriter::class, CeneoXmlWriter::class, NokautXmlWriter::class, HomebookXmlWriter::class, GmcXmlWriter::class];
    }
}
