<script type="text/javascript" nonce="<?php echo $block->getNonce() ?>">
    require(['jquery', 'afdPce', 'domReady!app/Afd/Pce/view/base/templates/afd_options'], function ($, afdPCE, domReady) {

        window.afd = {
            ready: {}
        };

        // Identify if this is an admin page
        var admin = $("[class*=page-layout-admin]").length > 0;

        // Check how many forms are on the page and set the containers array accordingly
        var formCount = 1;
        // Identify the billing page of checkout
        if ($(".checkout-index-index").length > 0 && window.location.hash.substr(1) === "payment") {
            formCount = 2;
            afdOptions.typeahead.containers = ['.form-shipping-address', '.billing-address-form'];
            // Identify the admin create order page which has two forms
        } else if ($(".sales-order_create-index").length > 0) {
            formCount = 2;
            afdOptions.typeahead.containers = ["#order-billing_address_fields", "#order-shipping_address_fields"];
            $(".sales-order_create-index .afd-manual-input-button").html("<?php echo $block->getConfig('afd_typeahead/fields/manualEntryMarkup');?>");
            $(".sales-order_create-index .afd-manual-input-search-button").html("<?php echo $block->getConfig('afd_typeahead/fields/manualEntrySearchMarkup');?>");
            $(".sales-order_create-index .afd-search-again").html("<?php echo $block->getConfig('afd_typeahead/fields/manualEntrySearchMarkup');?>");
            <?php $manualInputCss = preg_replace("/\r|\n/", "", $block->getConfig('afd_typeahead/fields/manualEntryCss'));?>
            <?php $searchAgainCss = preg_replace("/\r|\n/", "", $block->getConfig('afd_typeahead/fields/searchAgainCss'));?>
            $(".sales-order_create-index .afd-manual-input-style").html('<?php echo $manualInputCss ?>');
            $(".sales-order_create-index .afd-search-again-style").html('<?php echo $searchAgainCss ?>');
        } else {
            afdOptions.typeahead.containers = [];
        }

        // Country selector is different when in admin or frontend
        var countrySelector = admin ? "[data-index='country_id'] select, #order-billing_address_country_id" : "[name='country_id']";

        function hideRegions(code) {
            if (hideStateCountries.includes(code)) {
                $("[name='shippingAddress.region']").hide();
                $("[name='shippingAddress.region_id']").hide();
                $("[name='billingAddresscheckmo.region']").hide();
                $("[name='billingAddresscheckmo.region_id']").hide();
                $(".field.region").hide();
                $("[data-index='region_id']").hide();
                $("[data-index='region_id_input']").hide();
            }
        }

        // hide regions for fields countries that hide it
        $(document).on('afd:countryChanged', function (e, code) {
            hideRegions(code);
        });

        hideRegions($(countrySelector).val());

        $(document).on("afd:pceRetrieveComplete", function (e, result) {

            // crown dependency setting
            if ('<?php echo $block->getConfig('afd_typeahead/fields/crownDependencies');?>' === '1') {

                var countryFields = $("#country, " + countrySelector);

                if (result.CountryISO === "GBR") {
                    var countryCode = "GB";
                    switch (result.Country) {
                        case "Isle of Man":
                            countryCode = "IM";
                            break;
                        case "Jersey":
                            countryCode = "JE";
                            break;
                        case "Guernsey":
                            countryCode = "GG";
                            break;
                    }
                    countryFields.val(countryCode).trigger('change');
                }
            }

        });


        // in case the form becomes invalid, show all fields
        $(".form-address-edit").on("invalid-form", function (e) {
            var $resultFields = $('[data-afd-result]');
            if (afdOptions.parentClass) {
                $resultFields.closest('.' + options.typeahead.parentClass).show();
            } else {
                $resultFields.show();
            }
        });

    });

</script>