<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile
/**
 * Currency switcher
 *
 * @var Currency $block
 */

use Magento\Directory\Block\Currency;

?>
<?php if ($block->getCurrencyCount() > 1): ?>
    <?php
    $theme_helper = $this->helper(Olegnax\Athlete2\Helper\Helper::class);
    $at_settings = $theme_helper->getConfig('athlete2_settings');

    $language_in_header = $at_settings['header']['language_in_header'];
    $language_type = $at_settings['header']['language_type'];
    $language_move_mobile = $at_settings['header']['language_move_mobile'];

    $tiny_drops = $at_settings['header']['language_tiny_drops'];
    if($block->getData('modal') !== NULL){
        $modal = $block->getData('modal');
    } else{
        $modal = !$tiny_drops && $language_type !== 'dropdown-hover';
    }
    if($block->getData('mobile_modal') !== NULL){
        $mobile_modal = $block->getData('mobile_modal');
    } else{
        $mobile_modal = !$tiny_drops && $language_in_header && !$language_move_mobile;
    }

    $classes = '';

    if( $modal ){
        $classes .= ' js-item-language-action';
    } else{
        $classes .= ' switcher-hover ';
    }
    if( !$mobile_modal ){
        $classes .= ' hide-on-mobile hide-on-tablet';
    }    
    ?>
    <?php $currencies = $block->getCurrencies(); ?>
    <?php $currentCurrencyCode = $block->getCurrentCurrencyCode(); ?>
    <?php $id = $block->getIdModifier() ? '-' . $block->getIdModifier() : '' ?>

    <div class="switcher currency switcher-currency tiny-drop tiny-arrow <?php echo $classes ?>" 
        <?php if( !$modal ): ?>
        id="switcher-currency<?= $block->escapeHtmlAttr($id) ?>"
        <?php endif; ?>
        >
        <strong class="label switcher-label"><span><?= $block->escapeHtml(__('Currency')) ?></span></strong>
        <div class="actions dropdown options switcher-options">
            <div class="action toggle switcher-trigger"
                <?php if( !$modal ): ?>
                id="switcher-currency-trigger<?= $block->escapeHtmlAttr($id) ?>"
                data-mage-init='{"dropdown":{}}'
                data-toggle="dropdown"
                data-trigger-keypress-button="true"
                <?php endif; ?>
                >
                <strong class="language-<?= $block->escapeHtml($block->getCurrentCurrencyCode()) ?>">
                    <span><?= $block->escapeHtml($currentCurrencyCode) ?></span>
                </strong>
            </div>
            <?php if( !$modal ): ?>
            <ul class="dropdown switcher-dropdown" data-target="dropdown">
                <?php foreach ($currencies as $_code => $_name): ?>
                    <?php if ($_code != $currentCurrencyCode): ?>
                        <li class="currency-<?= $block->escapeHtmlAttr($_code) ?> switcher-option">
                            <a href="#" data-post='<?= /* @noEscape */ $block->getSwitchCurrencyPostData($_code) ?>'><?= $block->escapeHtml($_code) ?> - <?= $block->escapeHtml($_name) ?></a>
                        </li>
                    <?php endif; ?>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>
    </div>

    <?php if(!$mobile_modal): ?>
        <div class="switcher currency switcher-currency js-item-currency-mobile-action tiny-drop tiny-arrow hide-on-desktop">
            <strong class="label switcher-label"><span><?= $block->escapeHtml(__('Currency')) ?></span></strong>
            <div class="actions options switcher-options">
                <div class="action toggle switcher-trigger">
                    <strong class="language-<?= $block->escapeHtml($block->getCurrentCurrencyCode()) ?>">
                        <span><?= $block->escapeHtml($currentCurrencyCode) ?></span>
                    </strong>
                </div>
            </div>
        </div>
        <?php $scriptString = <<<script
        require([
            'jquery'
        ], function ($) {
            let flag;
            const init = () => {
                if(flag) {return}
                const settingsTab = $('.ox-section-items a[href="#store.settings"]').parent();
                if(settingsTab.length){
                    $(".js-item-currency-mobile-action").on('click', function(e) {
                        e.preventDefault();
                        $('.slide-out-menu--mobile').click();
                        settingsTab.click()
                    });
                    flag = true;
                }
            }
            init();
            $('body').on('contentUpdated', init);
        });
script;
if (isset($secureRenderer)){
    echo /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false);
} else{
    echo /* @noEscape */ '<script>' .  $scriptString . '</script>';
} ?>
    <?php endif; ?>
<?php endif; ?>
