import CodeContent_default from "../CodeContent.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 { Check, Copy } from "lucide-react";
import { Controller } from "react-hook-form";
var stripCodeFence = (e) => {
	let u = e.match(/^```[\w-]*\n?([\s\S]*?)\n?```$/);
	return u ? u[1] : e;
};
const CodeViewField = (f) => {
	let p = c(11), { name: m, schema: h, ui: g, required: _, form: v, disabled: y } = f, [b, x] = React.useState(!1), S = useFieldConfig(m, h, g, y), C;
	p[0] === Symbol.for("react.memo_cache_sentinel") ? (C = async (e) => {
		try {
			let u = stripCodeFence(e);
			await navigator.clipboard.writeText(u), x(!0), setTimeout(() => x(!1), 2e3);
		} catch (e) {
			let u = e;
			console.error("Failed to copy:", u);
		}
	}, p[0] = C) : C = p[0];
	let w = C, T = S.defaultValue || "", E;
	p[1] !== S || p[2] !== b || p[3] !== m || p[4] !== _ ? (E = (u) => {
		let { field: d } = u;
		return /* @__PURE__ */ jsx(BaseFieldWrapper, {
			name: m,
			label: S.fieldLabel,
			required: _,
			error: S.error,
			helpText: S.helpText,
			description: S.description,
			children: /* @__PURE__ */ jsxs("div", {
				className: "relative w-full overflow-hidden rounded-md border",
				...S.getAriaProps(),
				children: [/* @__PURE__ */ jsx("button", {
					type: "button",
					onClick: () => void w(String(d.value ?? "")),
					disabled: S.isDisabled || !d.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: b ? "Copied!" : "Copy to clipboard",
					"aria-label": b ? "Copied to clipboard" : "Copy code to clipboard",
					children: b ? /* @__PURE__ */ jsx(Check, {
						className: "h-4 w-4 text-green-600",
						"aria-hidden": "true"
					}) : /* @__PURE__ */ jsx(Copy, {
						className: "h-4 w-4",
						"aria-hidden": "true"
					})
				}), /* @__PURE__ */ jsx(CodeContent_default, { content: String(d.value ?? "") })]
			})
		});
	}, p[1] = S, p[2] = b, p[3] = m, p[4] = _, p[5] = E) : E = p[5];
	let D;
	return p[6] !== v.control || p[7] !== m || p[8] !== T || p[9] !== E ? (D = /* @__PURE__ */ jsx(Controller, {
		name: m,
		control: v.control,
		defaultValue: T,
		render: E
	}), p[6] = v.control, p[7] = m, p[8] = T, p[9] = E, p[10] = D) : D = p[10], D;
};
export { CodeViewField };
