/**
 * Top-center toast for script execution submitted (3s auto-dismiss, click to close).
 */import { reactive } from 'vue'

const TOAST_MS = 3000

export const scriptSubmitToastState = reactive({
  visible: false,
  text: '',
  color: 'success' as string,
})

let hideTimer: ReturnType<typeof setTimeout> | null = null

export function showScriptSubmitToast(text: string, color = 'success') {
  if (hideTimer) {
    clearTimeout(hideTimer)
    hideTimer = null
  }
  scriptSubmitToastState.text = text
  scriptSubmitToastState.color = color
  scriptSubmitToastState.visible = true
  hideTimer = setTimeout(() => {
    dismissScriptSubmitToast()
  }, TOAST_MS)
}

export function dismissScriptSubmitToast() {
  if (hideTimer) {
    clearTimeout(hideTimer)
    hideTimer = null
  }
  scriptSubmitToastState.visible = false
}

export function useScriptSubmitToast() {
  return {
    state: scriptSubmitToastState,
    show: showScriptSubmitToast,
    dismiss: dismissScriptSubmitToast,
  }
}
