<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Resolver;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataFormatValidatorFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\File\FileObject;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Data\DataFormat;
/**
 * Class FileFormatResolverService
 *
 * @package WPDesk\Library\DropshippingExport\Service\Resolver
 */
class FileFormatResolverService
{
    /**
     * @var DataFormatValidatorFactory
     */
    private $validator_factory;
    /**
     * @var DataFormat
     */
    private $data_format;
    public function __construct(DataFormatValidatorFactory $factory, DataFormat $data_format)
    {
        $this->validator_factory = $factory;
        $this->data_format = $data_format;
    }
    public function resolve_data_format(FileObject $file)
    {
        try {
            $mime_types = DataFormat::XML_MIME_TYPES;
            $mime_type = reset($mime_types);
            if (!$this->is_valid($file, $mime_type)) {
                $mime_type = $file->getMimeType();
            }
        } catch (\Exception $e) {
            $mime_type = $file->getMimeType();
        }
        return $this->data_format->get_format_from_mime_type($mime_type);
    }
    public function is_valid(FileObject $file, string $mime_type): bool
    {
        $validator = $this->validator_factory->create_validator($mime_type);
        return $validator->is_file_valid($file);
    }
}
