<?php

namespace Planaday\Html\Parsers\NodeParsers;

use DOMNode;
use Planaday\Html\Nodes\NodeData;
use Planaday\Html\Nodes\ParserNode;
use Planaday\Utility\Debug\Log\Logging;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;

#[Scoped]
class ConfigInjector implements NodeParser {
	use Logging;

	public function __construct() {}

	protected NodeData $nodeData;

	public function setNodeData( NodeData $nodeData ): void {
		$this->nodeData = $nodeData;
	}

	public function modifyNode( DOMNode&ParserNode $node ): void {
		$tag = $node->getAssociatedTag();

		if ( ! isset( $this->nodeData )  ) {
			return;
		}

		if ( $tag === null ){
			return;
		}

		$config = $tag->getConfig();
		if ( empty( $config ) ) {
			return;
		}

		$this->logDebug( $node->nodeName . " Tag has config, injecting config");
		$this->nodeData->data["config"] = $config;
	}
}