package dev.inmo.postssystem.client.fsm.ui

import dev.inmo.postssystem.client.ui.fsm.UIFSMHandler
import dev.inmo.postssystem.client.ui.fsm.UIFSMState
import dev.inmo.micro_utils.fsm.common.StatesMachine
import kotlinx.browser.document
import org.w3c.dom.HTMLElement

abstract class JSView<T : UIFSMState> : UIFSMHandler<T> {
    open suspend fun StatesMachine<in UIFSMState>.safeHandleState(
        htmlElement: HTMLElement,
        state: T
    ): UIFSMState? = null

    override suspend fun StatesMachine<in UIFSMState>.safeHandleState(state: T): UIFSMState? {
        return safeHandleState(document.getElementById(state.context) as? HTMLElement ?: return null, state)
    }
}
