<?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 GeneralTagDataInjector implements NodeParser {
	use Logging;
	protected NodeData $nodeData;

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

	public function modifyNode( DOMNode&ParserNode $node ): void {
		if ( ! isset( $this->nodeData )  ) {
			return;
		}

		$tag = $node->getAssociatedTag();

		if ( $tag !== null ) {
			$currentTag = [
				'__DIR__' => $tag->getTagDir(),
				'title' => $tag->getTagTitle(),
				'class' => get_class($tag),
				'template' => $tag->getCurrentTemplateName(),
			];

			$this->nodeData->data['autoGenerated'] ??= [];
			$this->nodeData->data['autoGenerated']['currentTag'] = $currentTag;
			$this->nodeData->data['autoGenerated']['tagHistory'][] = $currentTag;
		}
	}
}