// Port of web/kit/RemoteUpdateFlash.tsx.

package webui

import "kjol/wasmruntime/vdom"

// RemoteFlash is the trigger/signal pair from createRemoteFlash: Fire() shows
// the flash, Visible() reports whether it is currently shown, and Clear() hides
// it. Create one with NewRemoteFlash and render RemoteUpdateFlash(flash.Visible())
// wherever the pill should appear.
type RemoteFlash struct {
	visible *vdom.Signal[bool]
}

// NewRemoteFlash creates a flash controller.
//
// NOTE: the TSX createRemoteFlash auto-clears after durationMs via setTimeout.
// The neutral runtime has no timer, so the auto-clear is dropped — the caller
// must call Clear() when the flash should end (durationMs is retained only for
// API/documentation parity).
func NewRemoteFlash(durationMs int) *RemoteFlash {
	_ = durationMs
	return &RemoteFlash{visible: vdom.NewSignal(false)}
}

// Visible reports whether the flash is currently showing.
func (f *RemoteFlash) Visible() bool { return f.visible.Get() }

// Fire shows the flash (call when a remote update arrives).
func (f *RemoteFlash) Fire() { f.visible.Set(true) }

// Clear hides the flash.
func (f *RemoteFlash) Clear() { f.visible.Set(false) }

const remoteUpdateFlashCls = "remote-update-flash inline-flex items-center gap-1 rounded-full bg-emerald-100 dark:bg-emerald-900 border border-emerald-300 px-2 py-0.5 text-[10px] font-semibold uppercase tracking-widest text-emerald-700 dark:text-emerald-400"

// RemoteUpdateFlash is a small pill that briefly shows "Updated". It renders
// nothing (nil) when when is false, mirroring the TSX <Show when=…>.
func RemoteUpdateFlash(when bool) *vdom.VNode {
	if !when {
		return nil
	}
	return vdom.Div(vdom.Attr("class", remoteUpdateFlashCls),
		vdom.Svg(vdom.Attr("viewBox", "0 0 12 12"),
			vdom.Attr("class", "w-2.5 h-2.5 fill-current"),
			vdom.Raw(`<circle cx="6" cy="6" r="6"/>`),
		),
		vdom.Text("Updated"),
	)
}
