window.addEventListener("DOMContentLoaded", function () {
    
    let form = document.querySelector("main form");
    if (form) {
        
        if (form.classList.contains("disable-enter-submit")) {
            form.addEventListener("keypress", e => {
                if (e.key == "Enter") {
                    e.preventDefault();
                }
            });
        }
        
        fh = new FormHandler(form);
        fh.detectFocusField();
        // setTimeout(() => {
        //     fh.focus();
        // }, 250);
        
        form.addEventListener("submit", () => {
            let submitButton = qs(":is(button, input)[type=submit]");
            if (submitButton) {
                submitButton.disabled = true;
            }
        });
        
        Notification.setDefaults({
            "baseClass"          : "alert",
            "classPrefix"        : "alert-",
            "dismissibleClass"   : "dismissible",
            "dismissibleElement" : elem("button", {"type" : "button", "class" : "close", "data-dismiss" : "alert"}, [
                elem("i", {"class" : "fas fa-times"}),
            ]),
            "types" : ["success", "danger", "warning", "info"],
        });
        
        if (fh.fields.avatar) {
            
            imgUploader = new SingleImageUploader(fh.fields.avatar, avatarUploadConfig);
            
            let cardBody = fh.fields.avatar.closest(".card-body");
            
            imgUploader.notificationBox = cardBody.querySelector(".notifications") || document.querySelector("main > .notifications");
            imgUploader.notifications   = new NotificationManager("danger", true);
            
            imgUploader.image = cardBody.querySelector(".image-frame img");
            
            imgUploader.buttons.browse = cardBody.querySelector("#browse-image");
            imgUploader.buttons.crop = cardBody.querySelector("#crop-image");
            imgUploader.buttons.cancel = cardBody.querySelector("#remove-selected");
            
            if (fh.fields.delete_avatar) {
                imgUploader.inputs.delete  = fh.fields.delete_avatar;
                imgUploader.buttons.delete = cardBody.querySelector("#delete-image");
            }
            
            imgUploader.attachClickHandlers();
        }
        
    }
    
});