<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldXml;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DOMElement;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\CustomProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\GmcShippingProductFieldValue;
class ShippingProductField extends CustomProductField implements ProductField, ProductFieldXml
{
    public const TAG_NAME = 'g:shipping';
    private string $field_name;
    private ProductFieldValue $product_value;
    public function __construct(ProductFieldValue $product_value)
    {
        $this->product_value = $product_value;
    }
    public static function get_default_class_value(): string
    {
        return GmcShippingProductFieldValue::class;
    }
    public static function is_optional(): bool
    {
        return \true;
    }
    public static function is_allowed_to_change(): bool
    {
        return \false;
    }
    public static function get_default_field_name(): string
    {
        return 'shipping';
    }
    public static function get_default_field_nice_name(): string
    {
        return sprintf(esc_html__('Shipping %s', 'dropshipping-export-free'), '(' . self::TAG_NAME . ')');
    }
    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)) {
            foreach ($values as $row) {
                $shipping = $element->appendChild($root->createElement(self::TAG_NAME));
                foreach ($row as $key => $value) {
                    $g = $shipping->appendChild($root->createElement('g:' . $key));
                    $g->appendChild($root->createCDATASection($value));
                }
            }
        }
        return $element;
    }
}
