<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldXml;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductField;
use DOMElement;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\CustomProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldCsv;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\IdProductFieldValue;
class IdProductField extends CustomProductField implements ProductField, ProductFieldXml, ProductFieldCsv
{
    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 IdProductFieldValue::class;
    }
    public static function get_default_field_name(): string
    {
        return 'ID';
    }
    public static function get_default_field_nice_name(): string
    {
        return __('Product ID', '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;
        $value = $this->get_values_as_int($this->product_value);
        if (\is_int($value)) {
            $tag = $element->appendChild($root->createElement($this->change_node_name($this->get_field_name())));
            $tag->appendChild($root->createCDATASection((string) $value));
        }
        return $element;
    }
    public function map_csv_value(array $csv_data): array
    {
        $value = $this->get_values_as_int($this->product_value);
        $csv_data[$this->get_field_name()] = $value ?? '';
        return $csv_data;
    }
}
