import { addEventListenerWithInitialCall, observeClick, observeOnce } from "./_global--observer.js";




observeOnce(".neo-settings--section-group-toggle", (toggleNode) => {
    const groupNode = toggleNode.parentNode.nextElementSibling;
    observeClick(toggleNode, (toggleNode) => {
        if (groupNode.classList.contains("neo-settings--section-group-collapsed")) {
            groupNode.hidden = false;
            groupNode.classList.remove("neo-settings--section-group-collapsed"); toggleNode.classList.add("neo-settings--section-group-toggle-open");
            groupNode.style.maxHeight = "0px"; groupNode.style.overflow = "hidden"; requestAnimationFrame(() => { groupNode.style.maxHeight = groupNode.scrollHeight + "px"; groupNode.style.opacity = "1"; });
        } else {
            groupNode.classList.add("neo-settings--section-group-collapsed"); toggleNode.classList.remove("neo-settings--section-group-toggle-open");
            groupNode.style.maxHeight = groupNode.scrollHeight + "px"; groupNode.style.overflow = "hidden"; requestAnimationFrame(() => { groupNode.style.maxHeight = "0px"; groupNode.style.opacity = "0"; });
        }
    });
    groupNode.addEventListener("transitionend", (event) => {
        if (event.propertyName !== "max-height") { return; }
        if (!groupNode.classList.contains("neo-settings--section-group-collapsed")) { groupNode.style.maxHeight = "none"; groupNode.style.overflow = "visible"; return; }
        groupNode.hidden = true;
    });
});


addEventListenerWithInitialCall(window, "hashchange", async () => {
    if (!location.hash) { return; }
    const sectionNode = document.querySelector(location.hash);
    if (!sectionNode) { return; }
    while (!sectionNode.open) { await new Promise(requestAnimationFrame); }
    sectionNode.open(true, { suppressAnimation: true });
    await new Promise(requestAnimationFrame);
    sectionNode.scrollIntoView({ behavior: "smooth", block: "start" });
    history.replaceState(null, "", location.hash + "-scrolled");
});
