import { t as e } from "./useIntl-CtHcH0Kd.js";
import { t } from "./editor-wrap-DQdG3MHA.js";
import { forwardRef as n, useEffect as r, useImperativeHandle as i, useRef as a } from "react";
import { jsx as o, jsxs as s } from "react/jsx-runtime";
import { ImportOutlined as c, LoadingOutlined as l } from "@ant-design/icons";
import { Button as u, Typography as d, Upload as f, message as p } from "antd";
import m from "styled-components";
import h, { loader as g } from "@monaco-editor/react";
import "antd/lib/upload";
import * as _ from "monaco-editor";
import { yamlDefaults as v } from "monaco-yaml";
//#region src/lib/components/yaml-editor/editor.tsx
g.config({ monaco: _ });
var y = "inmemory://model/config.yaml", b = n((e, n) => {
	let { value: s, height: c = 380, theme: u = "vs-dark", header: d, variant: f = "borderless", schema: p, placeholder: m } = e, g = a(null), _ = a(null), b = a(null), x = (e) => {
		let t = e.Uri.parse(y);
		v.setDiagnosticsOptions({
			validate: !1,
			enableSchemaRequest: !0,
			hover: !1,
			schemas: [{
				uri: "http://example.com/schema-name.json",
				fileMatch: [t.toString()],
				schema: p
			}]
		});
	}, S = (e, t) => {
		g.current = e, _.current = t;
	}, C = () => {
		g.current && setTimeout(() => {
			g.current?.getAction?.("editor.action.formatDocument")?.run().then(() => {
				console.log("format success");
			});
		}, 100);
	}, w = () => {
		let e = g.current?.getModel()?.uri;
		return _.current?.editor.getModelMarkers({ resource: e });
	};
	return i(n, () => ({
		format: () => {
			C();
		},
		getValue: () => g.current?.getValue?.(),
		setValue: (e) => {
			g.current?.setValue?.(e);
		},
		dispose: () => {
			g.current?.dispose?.(), b.current?.dispose?.();
		},
		validate() {
			return w();
		},
		editor: g.current
	})), r(() => {
		C();
	}, [s]), /* @__PURE__ */ o(t, {
		header: d,
		variant: f,
		children: /* @__PURE__ */ o(h, {
			path: y,
			defaultPath: y,
			height: c,
			theme: u,
			className: "monaco-editor",
			defaultLanguage: "yaml",
			language: "yaml",
			value: s,
			options: {
				minimap: { enabled: !1 },
				quickSuggestions: !0,
				suggestOnTriggerCharacters: !0,
				fontSize: 14,
				scrollbar: {
					verticalScrollbarSize: 6,
					horizontalScrollbarSize: 6
				}
			},
			loading: /* @__PURE__ */ o(l, { style: { fontSize: 24 } }),
			beforeMount: x,
			onMount: S
		})
	});
}), { Text: x } = d;
g.config({ monaco: _ });
var S = m.div`
  position: relative;
  border: 1px solid var(--ant-color-border);
  border-radius: var(--ant-border-radius);
  .monaco-editor .scroll-decoration {
    box-shadow: none;
  }
`, C = m(x)`
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 4px 6px;
  background-color: var(--ant-color-bg-elevated);
  border-radius: 0 0 var(--ant-border-radius) var(--ant-border-radius);
`, w = m.div`
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
  padding-inline: 10px;
  font-size: 14px;
  border-bottom: 1px solid var(--ant-color-border);
  background-color: var(--ant-color-fill-quaternary);
`, T = n((t, n) => {
	let { value: l, height: d = 380, variant: m = "borderless", isDarkTheme: h, schema: g, placeholder: _, validateMessage: v, title: y, onUpload: x } = t, T = e(), E = a(null), D = (e) => {
		E.current?.setValue?.(e);
	};
	return i(n, () => ({
		format: () => {
			E.current?.format();
		},
		getValue: () => E.current?.getValue?.(),
		setValue: (e) => {
			E.current?.setValue?.(e);
		},
		dispose: () => {
			E.current?.dispose?.();
		},
		validate() {
			return E.current?.validate();
		},
		editor: E.current
	})), r(() => {
		E.current?.format();
	}, [l]), /* @__PURE__ */ s(S, {
		style: { minHeight: d },
		children: [/* @__PURE__ */ o(b, {
			ref: E,
			header: /* @__PURE__ */ s(w, { children: [/* @__PURE__ */ o("span", {
				className: "title",
				children: y || "YAML"
			}), /* @__PURE__ */ o(f, {
				name: "file",
				multiple: !1,
				beforeUpload: (e) => {
					e.type === "application/x-yaml" || e.type === "text/yaml" || e.name.endsWith(".yaml") || e.name.endsWith(".yml") || p.error("You can only upload YAML file!");
					let t = new FileReader();
					return t.onload = (e) => {
						let t = e.target?.result;
						typeof t == "string" ? (x?.(t), D(t)) : p.error("Failed to read file content!");
					}, t.readAsText(e), !1;
				},
				showUploadList: !1,
				accept: ".yaml,.yml,text/yaml,application/x-yaml",
				children: /* @__PURE__ */ o(u, {
					icon: /* @__PURE__ */ o(c, {}),
					type: "text",
					size: "small",
					children: T.formatMessage({ id: "common.button.import" })
				})
			})] }),
			variant: m,
			height: d,
			theme: h ? "vs-dark" : "light",
			value: l,
			placeholder: _,
			schema: g
		}), v && /* @__PURE__ */ o(C, {
			type: "danger",
			children: v
		})]
	});
});
//#endregion
export { T as YamlEditor };
