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

// @codingStandardsIgnoreFile

?>
<script>
require([
    "jquery",
    "mage/adminhtml/form"
], function(jQuery){

    var updater = new RegionUpdater('tax_country_id', 'tax_region', 'tax_region_id', <?php /* @escapeNotVerified */ echo $this->helper('Magento\Directory\Helper\Data')->getRegionJson() ?>, 'disable');
    updater.disableRegionValidation();

    (function ($) {
        $(document).ready(function () {
            'use strict';

            var zipIsRange = $('#zip_is_range')
                .on('change.zipRange', function(){
                    var elem = $(this),
                        zipIsRangeChecked = elem.is(':checked'),
                        zipFrom = $('.field-zip_from'),
                        zipTo = $('.field-zip_to'),
                        zipCode = $('.field-tax_postcode'),
                        setState = function(element, visibility) {
                            var input = element.find(':input');
                            if (visibility) {
                                element
                                    .addClass('required')
                                    .removeClass('hidden');
                                input.addClass('required-entry');
                            } else {
                                element
                                    .removeClass('required')
                                    .addClass('hidden');
                                input.removeClass('required-entry');
                            }
                        };
                    elem.val(zipIsRangeChecked ? 1 : 0);
                    setState(zipCode, !zipIsRangeChecked);
                    setState(zipFrom, zipIsRangeChecked);
                    setState(zipTo, zipIsRangeChecked);
                });
            if (zipIsRange.val()) {
                zipIsRange.attr({checked: 'checked'})
            }
            zipIsRange.trigger('change');
        });
    })(jQuery);

    window.updater = updater;
});
</script>
