import { getAttributeChanges as e, getAttributeListChanges as t, getElementUpdate as n, matchesElementName as r } from "../../internal/utils/dom.js";
import { ControlMessage as i } from "../control-message/control-message2.js";
//#region src/forms/utils/states.ts
var a = "input, select, selectmenu, textarea, [nve-control]";
function o(e, t) {
	if (!e.input.form?.noValidate && !e.input.formNoValidate && !e.input.hasAttribute("formnovalidate")) {
		f(t);
		let n = () => {
			e.input.validity?.valid && (e._internals.states.delete("invalid"), e._internals.states.add("valid"), e.status = null), m(e, t);
		}, r = () => {
			e.status = null, e._internals.states.delete("valid"), e._internals.states.delete("invalid"), f(t);
		};
		e.input.addEventListener("blur", () => {
			e.input.checkValidity(), n();
		}), e.input.addEventListener("input", () => {
			n();
		}), e.input.addEventListener("invalid", () => {
			t.find((e) => e.error) && (f(t), p(e, t)), e.status = "error", e._internals.states.delete("valid"), e._internals.states.add("invalid");
		}), e.addEventListener("reset", () => r()), e.input.form?.addEventListener("reset", () => r());
	}
	e.shadowRoot.addEventListener("slotchange", () => {
		let t = Array.from(e.querySelectorAll(i.metadata.tag));
		e._internals.states.delete("valid"), e._internals.states.delete("invalid"), t.find((e) => !e.hidden && (e.status === "error" || e.error)) ? e._internals.states.add("invalid") : e._internals.states.add("valid");
	});
}
function s(e) {
	let t = [], r = e._internals.states;
	return e.input.checked ? r.add("checked") : r.delete("checked"), e.input.indeterminate ? r.add("indeterminate") : r.delete("indeterminate"), e.input.addEventListener("focus", () => e._internals.states.add("focus")), e.input.addEventListener("input", () => e._internals.states.add("dirty")), e.input.addEventListener("blur", () => {
		e._internals.states.add("touched"), e._internals.states.delete("focus");
	}), e.input.getRootNode().addEventListener("change", (t) => {
		t.target.name === e.input?.name && (e.input.checked ? r.add("checked") : r.delete("checked"));
	}), e.input.form?.addEventListener("reset", () => {
		e._internals.states.delete("touched"), e._internals.states.delete("dirty"), e._internals.states.delete("error"), e._internals.states.delete("success"), e.requestUpdate();
	}), t.push(n(e.input, "readonly", (e) => e === "" || e ? r.add("readonly") : r.delete("readonly")), n(e.input, "checked", () => e.input.checked ? r.add("checked") : r.delete("checked")), n(e.input, "disabled", (e) => e === "" || e ? r.add("disabled") : r.delete("disabled")), n(e.input, "indeterminate", () => e.input.indeterminate ? r.add("indeterminate") : r.delete("indeterminate"))), t;
}
function c(t) {
	return l(t), e(t, "disabled", () => l(t));
}
function l(e) {
	Array.from(e.inputs).find((e) => e.disabled) ? e._internals.states.add("disabled") : e._internals.states.delete("disabled");
}
function u(e, n) {
	d(e, n.find((e) => !e.hidden));
	let a = [];
	return a.push(t(e, ["hidden", "status"], (t) => {
		let n = t.target;
		r(n, i) && d(e, n);
	})), e.shadowRoot.addEventListener("slotchange", () => {
		let t = Array.from(e.querySelectorAll(i.metadata.tag)).find((e) => e.status && !e.hidden);
		e._internals.states.delete("error"), e._internals.states.delete("success"), t && e._internals.states.add(t.status);
	}), a;
}
function d(e, t) {
	e._internals.states.delete("error"), e._internals.states.delete("success"), t?.status?.length && !t?.hidden && e._internals.states.add(t.status);
}
function f(e) {
	e.filter((e) => e.hasAttribute("error")).forEach((e) => e.setAttribute("hidden", ""));
}
function p(e, t) {
	t.find((t) => t.error && e.input.validity[t.error])?.removeAttribute("hidden");
}
function m(e, t) {
	t.find((e) => e.error) && e.input.validity.valid && t.filter((t) => t.error && !e.input.validity[t.error]).forEach((e) => e.setAttribute("hidden", ""));
}
//#endregion
export { a as inputQuery, c as setupControlGroupStates, s as setupControlStates, u as setupControlStatusStates, o as setupControlValidationStates };

//# sourceMappingURL=states.js.map