<?php

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

use DOMElement;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\CustomProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\CustomAttrProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldXml;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldArrayValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldCsv;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\AttributesProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\Modificator\AttributesProductFieldModificator;
class AttributesProductField extends CustomProductField implements ProductField, CustomAttrProductField, ProductFieldXml, ProductFieldCsv
{
    public const SINGLE_ATTR_TAG = 'item';
    public const SINGLE_ATTR_TAG_NAME = 'name';
    public const SINGLE_ATTR_TAG_VALUE = 'value';
    private string $field_name;
    private bool $is_multiple_column = \false;
    private ProductFieldValue $product_value;
    public function __construct(ProductFieldValue $product_value)
    {
        $this->product_value = $product_value;
    }
    public static function is_optional(): bool
    {
        return \true;
    }
    public function set_as_multiple_column(bool $is_multiple): void
    {
        $this->is_multiple_column = $is_multiple;
    }
    public function map_csv_value(array $csv_data): array
    {
        if (\true === $this->is_multiple_column) {
            if ($this->product_value instanceof ProductFieldArrayValue && $this->product_value instanceof ProductFieldStringValue) {
                $attributes = $this->product_value->get_array_value();
                $string_attributes = $this->product_value->get_string_value();
                if (is_array($attributes) && is_string($string_attributes)) {
                    $attributes_keys = array_keys($attributes);
                    $items = $this->get_multiple_field_value($string_attributes);
                    foreach ($attributes_keys as $value) {
                        $csv_data[$value] = isset($items[$value]) ? $items[$value] : '';
                    }
                }
            }
        } else {
            $value = '';
            if ($this->product_value instanceof ProductFieldStringValue && $this->product_value->has_string_value()) {
                $value = $this->product_value->get_string_value();
            }
            $csv_data[$this->get_field_name()] = $value;
        }
        return $csv_data;
    }
    public static function get_default_class_value(): string
    {
        return AttributesProductFieldValue::class;
    }
    public static function get_default_field_name(): string
    {
        return 'ProductAttributes';
    }
    public static function get_default_field_nice_name(): string
    {
        return __('Product Attributes', '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;
        if (\true === $this->is_multiple_column) {
            $str_values = $this->get_values_as_string($this->product_value);
            if (\is_string($str_values)) {
                $attr = $element->appendChild($root->createElement($this->change_node_name($this->get_field_name())));
                $values = $this->get_multiple_field_value($str_values);
                foreach ($values as $key => $value) {
                    if (empty($key)) {
                        continue;
                    }
                    $a = $attr->appendChild($root->createElement(self::SINGLE_ATTR_TAG));
                    $a->setAttribute(self::SINGLE_ATTR_TAG_NAME, $this->strip_invalid_string($key));
                    $a->setAttribute(self::SINGLE_ATTR_TAG_VALUE, $this->strip_invalid_string($value));
                }
            }
        } else {
            $value = $this->get_values_as_string($this->product_value);
            if (is_string($value)) {
                $attr = $element->appendChild($root->createElement($this->change_node_name($this->get_field_name())));
                $attr->appendChild($root->createCDATASection($value));
            }
        }
        return $element;
    }
    private function get_multiple_field_value(string $attributes_string): array
    {
        $result = [];
        $attributest_separator = AttributesProductFieldModificator::ATTRIBUTES_SEPARATOR;
        $key_separator = AttributesProductFieldModificator::KEY_SEPARATOR;
        $attributes = explode($attributest_separator, $attributes_string);
        foreach ($attributes as $attribute) {
            $attribute = explode($key_separator, $attribute);
            $key = $attribute[0];
            $value = $attribute[1];
            $options = trim($value);
            $result[$key] = $options;
        }
        return $result;
    }
}
