<?php

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

use DOMElement;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductField;
use WC_Product;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\CustomProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldCsv;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\ImageProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldXml;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldImgLocationValue;
class ImagesProductField extends CustomProductField implements ProductField, ProductFieldXml, ProductFieldCsv, ProductFieldImgLocationValue
{
    public const PARENT_FIELD_NAME = 'imgs';
    private $field_name;
    const SEPARATOR = ',';
    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 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())));
            $tag->appendChild($root->createCDATASection(\implode(self::SEPARATOR, $values)));
        }
        return $element;
    }
    public static function get_default_class_value(): string
    {
        return ImageProductFieldValue::class;
    }
    public static function get_default_field_name(): string
    {
        return 'ProductImages';
    }
    public static function get_default_field_nice_name(): string
    {
        return __('Product Images', '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 get_file_locations(): ?array
    {
        if ($this->product_value instanceof ProductFieldImgLocationValue) {
            return $this->product_value->get_file_locations();
        }
        return null;
    }
    public function map_csv_value(array $csv_data): array
    {
        $value = '';
        $values = $this->get_values_as_array($this->product_value);
        if (\is_array($values)) {
            $value = \implode(self::SEPARATOR, $values);
        }
        $csv_data[$this->get_field_name()] = $value ?? '';
        return $csv_data;
    }
}
