jQuery(document).ready(function($){

    $(document).on('change', '[data-setting="acf_repeater_field"]', function(){

        var repeaterName = $(this).val();

        var $subfieldSelect = $('[data-setting="acf_repeater_subfield"]');

        if (!repeaterName) {
            $subfieldSelect.html('');
            return;
        }

        $.ajax({
            url: ajaxurl,
            method: 'POST',
            dataType: 'json',
            data: {
                action: 'get_acf_repeater_subfields',
                repeater_name: repeaterName,
            },
            success: function(response){
                if (response.success) {
                    var options = '';

                    $.each(response.data, function(key, label){
                        options += '<option value="'+key+'">'+label+'</option>';
                    });

                    $subfieldSelect.html(options);
                }
            },
            error: function() {
                $subfieldSelect.html('');
            }
        });

    });

});
