ondomload(function () {
    
    
    // #region ==================== LANGUAGE SWITCHER
    
    let langSwitchers = document.querySelectorAll(".lang-switcher");
    langSwitchers.forEach(switcher => {
        let items = switcher.querySelectorAll(".dropdown-item");
        items.forEach(item => {
            item.addEventListener("click", (e) => {
                
                e.preventDefault();
                
                let toggle = item.parentElement.previousElementSibling;
                if (toggle.children[0].dataset.lang == item.children[0].dataset.lang) {
                    return;
                }
                // toggle.innerHTML = "";
                // toggle.append(item.children[0].cloneNode(true));
                
                Cookies.set("lang", item.children[0].dataset.lang, location.host, "/", 7 * 24 * 60 * 60);
                
                // FORM STATE SAVE
                // if (typeof fh != "undefined") {
                //     if (fh.hasChanged() || !fh.isEmpty()) {
                //         let values = fh.getFieldValues();
                //         values = JSON.stringify(values);
                //         sessionStorage.setItem("formValues", values);
                //     }
                // }
                
                // NOTIFICATIONS SAVE
                if (typeof fh != "undefined") {
                    let formErrors = {
                        errors : [],
                        fieldErrors : {},
                    };
                    fh.form.querySelectorAll(".notifications .alert").forEach(notif => {
                        if (Notification.isNotification(notif)) {
                            let error = Notification.getVarsFromElement(notif);
                            formErrors.errors.push(error);
                        }
                    });
                    fh.forEach("fields", function (field, fieldName) {
                        let nextSib = field.nextElementSibling;
                        if (nextSib && Notification.isNotification(nextSib)) {
                            let error = Notification.getVarsFromElement(nextSib);
                            formErrors.fieldErrors[fieldName] = error;
                        }
                    });
                    sessionStorage.setItem("formErrors", JSON.stringify(formErrors));
                }
                
                location.reload();
            });
        });
    });
    
    // #endregion
    
    
    // #region ==================== AUTO WIDTH SYNC
    
    Object.values(WidthSyncer.findSyncWidthGroups()).forEach(
        group => {
            WidthSyncer.syncElementsWidths(group);
        }
    );
    
    let fitColumnTables = Array.from(document.querySelectorAll("[class*=fit-col-]"));
    fitColumnTables.forEach(table => {
        let fitColumns = table.className.match(/(?:fit-col-(\d+))+/g);
        fitColumns.forEach(fitColumn => {
            let colIndex = fitColumn.match(/fit-col-(\d+)/)[1];
            colIndex = parseInt(colIndex) - 1;
            table.classList.add("w-auto");
            let computedWidth = getComputedStyle(table.tBodies[0].rows[0].cells[colIndex]).width;
            let colWidth = parseFloat(computedWidth) || 0;
            table.classList.remove("w-auto");
            table.tBodies[0].rows[0].cells[colIndex].style.width = colWidth + "px";
        });
    });
    
    let columnSyncedTables = Array.from(document.querySelectorAll("table.sync-columns"));
    if (columnSyncedTables.length) {
        syncTableColumns(...columnSyncedTables);
    }
    
    // #endregion
    
    
    // #region ==================== MUST-CONFIRM ELEMENTS
    
    let mustConfirmElements = document.querySelectorAll(".must-confirm");
    mustConfirmElements.forEach(el => {
        el.addEventListener("click", e => {
            e.preventDefault();
            if ((el.dataConfirmPreCallback && el.dataConfirmPreCallback == "function") || el.dataset.confirmPreCallback) {
                if (!(el.dataConfirmPreCallback && el.dataConfirmPreCallback == "function") && el.dataset.confirmPreCallback) {
                    let preconfirmCallback = Function(el.dataset.confirmPreCallback);
                    preconfirmCallback = preconfirmCallback.bind(el);
                    el.dataConfirmPreCallback = preconfirmCallback;
                }
                el.dataConfirmPreCallback();
            }
            let confirmText = el.dataset.confirmText || "You must confirm to continue.";
            setTimeout(function () {
                if (confirm(confirmText)) {
                    let tagName = el.tagName.toLowerCase();
                    if (tagName == "a") {
                        location.href = el.href;
                    }
                    else if (tagName == "button") {
                        if (el.form) {
                            el.form.submit();
                        }
                    }
                }
                el.blur();
                if ((el.dataConfirmPostCallback && el.dataConfirmPostCallback == "function") || el.dataset.confirmPostCallback) {
                    if (!(el.dataConfirmPostCallback && el.dataConfirmPostCallback == "function") && el.dataset.confirmPostCallback) {
                        let preconfirmCallback = Function(el.dataset.confirmPostCallback);
                        preconfirmCallback = preconfirmCallback.bind(el);
                        el.dataConfirmPostCallback = preconfirmCallback;
                    }
                    el.dataConfirmPostCallback();
                }
            }, parseInt(el.dataset.confirmDelay) || 0);
        });
    });
    
    // #endregion
    
    
    // #region ==================== MISC
    
    // TOOLTIPS
    $('.data-toggle-tooltip, [data-toggle="tooltip"]').tooltip();
    
    // NO-CLICK ELEMENTS
    document.querySelectorAll(".no-click").forEach(el => {
        el.setAttribute("onclick", "return false");
    });
    
    // #endregion
    
    
    // #region ==================== :BLANK
    
    document.querySelectorAll("body *").forEach(el => {
        if (["input", "hr", "img", "script"].includes(el.tagName.toLowerCase())) {
            return;
        }
        if (el.childElementCount == 0 && el.textContent.trim() == "") {
            el.classList.add("is-blank");
        }
    });
    
    //#endregion
    
    // eskisi
    // $(document).ready(function () {
        // bsCustomFileInput.init()
    // })
    
    if (typeof bsCustomFileInput != "undefined") {
        bsCustomFileInput.init();
    }
    
});