package app

import 

// EventHandler represents a function that can handle HTML events. They are
// always called on the UI goroutine.
type EventHandler func(ctx Context, e Event)

// Event is the interface that describes a javascript event.
type Event struct {
	Value

	// Data carries event attributes in server-side renderers, eg livepage.
	// It is nil in browser-based events.
	Data map[string]string
}

// PreventDefault cancels the event if it is cancelable. The default action that
// belongs to the event will not occur.
func ( Event) () {
	.Call("preventDefault")
}

// If several listeners are attached to the same element for the same event type,
// they are called in the order in which they were added. If
// StopImmediatePropagation() is invoked during one such call, no remaining
// listeners will be called, either on that element or any other element.
func ( Event) () {
	.Call("stopImmediatePropagation")
}

// EventOption represents an option for configuring event handlers, such as
// setting scopes or marking them as passive.
type EventOption struct {
	name  string
	value string
}

// EventScope returns an EventOption that adds a scope to an event handler.
// This is useful in dynamic UI contexts to ensure correct mounting and
// dismounting of handlers as UI elements are reordered. The scope is defined
// by concatenating the provided path arguments into a unique identifier.
func ( ...any) EventOption {
	return EventOption{
		name:  "scope",
		value: toPath(...),
	}
}

// PassiveEvent returns an EventOption that marks an event handler as passive.
// Passive handlers improve performance for high-frequency events by signaling
// that they will not call Event.PreventDefault(), allowing the browser to
// optimize event processing.
// More on passive listeners: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listeners
func () EventOption {
	return EventOption{
		name: "passive",
	}
}

type eventHandlers map[string]eventHandler

func ( eventHandlers) ( string,  EventHandler,  ...EventOption) {
	if  != nil {
		[] = makeEventHandler(, , ...)
	}
}

type eventHandler struct {
	event     string
	scope     string
	passive   bool
	goHandler EventHandler
	jsHandler Func
	close     func()
}

func makeEventHandler( string,  EventHandler,  ...EventOption) eventHandler {
	 := eventHandler{
		event:     ,
		goHandler: ,
	}

	for ,  := range  {
		switch .name {
		case "scope":
			.scope = .value

		case "passive":
			.passive = true
		}
	}
	return 
}

func ( eventHandler) ( eventHandler) bool {
	return .event == .event &&
		.scope == .scope &&
		.passive == .passive &&
		reflect.ValueOf(.goHandler).Pointer() == reflect.ValueOf(.goHandler).Pointer()
}

func ( eventHandler) () map[string]any {
	if .passive {
		return map[string]any{
			"passive": true,
		}
	}
	return nil
}

func trackMousePosition( Event) {
	 := .Get("clientX")
	if !.Truthy() {
		return
	}

	 := .Get("clientY")
	if !.Truthy() {
		return
	}

	Window().setCursorPosition(.Int(), .Int())
}