<?php

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

use DropshippingExportFreeVendor\WPDesk\Forms\FieldProvider;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\SelectField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\SubmitField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\CategoryNokautDataProvider;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\NoOnceField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\Config\NokautConfig;
class CategoriesNokautFormFields implements FieldProvider
{
    public const NONCE_ACTION = 'settings_categories_nokaut_action';
    public const NONCE_NAME = 'settings_categories_nokaut_nonce';
    public const FILED_CATEGORY = 'category';
    public const FILED_SUBMIT = 'save';
    public const CATEGORY_DEFAULT_VALUE = '';
    private CategoryNokautDataProvider $data_provider;
    private NokautConfig $nokaut;
    public function __construct(CategoryNokautDataProvider $data_provider, NokautConfig $nokaut)
    {
        $this->data_provider = $data_provider;
        $this->nokaut = $nokaut;
    }
    /**
     * @see FieldProvider::get_fields()
     */
    public function get_fields()
    {
        $result = [];
        $categories = \get_terms('product_cat');
        foreach ($categories as $category) {
            $val = null;
            $obj = (new SelectField())->set_label($category->name)->set_attribute('id', self::FILED_CATEGORY . '_' . $category->term_id)->set_attribute('data-item', NokautConfig::ID)->set_name($category->term_id)->add_class('dropshipping-select2-autocomplete');
            $options = [self::CATEGORY_DEFAULT_VALUE => \esc_attr__('Select value', 'dropshipping-export-free')];
            if ($this->data_provider->has($category->term_id)) {
                $val = $this->data_provider->get($category->term_id);
                $options[$val] = $this->nokaut->get_category()->get_category_full_name((int) $val);
            }
            if (null === $val) {
                $val = self::CATEGORY_DEFAULT_VALUE;
            }
            $obj->set_options($options);
            $obj->set_default_value($val);
            $result[] = $obj;
        }
        $result[] = (new NoOnceField(self::NONCE_ACTION))->set_name(self::NONCE_NAME);
        $result[] = (new SubmitField())->set_name(self::FILED_SUBMIT)->set_label(__('Apply settings', 'dropshipping-export-free'))->add_class('button button-hero button-primary')->set_attribute('id', self::FILED_SUBMIT);
        return $result;
    }
}
