import e from "../CodeContent.js";
import { useFieldConfig as t } from "../hooks/useFieldConfig.js";
import { BaseFieldWrapper as n } from "./BaseFieldWrapper.js";
import r from "react";
import { jsx as i, jsxs as a } from "react/jsx-runtime";
import { Check as o, Copy as s } from "lucide-react";
import { Controller as c } from "react-hook-form";
//#region src/components/dynamic-form/fields/CodeViewField.tsx
var l = /^```[\w-]*\n?([\s\S]*?)\n?```$/, u = (e) => {
	let t = e.match(l);
	return t ? t[1] : e;
}, d = (e) => !e || l.test(e.trim()) ? e : "```\n" + e + "\n```", f = ({ name: l, schema: f, ui: p, required: m, form: h, disabled: g }) => {
	let [_, v] = r.useState(!1), y = t(l, f, p, g), b = async (e) => {
		try {
			let t = u(e);
			await navigator.clipboard.writeText(t), v(!0), setTimeout(() => v(!1), 2e3);
		} catch (e) {
			console.error("Failed to copy:", e);
		}
	};
	return /* @__PURE__ */ i(c, {
		name: l,
		control: h.control,
		defaultValue: y.defaultValue || "",
		render: ({ field: t }) => /* @__PURE__ */ i(n, {
			name: l,
			label: y.fieldLabel,
			required: m,
			error: y.error,
			helpText: y.helpText,
			description: y.description,
			children: /* @__PURE__ */ a("div", {
				className: "relative w-full overflow-hidden rounded-md border",
				...y.getAriaProps(),
				children: [/* @__PURE__ */ i("button", {
					type: "button",
					onClick: () => void b(String(t.value ?? "")),
					disabled: !t.value,
					className: "bg-background/80 hover:bg-background absolute top-2 right-2 z-10 rounded-md border p-2 transition-colors disabled:cursor-not-allowed disabled:opacity-50",
					title: _ ? "Copied!" : "Copy to clipboard",
					"aria-label": _ ? "Copied to clipboard" : "Copy code to clipboard",
					children: _ ? /* @__PURE__ */ i(o, {
						className: "h-4 w-4 text-green-600",
						"aria-hidden": "true"
					}) : /* @__PURE__ */ i(s, {
						className: "h-4 w-4",
						"aria-hidden": "true"
					})
				}), /* @__PURE__ */ i(e, { content: d(String(t.value ?? "")) })]
			})
		})
	});
};
//#endregion
export { f as CodeViewField };
