{block name='snippets-categories-mega-recursive'}
    {block name='snippets-categories-mega-recursive-assigns'}
        {$autoScrollTarget=$autoScrollTarget|default:''}
        {if isset($activeParents) && is_array($activeParents) && isset($activeParents[$currentLevel])}
            {assign var=activeParent value=$activeParents[$currentLevel]}
        {/if}
        {block name='snippets-categories-mega-recursive-max-subsub-items'}
            {$maxLevel=$maxLevel|default:3}
        {/block}
        {$forceOpen=$forceOpen|default:false}
        {$categoryTreeMoreChildrenLimit=$categoryTreeMoreChildrenLimit|default:[]}
    {/block}
    {block name='snippets-categories-mega-recursive-main-link'}
        {if $mainCategory->getID() === $activeId
        || ((isset($activeParent)
        && isset($activeParent->getID()))
        && $activeParent->getID() == $mainCategory->getID())}
            {assign var='isActive' value=true}
        {else}
            {assign var='isActive' value=false}
        {/if}
        {if $mainCategory->getID() === $activeId}
            {assign var='isCurrent' value=true}
            {else}
            {assign var='isCurrent' value=false}
        {/if}
        <a href="{if $mainCategory->hasChildren() && $currentLevel < $maxLevel && !$forceOpen}#category-mega-collapse-{$mainCategory->getID()}{else}{$mainCategory->getURL()}{if !empty($autoScrollTarget)}{$autoScrollTarget}{/if}{/if}"
            class="link-discreet et-categories-level-{$currentLevel}{if $mainCategory->hasChildren() && $currentLevel < $maxLevel} et-categories-mega-collapse et-collapse{else} et-categories-mega-link{/if}{if $isActive} active{/if}{if $isCurrent} current{/if}"
           {if $mainCategory->hasChildren() && $currentLevel < $maxLevel && !$forceOpen} aria-expanded="{if $isActive}true{else}false{/if}" aria-controls="category-mega-collapse-{$mainCategory->getID()}" data-toggle="collapse"{/if}
        >
            <span class="text-truncate">
                {$mainCategory->getShortName()}{if $mainCategory->hasChildren() && $currentLevel >= $maxLevel}<span class="more-subcategories">&nbsp;({count($mainCategory->getChildren())})</span>{/if}
            </span>
            {if $mainCategory->hasChildren() && $currentLevel < $maxLevel && !$forceOpen}
                {block name='snippets-categories-mega-recursive-main-link-icons'}
                    <i class='{et_getIcon key="collapseOpen"} et-collapse-open'></i>
                    <i class='{et_getIcon key="collapseClosed"} et-collapse-closed'></i>
                {/block}
            {/if}
        </a>
    {/block}
    {if $mainCategory->hasChildren() && $currentLevel < $maxLevel}
        {block name='snippets-categories-mega-recursive-child-content'}
            <div class="{if !$forceOpen}collapse{if $isActive} show{/if}{/if}" id="category-mega-collapse-{$mainCategory->getID()}">
                <div class="subcategory-wrapper">
                    {if !$forceOpen}
                        <a href="{$mainCategory->getURL()}{if !empty($autoScrollTarget)}{$autoScrollTarget}{/if}" title="{$mainCategory->getName()|escape:'html'}" class="et-categories-mega-show-all link-discreet">
                            <span class="text-truncate">{lang key='menuShow' printf=$mainCategory->getShortName()}</span>
                        </a>
                    {/if}
                    {block name='snippets-categories-mega-recursive-child-categories'}
                        {foreach $mainCategory->getChildren() as $category}
                            {block name='snippets-categories-mega-recursive-child-category-child'}
                                <div class="et-categories-mega-sub-category">
                                    {include file='snippets/categories_mega_recursive.tpl'
                                        mainCategory=$category
                                        activeId=$activeId
                                        activeParents=$activeParents
                                        firstChild=false
                                        maxLevel=$maxLevel
                                        autoScrollTarget=$autoScrollTarget
                                        currentLevel=$currentLevel + 1
                                        categoryTreeMoreChildrenLimit=$categoryTreeMoreChildrenLimit
                                    }
                                </div>
                            {/block}
                        {/foreach}
                    {/block}
                    {block name="et-categories-mega-recursive-more"}
                        {if !empty($categoryTreeMoreChildrenLimit)}
                            <div class="et-categories-mega-recursive-more et-item-limit-more" style="display: none">
                                <a class="link-discreet" href="javascript:void(0);" title="{lang key='showMore' section='global'}">{lang key='showMore' section='global'}</a>
                            </div>
                        {/if}
                    {/block}
                </div>
            </div>
        {/block}
    {/if}
{/block}
