<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
/*
Below commit was igrnored. Tabs visibility controlled via css in theme.
https://github.com/magento/magento2/commit/af55d2be15f0f0e25cd2629a2d6907181fde4a2f
*/
use Magento\Catalog\Block\Product\View\Details;

$theme_settings = $this->helper( Olegnax\Athlete2\Helper\Helper::Class )->getConfig( 'athlete2_settings/product' );
$args = $attrContent = $attrtitle = $classTitle ='';
if( $theme_settings[ 'product_tabs_accordion' ] ) {
	$tabType = 'accordion';
	if(!$theme_settings[ 'product_tabs_closed' ] && $theme_settings[ 'product_tabs_opened' ]){
		$attrContent = ' aria-hidden="false"';
		$attrtitle = ' aria-selected="true" aria-expanded="true"';
		$classTitle = 'active';
	}
} else {
	$tabType = 'tabs';
	$classTitle = 'active';
}
$indexes = [];
$i=0;
/** @var Details $block */
?>
<?php if ($detailedInfoGroup = $block->getGroupSortedChildNames('detailed_info', 'getChildHtml')): ?>
	<div class="product info detailed">
		<?php $layout = $block->getLayout(); ?>
		<div class="product data items js-mage-tabs <?= $tabType ?>">
			<?php foreach ($detailedInfoGroup as $name): ?>
				<?php
                $html = (string)$layout->renderElement($name);
                if (!$html || !trim($html)) {
                    continue;
                }
				/* set first tab as opened by default to prevent CLS */
				if($tabType === 'tabs'){
					$classTitle = ($i == 0) ? 'active' : '';
				}

				$indexes[] = $i;
				$i++;
                $alias = $layout->getElementAlias($name);
                $label = $block->getChildData($alias, 'title');

                ?>
				<div class="data item title <?php echo $classTitle ?>"  
					<?php echo $attrtitle ?>
					data-role="collapsible" id="tab-label-<?= $block->escapeHtmlAttr($alias) ?>">
					<a class="data switch"
					   tabindex="-1"
					   data-toggle="trigger"
					   href="#<?= $block->escapeUrl($alias) ?>"
					   id="tab-label-<?= $block->escapeHtmlAttr($alias) ?>-title">
						<?= /* @noEscape */ $label ?>
					</a>
				</div>
				<div class="data item content" 
					<?php echo $attrContent ?>
					 aria-labelledby="tab-label-<?= $block->escapeHtmlAttr($alias) ?>-title" id="<?= $block->escapeHtmlAttr($alias) ?>" data-role="content">
					<?= /* @noEscape */ $html ?>
				</div>
			<?php endforeach; ?>
		</div>
	</div>
<?php endif; ?>

<?php
if( $theme_settings[ 'product_tabs_accordion' ] ) {
	if($theme_settings[ 'product_tabs_closed' ]){
		$args .= ',"active":false';
	} elseif($theme_settings[ 'product_tabs_opened' ]){
		if(is_array($indexes) && !empty($indexes)){
			$args .= ',"active":[' . implode(',', $indexes ) . ']';
		}
	}
	$args .= $theme_settings[ 'product_accordion_multiple' ] ? ',"multipleCollapsible": "true"' : '';
	$args .= $theme_settings[ 'product_accordion_collapsable' ] ? ',"collapsible":"true"' : '';
}
?>
<script type="text/x-magento-init">{".js-mage-tabs":{"<?= $tabType ?>":{"openedState":"active"<?= $args ?>}}}</script>