<?php
/**
 * @link https://craftcms.com/
 * @copyright Copyright (c) Pixel & Tonic, Inc.
 * @license https://craftcms.github.io/license/
 */

namespace craft\web\twig\tokenparsers;

use craft\web\twig\nodes\BaseNode;
use craft\web\twig\nodes\NavNode;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;

/**
 * Recursively outputs a hierarchical navigation.
 *
 * @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
 * @since 3.0.0
 */
class NavTokenParser extends AbstractTokenParser
{
    /**
     * @inheritdoc
     */
    public function getTag(): string
    {
        return 'nav';
    }

    /**
     * @inheritdoc
     */
    public function parse(Token $token): NavNode
    {
        $lineno = $token->getLine();
        $parser = $this->parser;
        $stream = $parser->getStream();

        $targets = $parser->getExpressionParser()->parseAssignmentExpression();
        $stream->expect(Token::OPERATOR_TYPE, 'in');
        $seq = $parser->getExpressionParser()->parseExpression();
        $stream->expect(Token::BLOCK_END_TYPE);

        $upperBody = $parser->subparse([$this, 'decideNavFork']);
        $lowerBody = new BaseNode();
        $indent = new BaseNode();
        $outdent = new BaseNode();

        $nextValue = $stream->next()->getValue();

        if ($nextValue !== 'endnav') {
            $stream->expect(Token::BLOCK_END_TYPE);

            if ($nextValue === 'ifchildren') {
                $indent = $parser->subparse([
                    $this,
                    'decideChildrenFork',
                ], true);
                $stream->expect(Token::BLOCK_END_TYPE);
                $outdent = $parser->subparse([
                    $this,
                    'decideChildrenEnd',
                ], true);
                $stream->expect(Token::BLOCK_END_TYPE);
            }

            $lowerBody = $parser->subparse([$this, 'decideNavEnd'], true);
        }

        $stream->expect(Token::BLOCK_END_TYPE);

        if (count($targets) > 1) {
            $keyTarget = $targets->getNode(0);
            $keyTarget = new AssignContextVariable($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
            $valueTarget = $targets->getNode(1);
            $valueTarget = new AssignContextVariable($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
        } else {
            $keyTarget = new AssignContextVariable('_key', $lineno);
            $valueTarget = $targets->getNode(0);
            $valueTarget = new AssignContextVariable($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
        }

        return new NavNode($keyTarget, $valueTarget, $seq, $upperBody, $lowerBody, $indent, $outdent, $lineno);
    }

    /**
     * @param Token $token
     * @return bool
     */
    public function decideNavFork(Token $token): bool
    {
        return $token->test(['ifchildren', 'children', 'endnav']);
    }

    /**
     * @param Token $token
     * @return bool
     */
    public function decideChildrenFork(Token $token): bool
    {
        return $token->test('children');
    }

    /**
     * @param Token $token
     * @return bool
     */
    public function decideChildrenEnd(Token $token): bool
    {
        return $token->test('endifchildren');
    }

    /**
     * @param Token $token
     * @return bool
     */
    public function decideNavEnd(Token $token): bool
    {
        return $token->test('endnav');
    }
}
