<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

use Hyva\Theme\ViewModel\HyvaCsp;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/** @var Template $block */
/** @var Escaper $escaper */
/** @var HyvaCsp $hyvaCsp */

?>
<script>
    <?php /* Usage: apply rule on the 'min value' input and pass the 'max value' input ID to check against as the 'options' param */ ?>
    hyva.formValidation.addRule('minMax', (value, options, field, context) => {
        const min = field.element;
        const max = document.getElementById(options);

        if (!min || !max || min.value === '' || max.value === '') {
            return true;
        }

        const valid = !(Number(min.value) > Number(max.value));

        return !valid ? '<?= $escaper->escapeJs(__("The 'Min' value must be lower than the 'Max' value.")) ?>' : true;
    });
</script>
<?php $hyvaCsp->registerInlineScript() ?>
