<?php if ($block->getConfig('afd_email/enable/enable') == 1): ?>

    <script type="text/javascript" nonce="<?php echo $block->getNonce(); ?>">

        require(['jquery', 'afdPce', 'domReady!app/Afd/Pce/view/base/templates/afd'], function ($) {

            window.afdEmailOptions = {};

            <?php if($block->getConfig('afd_email/account/useDefaultCredentials') == 0): ?>


            <?php if($block->getConfig('afd_email/account/authMethod') == "id"): ?>
            afdEmailOptions.pceUrl = '<?php echo $block->getConfig('afd_email/account/idPceUrl'); ?>';
            afdEmailOptions.id = '<?php echo $block->getConfig('afd_email/account/id'); ?>';
            afdEmailOptions.token = '<?php echo $block->getConfig('afd_email/account/token'); ?>';
            afdEmailOptions.serial = null;
            afdEmailOptions.password = null;
            <?php else: ?>
            afdEmailOptions.pceUrl = '<?php echo $block->getConfig('afd_email/account/serialPceUrl'); ?>';
            afdEmailOptions.serial = '<?php echo $block->getConfig('afd_email/account/serial'); ?>';
            afdEmailOptions.password = '<?php echo $block->getConfig('afd_email/account/password'); ?>';
            afdEmailOptions.id = null;
            afdEmailOptions.token = null;
            <?php endif; ?>


            <?php endif; ?>

            $('[data-afd-control="email"]').on('keyup', function (e) {

                $(e.target).closest('form').find('button').attr('disabled', true);

            });

            $(document).on('afd:emailValidationStarted', function (e, el) {

                var $el = $(el);

                // newsletter signup
                if ($el.closest('.newsletter').length !== 0) {
                    $el.closest('.field').addClass('afd-loading');
                    //create account
                } else if ($el.closest('.form-create-account').length !== 0) {
                    $el.closest('.control').addClass('afd-loading');
                    // my account edit email
                } else if ($el.closest('.form-edit-account').length !== 0) {
                    $el.closest('.control').addClass('afd-loading');
                } else {
                    $el.closest('.control').addClass('afd-loading');
                }

                $el.closest('form').find('button').attr('disabled', true);

            });

            $(document).on('afd:emailValidationSuccess', function (e, res, $el) {

                if (typeof res.Result !== 'undefined' && res.Result === '-2') {
                    $el.closest('form').find('button').attr('disabled', true);
                } else {
                    $el.closest('form').find('button').attr('disabled', false);
                }

                $el.closest('.afd-loading').removeClass('afd-loading');
            });
            $(document).on('afd:emailValidationUpdated', function (e, $el) {
                const currentEmail = $el.val();
                if ($el.data('afd-already-valid') && $el.data('afd-already-valid').indexOf(currentEmail) > -1) {
                    $el.closest('form').find('button').attr('disabled', false);
                }
            })
        });

    </script>

<?php endif; ?>

