import { Input } from "../../ui/input.js";
import { Label } from "../../ui/label.js";
import { buildTextValidationRules } from "./utils/text-validation-utils.js";
import { useFieldConfig } from "../hooks/useFieldConfig.js";
import { BaseFieldWrapper } from "./BaseFieldWrapper.js";
import { c } from "react/compiler-runtime";
import React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Controller } from "react-hook-form";
const getInputType = (t) => {
	if (t.format) switch (t.format) {
		case "email": return "email";
		case "url": return "url";
		case "date": return "date";
		case "time": return "time";
		case "datetime": return "datetime-local";
		case "phone": return "tel";
	}
	switch (t.type) {
		case "integer":
		case "number": return "number";
		case "string":
		default: return "text";
	}
};
var isNumericType = (t) => t.type === "number" || t.type === "integer";
const InputField = (p) => {
	let m = c(36), { name: h, schema: g, ui: _, required: v, form: y, disabled: b, viewOnly: x } = p, S = useFieldConfig(h, g, _, b), C;
	m[0] === g ? C = m[1] : (C = getInputType(g), m[0] = g, m[1] = C);
	let w = C, T;
	m[2] !== g.examples?.[0] || m[3] !== g.placeholder ? (T = g.placeholder || g.examples?.[0]?.toString() || "", m[2] = g.examples?.[0], m[3] = g.placeholder, m[4] = T) : T = m[4];
	let E = T, D;
	m[5] !== v || m[6] !== g ? (D = buildTextValidationRules(g, v), m[5] = v, m[6] = g, m[7] = D) : D = m[7];
	let O = D, k;
	m[8] === g ? k = m[9] : (k = isNumericType(g), m[8] = g, m[9] = k);
	let A = k;
	if (x) {
		let t;
		m[10] === S.fieldLabel ? t = m[11] : (t = /* @__PURE__ */ jsx(Label, {
			className: "text-muted-foreground mb-1 block text-sm",
			children: S.fieldLabel
		}), m[10] = S.fieldLabel, m[11] = t);
		let u = S.defaultValue ?? "", d;
		m[12] !== y.control || m[13] !== h || m[14] !== u ? (d = /* @__PURE__ */ jsx(Controller, {
			name: h,
			control: y.control,
			defaultValue: u,
			render: _temp
		}), m[12] = y.control, m[13] = h, m[14] = u, m[15] = d) : d = m[15];
		let f;
		return m[16] !== t || m[17] !== d ? (f = /* @__PURE__ */ jsxs("div", {
			className: "mt-4 mb-8 block",
			children: [t, d]
		}), m[16] = t, m[17] = d, m[18] = f) : f = m[18], f;
	}
	let j = S.defaultValue ?? (A ? void 0 : ""), M;
	m[19] !== S || m[20] !== w || m[21] !== A || m[22] !== h || m[23] !== E || m[24] !== v || m[25] !== g.maxLength || m[26] !== g.maximum || m[27] !== g.minimum || m[28] !== g.multipleOf ? (M = (l) => {
		let { field: u } = l;
		return /* @__PURE__ */ jsx(BaseFieldWrapper, {
			name: h,
			label: S.fieldLabel,
			required: v,
			error: S.error,
			helpText: S.helpText,
			description: S.description,
			children: /* @__PURE__ */ jsx(Input, {
				...u,
				id: h,
				onChange: (t) => {
					if (!S.isReadOnly) if (A) {
						let l = t.target.value;
						u.onChange(l === "" ? void 0 : Number(l));
					} else u.onChange(t);
				},
				value: u.value ?? "",
				type: w,
				placeholder: E,
				disabled: S.isDisabled,
				readOnly: S.isReadOnly,
				min: g.minimum,
				max: g.maximum,
				step: w === "number" && g.multipleOf ? g.multipleOf : void 0,
				maxLength: g.maxLength,
				className: S.error ? "border-destructive focus-visible:ring-destructive" : "",
				...S.getAriaProps()
			})
		});
	}, m[19] = S, m[20] = w, m[21] = A, m[22] = h, m[23] = E, m[24] = v, m[25] = g.maxLength, m[26] = g.maximum, m[27] = g.minimum, m[28] = g.multipleOf, m[29] = M) : M = m[29];
	let N;
	return m[30] !== y.control || m[31] !== h || m[32] !== j || m[33] !== M || m[34] !== O ? (N = /* @__PURE__ */ jsx(Controller, {
		name: h,
		control: y.control,
		defaultValue: j,
		rules: O,
		render: M
	}), m[30] = y.control, m[31] = h, m[32] = j, m[33] = M, m[34] = O, m[35] = N) : N = m[35], N;
};
function _temp(t) {
	let { field: l } = t;
	return /* @__PURE__ */ jsx("div", {
		className: "text-sm",
		children: l.value ?? "—"
	});
}
export { InputField };
