<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields;

use DropshippingExportFreeVendor\WPDesk\Forms\Field\HiddenField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\NoOnceField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\SubmitField;
use DropshippingExportFreeVendor\WPDesk\Forms\FieldProvider;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\SelectField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\PriceCompareFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductTypes\SingleProductType;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\Field\SelectOptField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
class ExportFileTypeFormFields implements FieldProvider
{
    public const PRODUCT_TYPE = 'product_type';
    public const EXPORT_TYPE = 'export_type';
    public const NEXT_STEP = 'next_step';
    public const NONCE_ACTION = 'export_type_action';
    public const NONCE_NAME = 'export_type_nonce';
    public const UID = 'uid';
    private PriceCompareFactory $price_compare_factory;
    public function __construct(PriceCompareFactory $price_compare_factory)
    {
        $this->price_compare_factory = $price_compare_factory;
    }
    /**
     * @see FieldProvider::get_fields()
     */
    public function get_fields()
    {
        return [(new SelectField())->set_label('')->set_name(self::PRODUCT_TYPE)->add_class('dropshipping-select2 width-100')->set_multiple()->set_default_value(\array_keys(self::get_product_types()))->set_options(self::get_product_types()), (new SelectOptField())->set_label('')->set_name(self::EXPORT_TYPE)->add_class('dropshipping-select2 width-100')->set_options(self::get_export_types()), (new SubmitField())->set_name(self::NEXT_STEP)->set_label(__('Go to the next step &rarr;', 'dropshipping-export-free'))->add_class('button button-primary button-hero')->set_attribute('id', self::NEXT_STEP), (new HiddenField())->set_name(self::UID)->set_default_value($this->get_uid()), (new NoOnceField(self::NONCE_ACTION))->set_name(self::NONCE_NAME)];
    }
    private function get_export_types(): array
    {
        $result = [];
        $configs = $this->price_compare_factory->get_configs();
        foreach ($configs as $config) {
            if ($config instanceof PriceCompareConfig) {
                if ($config->is_custom()) {
                    $result[__('Custom format', 'dropshipping-export-free')][$config->get_id()] = $config->get_name();
                } else {
                    $result[__('Price Compare', 'dropshipping-export-free')][$config->get_id()] = $config->get_name();
                }
            }
        }
        return $result;
    }
    private function get_uid(): string
    {
        return \uniqid();
    }
    public static function get_product_types(): array
    {
        return [SingleProductType::PRODUCT_TYPE_SIMPLE => __('Simple product', 'dropshipping-export-free'), SingleProductType::PRODUCT_TYPE_VARIABLE => __('Variable product', 'dropshipping-export-free'), SingleProductType::PRODUCT_TYPE_GROUPED => __('Grouped product', 'dropshipping-export-free'), SingleProductType::PRODUCT_TYPE_EXTERNAL => __('External product', 'dropshipping-export-free')];
    }
}
