package ui

import (
	"strconv"

	"github.com/a-h/templ"
)

// mergeAttrs returns a new templ.Attributes containing every key from base,
// with override keys taking precedence on collision. Either input may be nil.
func mergeAttrs(base, override templ.Attributes) templ.Attributes {
	out := templ.Attributes{}
	for k, v := range base {
		out[k] = v
	}
	for k, v := range override {
		out[k] = v
	}
	return out
}

// buttonType returns "button" if value is empty, otherwise value.
// Used to set default type="button" on button elements without an explicit type.
func buttonType(value string) string {
	if value == "" {
		return "button"
	}
	return value
}

// inputType returns "text" if value is empty, otherwise value.
// Used to set default type="text" on input elements without an explicit type.
func inputType(value string) string {
	if value == "" {
		return "text"
	}
	return value
}

// inputID returns id if non-empty, otherwise name.
// Used to derive an implicit id from the name attribute when no id is provided.
func inputID(id string, name string) string {
	if id != "" {
		return id
	}
	return name
}

// textareaRows returns strconv.Itoa(rows) if rows > 0, else "4".
// Used to set a safe default row count on textarea elements.
func textareaRows(rows int) string {
	if rows <= 0 {
		rows = 4
	}
	return strconv.Itoa(rows)
}
