(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";

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

    const Toast = Swal.mixin({
        toast: true,
        position: "top-end",
        showConfirmButton: false,
        timer: 3000,
        timerProgressBar: true,
        customClass: {
            container: 'top-40'
        },
        didOpen: (toast) => {
          toast.onmouseenter = Swal.stopTimer;
          toast.onmouseleave = Swal.resumeTimer;
        }
    });

    function formatLabel(fieldId, str, tab) {
        if(fieldId == str){
            str = str
                .split('-')                               // Split by hyphen
                .map(part =>
                    part
                        .split('_')                           // Split by underscore
                        .map(word => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize
                        .join(' ')
                )
                .join(' → ');
            return str;
        }
        return tab + ' → ' + str;
    }

    function triggerSwalEmptyRequired(fieldId, firstLabel, tab) {
        Swal.fire({
            icon: 'warning',
            title: 'Empty Required Field',
            html: firstLabel ? 'Check: <b>' + formatLabel(fieldId, firstLabel, tab) + '</b>' : '',
            customClass: {
                confirmButton: 'btn text-bg-primary'
            }
        });
    }

    $('[data-vue]').each(function () {
      var $this = $(this);
      var data_var = $this.attr('data-vue');
      var data_source = $this.attr('data-source');
      Vue.mixin(window.validationMixin); 
      new Vue({
        el: $(this)[0],
        data: function data() {
          return {
            loading: false,
            data: '',
            settings_alert: {
              status: false,
              success: true
            },
            invalidFields: new Set(),
          };
        },
        created() {
            // Listen for validation updates from fields
            this.$on('field-validation', ({ fieldId, isValid }) => {
              if (isValid) {
                this.invalidFields.delete(fieldId);
              } else {
                this.invalidFields.add(fieldId);
              }
            });
        },
        mounted: function mounted() {
            this.getSettings();
            this.clearEmptyGroups();
          
            // Intercept #publish (WP Update/Publish) button
            const vm = this;
            $(document).on('click', '#publish', function(e) {
                vm.validateAllFields();
                const invalidField = vm.validateAllFields();
                if (invalidField) {
                    e.preventDefault();
                    let firstInvalidFieldId = invalidField.id;
                    let firstLabel = invalidField.label;
                    let tabTitle = invalidField.tab;
                    if (typeof window.Swal === 'function') {
                        triggerSwalEmptyRequired(firstInvalidFieldId, firstLabel, tabTitle);
                    } else {
                        alert('Please fill all required fields before saving.' + (firstLabel ? '\nFirst missing: ' + firstLabel : ''));
                    }
                    return false;
                }
            });
        },
        methods: {
            validateAllFields: function validateAllFields() {
                let domInvalidFields = [];
                $('.wpcfto-box-child').each(function () {
                    const boxChild = $(this);
                    const fieldId = boxChild.attr('data-field');
                    const fieldTab = boxChild.closest('.wpcfto-tab').attr('id');
                    const fieldTabTitle = $(`[data-section=${fieldTab}]`).text();
                    const $inputs = boxChild.find('input, textarea, select');

                    if(boxChild.hasClass('repeater')){
                        const repeater_item = boxChild.find('.wpcfto-repeater-single');
                        repeater_item.each(function(key, field){
                            const _key = key+1;
                            const repeater_item_inputs = $(this).find('input, textarea, select');
                            repeater_item_inputs.each(function () {
                                const $field = $(this);
                                const name = $field.attr('name');
                                // if (!$field.is(':visible')) return;
                                if (!$field.is('[required]')) return;
        
                                let isEmpty = false;
                                if ($field.is(':checkbox') || $field.is(':radio')) {
                                    if (!$(`[name="${name}"]`).val()) isEmpty = true;
                                } else {
                                    if (!$.trim($field.val())) isEmpty = true;
                                }
        
                                if (isEmpty) {
                                    const fieldSubTabTitle = fieldTabTitle +'→ Item #'+_key ;
                                    const fieldSubLabel = $field.closest('.wpcfto_generic_field').find('.wpcfto-field-aside__label span:first-child').text()
                                    domInvalidFields.push({
                                        id: fieldId,
                                        tab: fieldSubTabTitle,
                                        label: fieldSubLabel
                                    });
                                }
                            });
                        });
                    }else{
                        $inputs.each(function () {
                            const $field = $(this);
                            const name = $field.attr('name');

                            if (!$field.is('[required]')) return;
    
                            let isEmpty = false;
                            if ($field.is(':checkbox') || $field.is(':radio')) {
                                if (!$(`[name="${name}"]`).val()) isEmpty = true;
                            } else {
                                if (!$.trim($field.val())) isEmpty = true;
                            }
    
                            if (isEmpty) {
                                domInvalidFields.push({
                                    id: fieldId,
                                    tab: fieldTabTitle,
                                    label: $(`[data-field=${fieldId}] label > span:first-child`).text()
                                });
                            }
                        });
                    }

                });

                if (domInvalidFields.length > 0) {
                    console.log(domInvalidFields);
                    return domInvalidFields[0]; // Return the first invalid field object
                }
                return null;
            },
            getFieldLabelById(fieldId) {
                let label = '';
                var parent = $('#'+fieldId).closest('.wpcfto-box-child');
                var dataField = parent.attr('data-field');
                var labelText = $('[data-field='+dataField+'] label span:first-child').text();
                label = labelText;
                return label || fieldId;
            },
            initSubmenu: function initSubmenu() {
                Vue.nextTick().then(function () {
                (function ($) {
                    /*Hide all fields in submenu*/
                    var submenu_tab_fields = $('.wpcfto-tab.has-submenu-items [data-field], .wpcfto-tab.has-submenu-items .wpcfto_group_started');
                        submenu_tab_fields.css({
                        display: 'none'
                    });
                    var $sub_menu = $('.wpcfto-submenus .active');
                    var sub_menu_section = $sub_menu.attr('data-submenu');
                    var $submenu_section = $('.' + sub_menu_section);
                    $submenu_section.removeAttr('style');
                    submenu_tab_fields.parents('.wpcfto_group_started').css({
                        display: 'none'
                    });
                    $submenu_section.parents('.wpcfto_group_started').removeAttr('style');
                })(jQuery);
                });
            },
            changeTabFromAnchor: function changeTabFromAnchor() {
                var _this = this;

                var hash = window.location.hash;
                var hashParts = hash.split('#');

                if (typeof hashParts[1] !== 'undefined') {
                Vue.nextTick(function () {
                    _this.changeTab(hashParts[1]);
                });
                }
            },
            changeTab: function changeTab(tab) {
                var $tab = $('#' + tab);
                $tab.closest('.stm_metaboxes_grid__inner').find('.wpcfto-tab').removeClass('active');
                $tab.addClass('active');
                var $section = $('div[data-section="' + tab + '"]');
                $tab.closest('.wpcfto-settings').find('.wpcfto-nav').removeClass('active');
                $tab.closest('.stm_metaboxes_grid__inner').find('.wpcfto-nav').removeClass('active');
                $section.closest('.wpcfto-nav').addClass('active');
                history.pushState(null, null, '#' + tab);
                /*if has submenu*/

                if ($section.closest('.wpcfto-nav').hasClass('has-submenu')) {
                var $submenu = $section.closest('.wpcfto-nav').find('.wpcfto-submenus [data-submenu]').eq(0);
                var urlParams = new URLSearchParams(window.location.search);
                var submenuParam = urlParams.get('submenu');

                if (submenuParam) {
                    var navSubmenu = $section.closest('.wpcfto-nav').find(".wpcfto-submenus [data-submenu=".concat(tab, "_").concat(submenuParam, "]"));
                    $submenu = navSubmenu !== undefined && navSubmenu.length > 0 ? navSubmenu : $submenu;
                }

                this.changeSubMenu($submenu.attr('data-submenu'));
                }
                /*Scroll top*/


                $("html, body").animate({
                scrollTop: $tab.closest('.stm_metaboxes_grid__inner').offset().top - 100
                }, "fast");
            },
            changeSubMenu: function changeSubMenu(sub_menu) {
                var $submenu = $('[data-submenu="' + sub_menu + '"]');
                $('[data-submenu]').removeClass('active');
                $submenu.addClass('active');
                this.initSubmenu();
            },
            getSettings: function getSettings() {
                var _this = this;

                _this.loading = true;
                this.$http.get(stm_wpcfto_ajaxurl + '?action=stm_wpcfto_get_settings&source=' + data_source + '&name=' + data_var + '&nonce=' + wpcfto_global_settings['nonce']).then(function (r) {
                _this.$set(_this, 'data', r.body);

                _this.loading = false;
                this.changeTabFromAnchor();
                this.initSubmenu();
                });
            },
            saveSettings: function saveSettings(id) {
                const invalidField = this.validateAllFields();
                if (invalidField) {
                    let firstInvalidFieldId = invalidField.id;
                    let firstLabel = invalidField.label;
                    var firstTab = firstInvalidFieldId.split('-');
                    var _firstTab = firstTab[0];
                    if (typeof window.Swal === 'function') {
                        triggerSwalEmptyRequired(firstInvalidFieldId, firstLabel, _firstTab);
                    } else {
                        alert('Please fill all required fields before saving.' + (firstLabel ? '\nFirst missing: ' + firstLabel : ''));
                    }
                    return;
                }
                
                var vm = this;
                vm.loading = true;
                  // Proceed with existing saveSettings logic
                this.$http.post(stm_wpcfto_ajaxurl + '?action=wpcfto_save_settings&nonce=' + stm_wpcfto_nonces['wpcfto_save_settings'] + '&name=' + id, JSON.stringify(vm.data)).then(function (response) {
                    vm.loading = false;
                    vm.settings_alert = {
                        success: response.status === 200,
                        status: true
                    };
                    setTimeout(function () {
                        vm.settings_alert.status = false;
                    }, 1500);
                    // location.reload();
                    if(response.status == 200 && response.ok)
                    if (typeof window.Swal === 'function') {
                        Toast.fire({
                            icon: "success",
                            title: 'Successfully save any changes'
                        });
                    } else {
                        alert('Successfully save any changes');
                    }
                });
            },
            initOpen: function initOpen(field) {
                if (typeof field.opened === 'undefined') {
                this.$set(field, 'opened', !!field.value);
                }
            },
            openField: function openField(field) {
                var opened = !field.opened;
                this.$set(field, 'opened', opened);

                if (!field.opened) {
                this.$set(field, 'value', '');
                }
            },
            enableAddon: function enableAddon($event, option) {
                var _this = this;

                Vue.nextTick(function () {
                (function ($) {
                    var currentItem = $($event.target);
                    currentItem.addClass('loading');
                    var url = stm_wpcfto_ajaxurl + '?action=stm_lms_enable_addon&addon=' + option;

                    _this.$http.get(url).then(function (response) {
                    currentItem.removeClass('loading');
                    var $container = $('.stm_lms_addon_group_settings_' + option);
                    $container.each(function () {
                        var $this = $(this);
                        $this.removeClass('is_pro is_pro_in_addon');
                        $this.find('.field_overlay').remove();
                        $this.find('.pro-notice').remove();
                    });
                    });
                })(jQuery);
                });
            },
            clearEmptyGroups: function clearEmptyGroups() {
                Vue.nextTick().then(function () {
                (function ($) {
                    $('.wpcfto_group_started').each(function () {
                    var $group = $(this);
                    var $childs = $group.find('.wpcfto-box-child');

                    if (!$childs.length) {
                        $group.addClass('no-childs-visible');
                    } else {
                        $group.removeClass('no-childs-visible');
                    }
                    });
                })(jQuery);
                });
            },
            addRepeaterRow() {
                if (!this.validateAllFields()) {
                    if (typeof window.Swal === 'function') {
                        Toast.fire({
                            icon: "warning",
                            title: 'Please fill all required fields before adding a new row.'
                        });
                    } else {
                        alert('Please fill all required fields before adding a new row.');
                    }
                    return;
                }
                // Your existing logic to add repeater row
            }
        },
        watch: {
          data: {
            deep: true,
            handler: function handler() {
              var _this = this;

              setTimeout(function () {
                _this.clearEmptyGroups();

                _this.initSubmenu();
              }, 100);
            }
          }
        }
      });
    });
  });
})(jQuery);
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 20048 characters]
},{}]},{},[1])