<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\XML;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\Generator;
use DOMElement;
use DOMDocument;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\ExternalData;
class GmcXmlWriter extends AbstractBatchXmlWriter implements Generator, ExternalData
{
    public const PRODUCT_ROOT = 'rss';
    public const PRODUCT_ITEMS = 'channel';
    public const PRODUCT_ITEM = 'item';
    public const TAG_TITLE = 'title';
    public const TAG_LINK = 'link';
    public const TAG_DESCRIPTION = 'description';
    private array $data = [];
    public static function get_supported_extension(): string
    {
        return 'XML';
    }
    public function set_data(array $data): void
    {
        $this->data = $data;
    }
    protected function get_product_item_tag(): string
    {
        return self::PRODUCT_ITEM;
    }
    protected function get_product_items_tag(): string
    {
        return self::PRODUCT_ITEMS;
    }
    protected function prepare_root_element(): DOMElement
    {
        $xml_document = new \DOMDocument('1.0', 'UTF-8');
        $xml_document->formatOutput = \true;
        $xml_document->preserveWhiteSpace = \false;
        $root_element = $xml_document->createElement(self::PRODUCT_ROOT);
        $root_element->setAttribute('xmlns:g', 'http://base.google.com/ns/1.0');
        $root_element->setAttribute('xmlns:c', 'http://base.google.com/cns/1.0');
        $root_element->setAttribute('version', '2.0');
        $items = $root_element->appendChild($xml_document->createElement(self::PRODUCT_ITEMS));
        $title = $items->appendChild($xml_document->createElement(self::TAG_TITLE));
        $title->appendChild($xml_document->createTextNode((string) $this->data[self::TAG_TITLE] ?? ''));
        $link = $items->appendChild($xml_document->createElement(self::TAG_LINK));
        $link->appendChild($xml_document->createCDATASection((string) $this->data[self::TAG_LINK] ?? ''));
        $desc = $items->appendChild($xml_document->createElement(self::TAG_DESCRIPTION));
        $desc->appendChild($xml_document->createTextNode((string) $this->data[self::TAG_DESCRIPTION] ?? ''));
        $xml_document->appendChild($root_element);
        return $items;
    }
}
