<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\Custom;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldXml;
use DOMElement;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\CustomProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldCsv;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\ResponsibleProducersProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Integration\GpsrIntegration;
class ResponsibleProducerField extends CustomProductField implements ProductField, ProductFieldXml, ProductFieldCsv
{
    public const SINGLE_ATTR_TAG = 'item';
    public const SINGLE_ATTR_TAG_NAME = 'name';
    public const ATTRIBUTE_KEY_SEPARATOR = ': ';
    public const ATTRIBUTE_GLUE = \PHP_EOL;
    private string $field_name;
    private ProductFieldValue $product_value;
    public function __construct(ProductFieldValue $product_value)
    {
        $this->product_value = $product_value;
    }
    public static function is_optional(): bool
    {
        return \true;
    }
    public static function get_default_class_value(): string
    {
        return ResponsibleProducersProductFieldValue::class;
    }
    public static function get_default_field_name(): string
    {
        return 'GpsrProducer';
    }
    public static function get_default_field_nice_name(): string
    {
        return __('Responsible Producer', 'dropshipping-export-free');
    }
    public function get_field_name(): string
    {
        $default = self::get_default_field_name();
        return !empty($this->field_name) ? $this->field_name : $default;
    }
    public function set_field_name(string $string)
    {
        $this->field_name = trim($string);
    }
    public function map_xml_value(DOMElement $element): DOMElement
    {
        $root = $element->ownerDocument;
        $items = $this->get_values_as_array($this->product_value);
        if (\is_array($items)) {
            $tag = $element->appendChild($root->createElement($this->change_node_name($this->get_field_name())));
            foreach ($items as $key => $value) {
                if (empty($key)) {
                    continue;
                }
                $a = $tag->appendChild($root->createElement(self::SINGLE_ATTR_TAG));
                $a->setAttribute(self::SINGLE_ATTR_TAG_NAME, $this->strip_invalid_string($this->get_key_translation($key)));
                if (!empty($value)) {
                    $a->appendChild($root->createCDATASection($this->strip_invalid_string($value)));
                }
            }
        }
        return $element;
    }
    public function map_csv_value(array $csv_data): array
    {
        $items = $this->get_values_as_array($this->product_value);
        $string_value = '';
        if (\is_array($items)) {
            $result = [];
            foreach ($items as $key => $value) {
                $result[] = $this->get_key_translation($key) . self::ATTRIBUTE_KEY_SEPARATOR . $value;
            }
            $string_value = implode(self::ATTRIBUTE_GLUE, $result);
        }
        $csv_data[$this->get_field_name()] = $string_value;
        return $csv_data;
    }
    private function get_key_translation(string $key): string
    {
        $translations = [GpsrIntegration::FIELD_NAME => __('Name', 'dropshipping-export-free'), GpsrIntegration::FIELD_COUNTRY => __('Country', 'dropshipping-export-free'), GpsrIntegration::FIELD_CITY => __('City', 'dropshipping-export-free'), GpsrIntegration::FIELD_POSTCODE => __('Postcode', 'dropshipping-export-free'), GpsrIntegration::FIELD_ADDRESS => __('Address', 'dropshipping-export-free'), GpsrIntegration::FIELD_EMAIL => __('Email', 'dropshipping-export-free'), GpsrIntegration::FIELD_PHONE => __('Phone', 'dropshipping-export-free')];
        return isset($translations[$key]) ? $translations[$key] : $key;
    }
}
