<?php

namespace Google\Web_Stories_Dependencies\AmpProject\Optimizer\Transformer;

use Google\Web_Stories_Dependencies\AmpProject\Amp;
use Google\Web_Stories_Dependencies\AmpProject\Html\Attribute;
use Google\Web_Stories_Dependencies\AmpProject\Dom\Document;
use Google\Web_Stories_Dependencies\AmpProject\Dom\Element;
use Google\Web_Stories_Dependencies\AmpProject\Dom\NodeWalker;
use Google\Web_Stories_Dependencies\AmpProject\Extension;
use Google\Web_Stories_Dependencies\AmpProject\Optimizer\Configuration\OptimizeAmpBindConfiguration;
use Google\Web_Stories_Dependencies\AmpProject\Optimizer\ErrorCollection;
use Google\Web_Stories_Dependencies\AmpProject\Optimizer\Transformer;
use Google\Web_Stories_Dependencies\AmpProject\Optimizer\TransformerConfiguration;
use Google\Web_Stories_Dependencies\AmpProject\Html\Tag;
use DOMAttr;
/**
 * OptimizeAmpBind - inject a querySelectorAll query-able i-amphtml-binding attribute on elements with bindings.
 *
 * This is ported from the NodeJS optimizer.
 *
 * NodeJS:
 *
 * @version 4aa99eb6e16a39bb562acb67efdfd3ee3d993a98
 * @link https://github.com/ampproject/amp-toolbox/blob/4aa99eb6e16a39bb562acb67efdfd3ee3d993a98/packages/optimizer/lib/transformers/OptimizeAmpBind.js
 *
 * @package ampproject/amp-toolbox
 */
final class OptimizeAmpBind implements Transformer
{
    /**
     * Configuration store to use.
     *
     * @var TransformerConfiguration
     */
    private $configuration;
    /**
     * Instantiate an OptimizeAmpBind object.
     *
     * @param TransformerConfiguration $configuration Configuration store to use.
     */
    public function __construct(TransformerConfiguration $configuration)
    {
        $this->configuration = $configuration;
    }
    /**
     * Apply transformations to the provided DOM document.
     *
     * @param Document        $document DOM document to apply the transformations to.
     * @param ErrorCollection $errors   Collection of errors that are collected during transformation.
     * @return void
     */
    public function transform(Document $document, ErrorCollection $errors)
    {
        if ($this->configuration->get(OptimizeAmpBindConfiguration::ENABLED) === \false) {
            return;
        }
        if (!$this->hasAmpBindScriptElement($document)) {
            return;
        }
        $document->html->addBooleanAttribute(Attribute::I_AMPHTML_BINDING);
        for ($node = $document->html; $node !== null; $node = NodeWalker::nextNode($node)) {
            if (!$node instanceof Element) {
                continue;
            }
            if (Amp::isTemplate($node)) {
                $node = NodeWalker::skipNodeAndChildren($node);
                continue;
            }
            /** @var DOMAttr $attribute */
            foreach ($node->attributes as $attribute) {
                if (\strpos($attribute->name, Amp::BIND_DATA_ATTR_PREFIX) === 0) {
                    $node->addBooleanAttribute(Attribute::I_AMPHTML_BINDING);
                    break;
                }
            }
        }
    }
    /**
     * Check whether the document has an amp-bind script element.
     *
     * @param Document $document Document to check.
     * @return bool Whether the document has an amp-bind script element.
     */
    private function hasAmpBindScriptElement(Document $document)
    {
        for ($element = $document->head->firstChild; $element !== null; $element = $element->nextSibling) {
            if (!$element instanceof Element) {
                continue;
            }
            if ($element->tagName !== Tag::SCRIPT) {
                continue;
            }
            if ($element->getAttribute(Attribute::CUSTOM_ELEMENT) !== Extension::BIND) {
                continue;
            }
            return \true;
        }
        return \false;
    }
}
