//go:build js && wasm

package wasmruntime

import (
	"strings"
	"testing"

	"kjol/vdom"
)

// Floating.Panel and Modal both declare their initial inline style in the style
// ATTRIBUTE, then overwrite top/left/opacity imperatively with SetStyle. That only
// works because the reconciler skips setAttribute when the declared value has not
// changed. If that ever stops being true, every floating panel silently snaps back
// to visibility:hidden at 0,0 on the next re-render. Pin it.
func TestImperativeStylesSurviveReRender(t *testing.T) {
	root := document.Call("createElement", "div")
	panel := vdom.NewRef()

	const declared = "position:fixed;top:0;left:0;visibility:hidden"
	a := vdom.El("div", vdom.WithRef(panel), vdom.Attr("style", declared), vdom.Text("one"))
	patchChildren(root, nil, one(a))

	// Position it, the way Reposition does.
	SetStyle(panel, "top", "120px")
	SetStyle(panel, "left", "40px")
	SetStyle(panel, "visibility", "visible")

	// A re-render with the SAME declared style but different content.
	b := vdom.El("div", vdom.WithRef(panel), vdom.Attr("style", declared), vdom.Text("two"))
	patchChildren(root, one(a), one(b))

	got := root.Get("innerHTML").String()
	if !strings.Contains(got, "two") {
		t.Fatalf("content did not update: %s", got)
	}
	if !strings.Contains(got, "top: 120px") || !strings.Contains(got, "visibility: visible") {
		t.Errorf("imperative position was clobbered by the re-render:\n%s", got)
	}
}
