(function ($) {
    'use strict';

    var editorWindow = (window.parent && window.parent !== window) ? window.parent : window;
    var editor$ = editorWindow.jQuery || $;
    var refreshTimer = null;

    function getEditorDocument() {
        return editorWindow.document || document;
    }

    function findInEditor(selector) {
        return editor$(getEditorDocument()).find(selector);
    }

    function getLayoutType() {
        var $control = findInEditor('.elementor-control[data-setting="layout_type"]');
        var $field = $control.find('select, input').first();

        return String($field.val() || 'slider');
    }

    function toggleResponsiveControl(setting, shouldShow) {
        findInEditor(
            '.elementor-control[data-setting="' + setting + '"], ' +
            '.elementor-control[data-setting="' + setting + '_tablet"], ' +
            '.elementor-control[data-setting="' + setting + '_mobile"]'
        ).toggle(!!shouldShow);
    }

    function toggleControl(setting, shouldShow) {
        findInEditor('.elementor-control[data-setting="' + setting + '"]').toggle(!!shouldShow);
    }

    function toggleSection(sectionId, shouldShow) {
        findInEditor('.elementor-control-section[data-section="' + sectionId + '"]').toggle(!!shouldShow);
    }

    function getActiveRepeaterMediaType() {
        var $openRepeater = findInEditor('.elementor-repeater-row-item-open').last();
        var $field = $openRepeater.find('.elementor-control[data-setting="show_media"] select, .elementor-control[data-setting="show_media"] input').first();

        if (!$field.length) {
            $field = findInEditor('.elementor-control[data-setting="show_media"] select, .elementor-control[data-setting="show_media"] input').first();
        }

        return String($field.val() || 'none');
    }

    function refreshControls() {
        var layoutType = getLayoutType();
        var isGridOrMasonry = layoutType !== 'slider';
        var isPaginationEnabled = isGridOrMasonry && findInEditor('.elementor-control[data-setting="enable_pagination"] input').is(':checked');
        var mediaType = getActiveRepeaterMediaType();

        toggleResponsiveControl('columns', true);
        toggleResponsiveControl('item_gap', isGridOrMasonry);
        toggleControl('enable_pagination', isGridOrMasonry);
        toggleControl('items_per_page', isPaginationEnabled);
        toggleControl('pagination_type', isPaginationEnabled);
        toggleSection('style_testimonial_media_image', mediaType === 'image');
        toggleSection('style_testimonial_media_video', mediaType === 'video');
    }

    function scheduleRefresh() {
        if (refreshTimer) {
            window.clearTimeout(refreshTimer);
        }

        refreshTimer = window.setTimeout(refreshControls, 50);
    }

    function bindEvents() {
        refreshControls();

        editor$(getEditorDocument()).on('change input', '.elementor-control[data-setting="layout_type"] select', scheduleRefresh);

        if (editorWindow.elementor && editorWindow.elementor.channels && editorWindow.elementor.channels.editor) {
            editorWindow.elementor.channels.editor.on('change', scheduleRefresh);
        }

        editor$(getEditorDocument()).on('click', '.elementor-panel-navigation-tab, .elementor-control .elementor-switch-input, .elementor-control input, .elementor-control select', scheduleRefresh);
    }

    $(bindEvents);

    $(window).on('elementor/frontend/init', scheduleRefresh);

    if (editorWindow.elementor && typeof editorWindow.elementor.on === 'function') {
        editorWindow.elementor.on('preview:loaded', scheduleRefresh);
    }
})(jQuery);
