<?php if ($block->getConfig('afd_typeahead/advanced/customCountryCredentials') == 1): ?>

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

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

            if ('<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentials') ?>' === '1') {

                if ('<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsUK') ?>' === '1') {
                    $(document).on('afd:countryChanged', function (e, country) {
                        window.setTimeout(function () {
                            if (country === 'GB') {
                                window.afdOptions.id = '<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsUKID') ?>'
                                window.afdOptions.token = '<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsUKToken') ?>'
                                $('[data-afd-control="typeahead"]').afd('typeahead');
                            }
                        }, 500);

                    });
                }

                if ('<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsIE') ?>' === '1') {
                    $(document).on('afd:countryChanged', function (e, country) {
                        window.setTimeout(function () {
                            if (country === 'IE') {
                                window.afdOptions.id = '<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsIEID') ?>'
                                window.afdOptions.token = '<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsIEToken') ?>'
                                $('[data-afd-control="typeahead"]').afd('typeahead');
                            }
                        }, 500);
                    });
                }

                if ('<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsUS') ?>' === '1') {
                    $(document).on('afd:countryChanged', function (e, country) {
                        window.setTimeout(function () {
                            if (country === 'US') {
                                window.afdOptions.id = '<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsUSID') ?>'
                                window.afdOptions.token = '<?php echo $block->getConfig('afd_typeahead/advanced/customCountryCredentialsUSToken') ?>'
                                $('[data-afd-control="typeahead"]').afd('typeahead');
                            }
                        }, 500);
                    });
                }

                $(document).on('afd:countryChanged', function (e, country) {
                    window.setTimeout(function () {
                        if (['GB', 'IE', 'US'].indexOf(country) < 0) {
                            window.afdOptions.id = '<?php echo $block->getConfig('afd_general/account/id') ?>'
                            window.afdOptions.token = '<?php echo $block->getConfig('afd_general/account/token') ?>'
                            $('[data-afd-control="typeahead"]').afd('typeahead');
                        }
                    }, 500);
                });

            }

        });

    </script>

<?php endif; ?>
