<?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\Integration\GpsrIntegration;
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\SafetyInstructionsProductFieldValue;
class SafetyInformationProductField extends CustomProductField implements ProductField, ProductFieldXml, ProductFieldCsv
{
    public const SINGLE_ATTR_TAG = 'item';
    public const SINGLE_ATTR_TYPE = 'type';
    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 SafetyInstructionsProductFieldValue::class;
    }
    public static function get_default_field_name(): string
    {
        return 'SafetyInformation';
    }
    public static function get_default_field_nice_name(): string
    {
        return __('Safety Information', '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;
        $values = $this->get_values_as_array($this->product_value);
        if (\is_array($values)) {
            $tag = $element->appendChild($root->createElement($this->change_node_name($this->get_field_name())));
            $item = $tag->appendChild($root->createElement(self::SINGLE_ATTR_TAG));
            $type = !empty($values[GpsrIntegration::FIELD_INSTRUCTIONS_TYPE]) ? $values[GpsrIntegration::FIELD_INSTRUCTIONS_TYPE] : GpsrIntegration::VALUE_INSTRUCTIONS_TEXT;
            $item->setAttribute(self::SINGLE_ATTR_TYPE, $this->strip_invalid_string($type));
            $item->appendChild($root->createCDATASection($this->strip_invalid_string($values[GpsrIntegration::FIELD_INSTRUCTIONS_DATA] ?? '')));
        }
        return $element;
    }
    public function map_csv_value(array $csv_data): array
    {
        $values = $this->get_values_as_array($this->product_value);
        $value = '';
        if (\is_array($values)) {
            if (isset($values[GpsrIntegration::FIELD_INSTRUCTIONS_TYPE]) && isset($values[GpsrIntegration::FIELD_INSTRUCTIONS_DATA])) {
                $value = $values[GpsrIntegration::FIELD_INSTRUCTIONS_DATA];
            }
        }
        $csv_data[$this->get_field_name()] = $value ?? '';
        return $csv_data;
    }
}
