import { NewState, State } from '@apigo.cc/state'
import '@apigo.cc/bootstrap'

// Re-exports
export * from './http.js'
export * from './ui.js'
export * from './form.js'
export * from './controls.js'
export * from './list.js'
export * from './nav.js'
export * from './interaction.js'

export { State }

if (typeof window !== 'undefined') {
	window.addEventListener('beforeunload', (event) => {
		if (State.exitBlocks > 0) event.preventDefault()
	})
}

const htmlNode = document.documentElement;
if (!htmlNode.hasAttribute('$data-bs-theme') && !htmlNode.hasAttribute('data-bs-theme')) {
	htmlNode.setAttribute('$data-bs-theme', "LocalStorage.darkMode?'dark':'light'");
}

// Side effects: ensure global namespaces are populated
import { HTTP } from './http.js'
import { UI } from './ui.js'
import { AutoForm } from './form.js'
import { MouseMover } from './interaction.js'
import { VirtualScroll } from './list.js'

globalThis.HTTP = HTTP
globalThis.UI = UI
globalThis.AutoForm = AutoForm
globalThis.MouseMover = MouseMover
globalThis.VirtualScroll = VirtualScroll

const ApigoBase = {
    HTTP, UI, AutoForm, MouseMover, VirtualScroll, State
};

import { RefreshState } from '@apigo.cc/state'
if (typeof document !== 'undefined') {
    globalThis.ApigoBase = ApigoBase;

	const doRefresh = () => RefreshState(document.documentElement)
	if (document.readyState !== 'loading') setTimeout(doRefresh, 1)
	else document.addEventListener('DOMContentLoaded', () => setTimeout(doRefresh, 1), true)
}

