const selectCallbacks = {}

function onSelected(dropdownSelector, cb) {
    if (!selectCallbacks[dropdownSelector]) {
        selectCallbacks[dropdownSelector] = []
    }

    selectCallbacks[dropdownSelector].push(cb)
}

async function choose(dropdownSelector, option) {
    const select = document.querySelector(`details.${dropdownSelector}`)
    const title = document.querySelector(`details.${dropdownSelector}>summary`)
    const value = document.querySelector(`label[for=${option.id}]`)
    title.innerHTML = value.innerHTML
    if (select.getAttribute("value") != option.getAttribute("value")) {
        await sleep(115)
    }
    select.setAttribute("value", option.getAttribute("value"))
    select.removeAttribute("open")
    if (Array.isArray(selectCallbacks[dropdownSelector])) {
        for (const callback of selectCallbacks[dropdownSelector]) {
            callback(option.getAttribute("value"))
        }
    }
}