export function appendStylesheet(id: string, href: string) {
    return new Promise((resolve, reject) => {
        const element = document.getElementById(id) as HTMLLinkElement
        if (element && element.href === href) {
            resolve(0)
            return
        }

        const link = document.createElement('link')
        link.id = id
        link.rel = 'stylesheet'
        link.href = href
        link.onload = resolve
        link.onerror = link.onabort = reject
        document.head.appendChild(link)
    })
}

export function appendScript(id: string, src: string) {
    return new Promise((resolve, reject) => {
        const element = document.getElementById(id) as HTMLScriptElement
        if (element && element.src === src) {
            resolve(0)
            return
        }

        const script = document.createElement('script')
        script.id = id
        script.src = src

        script.onload = resolve
        script.onerror = script.onabort = reject
        document.head.appendChild(script)
    })
}

export function removeChild(id: string) {
    const child = document.getElementById(id)
    if (child) {
        child.parentNode.removeChild(child)
    }
}
