import { at as e, g as t, v as n, vt as r } from "../dist-DgSmYkuV.js";
import { t as i } from "../base-B7MLNUXu.js";
import { n as a } from "../dist-CC_emMZy.js";
import { n as o, t as s } from "../dist-CYSk5SkB.js";
import { fluxl as c, fluxlCompletions as l } from "./languages/fluxl.js";
import { yaral as u, yaralCompletions as d } from "./languages/yaral.js";
import { yara as f, yaraCompletions as p } from "./languages/yara.js";
import { t as m } from "../json-D8YcUjFo.js";
import { n as h, t as g } from "../sql-BTWxpluA.js";
import { t as _ } from "../yaml-BAVAG9c4.js";
import { a as v, i as y, n as b, o as x, r as S, t as C } from "../shell-CH7FHBJf.js";
//#region src/editors/code-editor.ts
function w(e) {
	switch (e) {
		case "fluxl": return c();
		case "sql": return g();
		case "yaral": return u();
		case "yara": return f();
		case "json": return m();
		case "yaml":
		case "yml": return _();
		case "python":
		case "py": return x();
		case "javascript":
		case "js":
		case "jsx": return y();
		case "typescript":
		case "ts":
		case "tsx": return v();
		case "rust":
		case "rs": return S();
		case "markdown":
		case "md": return b();
		case "bash":
		case "sh":
		case "shell":
		case "zsh": return C();
		default: return [];
	}
}
function T(e) {
	switch (e) {
		case "fluxl": return l;
		case "sql": return h;
		case "yaral": return d;
		case "yara": return p;
		default: return [];
	}
}
function E(e, t) {
	return (n) => {
		let r = n.matchBefore(/[\w.]+/);
		if (!r && !n.explicit) return null;
		let i = r?.text.toLowerCase() ?? "", a = T(e), o = (t.tables ?? []).map((e) => {
			let t = {
				label: e.name,
				type: "class"
			};
			return e.description !== void 0 && (t.info = e.description), t;
		}), s = (t.tables ?? []).flatMap((e) => e.columns.map((t) => ({
			label: t.name,
			type: "property",
			info: `${e.name}.${t.name} (${t.dataType})`
		}))), c = (t.functions ?? []).map((e) => ({
			label: `${e}()`,
			type: "function"
		})), l = (t.operators ?? []).map((e) => ({
			label: e,
			type: "keyword"
		})), u = (t.custom ?? []).map((e) => {
			let t = {
				label: e.label,
				type: e.type
			};
			return e.info !== void 0 && (t.info = e.info), e.detail !== void 0 && (t.detail = e.detail), t;
		}), d = [
			...a,
			...o,
			...s,
			...c,
			...l,
			...u
		].filter((e) => i.length > 0 ? e.label.toLowerCase().startsWith(i) : !0);
		return {
			from: r?.from ?? n.pos,
			options: d.slice(0, 100)
		};
	};
}
function D(e, t) {
	return async (n) => {
		let r = n.state.doc.toString();
		if (!r.trim()) return [];
		try {
			let i = t(), a = e.transformRequest ? e.transformRequest(r, i) : {
				query: r,
				type: i
			}, o = await fetch(e.endpoint, {
				method: e.method ?? "POST",
				headers: {
					"Content-Type": "application/json",
					...e.headers ?? {}
				},
				body: JSON.stringify(a)
			});
			if (!o.ok) return [];
			let s = await o.json(), c = e.transformResponse ? e.transformResponse(s) : s;
			return c.valid ? [] : O(n, c.errors ?? []);
		} catch {
			return [];
		}
	};
}
function O(e, t) {
	return t.map((t) => {
		let n = t.line ?? 1, r = t.column ?? 1, i = Math.min(Math.max(1, n), e.state.doc.lines), a = e.state.doc.line(i), o = Math.min(a.to, a.from + Math.max(0, r - 1));
		return {
			from: o,
			to: Math.min(a.to, o + 1),
			severity: t.severity ?? "error",
			message: t.message
		};
	});
}
var k = class extends i {
	constructor(i) {
		let c = new r(), l = new r(), u = new r(), d = i.language ?? "fluxl", f = i.keybindings ? e.of(Object.entries(i.keybindings).map(([e, t]) => ({
			key: e,
			run: () => (t(), !0)
		}))) : [], p = [
			c.of(w(d)),
			u.of(i.autocomplete ? a({ override: [E(d, i.autocomplete)] }) : a()),
			l.of(i.validation ? o(D(i.validation, () => this.currentLanguage), { delay: i.validation.debounceMs ?? 800 }) : []),
			s(),
			t(),
			e.of(n),
			f
		];
		super(i, p), this.languageCompartment = c, this.lintCompartment = l, this.autocompleteCompartment = u, this.currentLanguage = d, this.currentSchema = i.autocomplete ?? null;
	}
	setLanguage(e) {
		this.currentLanguage = e, this.view.dispatch({ effects: this.languageCompartment.reconfigure(w(e)) }), this.currentSchema && this.setAutocompleteSchema(this.currentSchema);
	}
	getLanguage() {
		return this.currentLanguage;
	}
	setAutocompleteSchema(e) {
		this.currentSchema = e, this.view.dispatch({ effects: this.autocompleteCompartment.reconfigure(a({ override: [E(this.currentLanguage, e)] })) });
	}
	setErrors(e) {
		let t = O(this.view, e);
		this.view.dispatch({ effects: this.lintCompartment.reconfigure(o(() => t, { delay: 0 })) });
	}
	clearErrors() {
		this.view.dispatch({ effects: this.lintCompartment.reconfigure([]) });
	}
	insertText(e) {
		let t = this.view.state.selection.main;
		this.view.dispatch({ changes: {
			from: t.from,
			to: t.to,
			insert: e
		} }), this.view.focus();
	}
	getSelection() {
		let e = this.view.state.selection.main;
		return this.view.state.sliceDoc(e.from, e.to);
	}
};
//#endregion
export { k as CodeEditor };

//# sourceMappingURL=code-editor.js.map