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

use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magento\Search\Helper\Data;
use Magento\Search\ViewModel\AdditionalSearchFormData;
use Magento\Search\ViewModel\ConfigProvider;

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
?>
<?php
/** @var $block Template */
/** @var $helper Data */
/** @var $escaper Escaper */

/** @var $configProvider ConfigProvider */
$configProvider = $block->getData('configProvider');
/** @var $versionManager AdditionalSearchFormData */
$additionalSearchFormData = $block->getData('additionalSearchFormData');
// ox compatiblity with prev magento versions
if($configProvider && method_exists($configProvider, 'getSearchHelperData')){
	$helper = $configProvider->getSearchHelperData();
} else{
	$helper = $this->helper(\Magento\Search\Helper\Data::class);
}


$icon	 = '<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" class="a2-icon-search"><use xlink:href="#a2-search-icon"></use></svg>';
$search_icon = '<span class="search-icon-wrapper">' . $icon . '</span>';
$placeholderText = $escaper->escapeHtml(__('Search'));
?>
<div class="header__item-search">
	<form class="form minisearch" id="search_mini_form_mobile" action="<?= $escaper->escapeUrl($helper->getResultUrl()) ?>" method="get">
		<?php // ox check compatiblity with prev magento versions ?>
		<?php if ($configProvider && $additionalSearchFormData && !empty($queryParams = $additionalSearchFormData->getFormData())): ?>
            <?php foreach ($queryParams as $param): ?>
                <input type="hidden" name="<?= $escaper->escapeHtmlAttr($param['name']) ?>"
                       value="<?= $escaper->escapeHtmlAttr($param['value']) ?>"/>
            <?php endforeach; ?>
        <?php endif; ?>
		<div class="field search">
			<div class="control">
				<input id="search_mobile"
					   data-mage-init='{"quickSearch":{
					   "formSelector":"#search_mini_form_mobile",
					   "url":"<?= $escaper->escapeUrl($helper->getSuggestUrl()) ?>",
					   "destinationSelector":"#search_autocomplete_mobile"}
					   }'
					   type="text"
					   name="<?= $escaper->escapeHtmlAttr($helper->getQueryParamName()) ?>"
					   value="<?= /* @noEscape */ $helper->getEscapedQueryText() ?>"
					   placeholder="<?= $escaper->escapeHtmlAttr($placeholderText) ?>"
					   class="input-text"
					   maxlength="<?= $escaper->escapeHtmlAttr($helper->getMaxQueryLength()) ?>"
					   role="combobox"
					   aria-haspopup="false"
					   aria-autocomplete="both"
					   autocomplete="off"
					   aria-expanded="false"/>
				<div id="search_autocomplete_mobile" class="search-autocomplete"></div>
				<div class="label">
					<div class="wrap">
						<label class="inner" for="search_mobile" data-role="minisearch-label"><?= $escaper->escapeHtml($placeholderText) ?></label>
					</div>
				</div>
                <?= $block->getChildHtml() ?>
			</div>
		</div>
		<div class="actions">
			<button type="submit"
					title="<?= $escaper->escapeHtml(__('Search')) ?>"
					class="action search"
					aria-label="Search"
			>
                <?= $search_icon ?>
				<span><?= $escaper->escapeHtml(__('Search')) ?></span>
			</button>
		</div>
	</form>
</div>
