import { n as e } from "../../fn-BbK27qJY.js";
import t, { Suspense as n, useEffect as r, useMemo as i } from "react";
import a from "prop-types";
import { jsx as o, jsxs as s } from "react/jsx-runtime";
import { createSelector as c } from "reselect";
import { fromJS as l } from "immutable";
import { toAsyncAPIDocument as u } from "@asyncapi/parser";
import * as d from "comlink";
//#region src/plugins/editor-preview-asyncapi/components/EditorPreviewAsyncAPI/EditorPreviewAsyncAPI.tsx
var f = () => /* @__PURE__ */ t.createElement("div", null, "Loading..."), p = ({ getComponent: e, editorPreviewAsyncAPIActions: a, editorPreviewAsyncAPISelectors: o }) => {
	let s = e("EditorPreviewAsyncAPIParseErrors"), c = e("EditorPreviewAsyncAPIReactComponent"), l = o.selectIsParseInProgress(), u = o.selectIsParseSuccess(), d = o.selectIsParseFailure(), p = o.selectParseResult(), m = o.selectParseErrors(), h = i(() => ({ show: { errors: !0 } }), []);
	r(() => () => {
		a.previewUnmounted();
	}, [a]);
	let g = u || p !== null, _ = !g && (d || m.length > 0);
	return /* @__PURE__ */ t.createElement("div", { className: "swagger-editor__editor-preview-asyncapi" }, /* @__PURE__ */ t.createElement(n, { fallback: /* @__PURE__ */ t.createElement(f, null) }, l && !g && !_ && /* @__PURE__ */ t.createElement(f, null), g && /* @__PURE__ */ t.createElement(c, {
		schema: p,
		config: h
	}), _ && /* @__PURE__ */ t.createElement(s, { errors: m })));
}, m = t.lazy(() => import("@asyncapi/react-component/lib/esm/without-parser.js")), h = () => /* @__PURE__ */ o("div", {
	className: "swagger-editor__editor-preview-asyncapi-parse-errors",
	children: /* @__PURE__ */ o("div", {
		className: "swagger-ui",
		children: /* @__PURE__ */ o("div", {
			className: "version-pragma",
			children: /* @__PURE__ */ o("div", {
				className: "version-pragma__message version-pragma__message--missing",
				children: /* @__PURE__ */ s("div", { children: [/* @__PURE__ */ o("h3", { children: "Invalid AsyncAPI definition." }), /* @__PURE__ */ o("p", { children: "The provided AsyncAPI definition is not valid. Please check your syntax, and correct any discrepancies to ensure it adheres to the AsyncAPI standards." })] })
			})
		})
	})
}), g = (e, t) => {
	let n = ({ getComponent: n, editorSelectors: r }) => {
		let i = n("EditorPreviewAsyncAPI", !0);
		return r.selectIsContentTypeAsyncAPI() ? /* @__PURE__ */ o(i, {}) : /* @__PURE__ */ o(e, { ...t });
	};
	return n.propTypes = {
		getComponent: a.func.isRequired,
		editorSelectors: a.shape({ selectIsContentTypeAsyncAPI: a.func.isRequired }).isRequired
	}, n;
}, _ = class {
	constructor() {
		let e = new URL("../../asyncapi-parser.worker.js", "" + import.meta.url), t = e.protocol === "file:" ? new URL("./asyncapi-parser.worker.js", globalThis.MonacoEnvironment?.baseUrl ?? location.origin) : e;
		return new Worker(t, { type: "module" });
	}
}, v = null, y = null, b, x = async () => (v ||= d.wrap(new _()), v), S = async (e) => {
	let t = await x();
	y !== null && await y;
	let n = e?.__unstable?.resolver?.resolvers ?? [], r = n.map((e, t) => ({
		index: t,
		schema: e.schema,
		order: e.order,
		canRead: typeof e.canRead == "boolean" ? e.canRead : (t, n) => !!e.canRead(t, n)
	}));
	b = n.length ? d.proxy(async (e, t) => n[e].read({
		toString: () => t,
		valueOf: () => t
	})) : void 0;
	let i = {
		...e,
		__unstable: e?.__unstable ? {
			...e.__unstable,
			resolver: void 0
		} : void 0
	};
	y = t.init(i, r.length ? r : void 0, b), await y;
}, C = "editor_preview_asyncapi_preview_unmounted", w = "editor_preview_asyncapi_parse_started", T = "editor_preview_asyncapi_parse_success", E = "editor_preview_asyncapi_parse_failure", D = () => ({ type: C }), O = ({ content: e, requestId: t }) => ({
	type: w,
	payload: e,
	meta: { requestId: t }
}), k = ({ parseResult: e, content: t, requestId: n }) => ({
	type: T,
	payload: e,
	meta: {
		content: t,
		requestId: n
	}
}), A = ({ error: e, parseResult: t, content: n, requestId: r }) => ({
	type: E,
	payload: e,
	error: !0,
	meta: {
		content: n,
		requestId: r,
		parseResult: t
	}
}), j = (e, t = {}) => async (n) => {
	let { editorPreviewAsyncAPIActions: r, fn: i } = n, a = i.generateRequestId();
	r.parseStarted({
		content: e,
		requestId: a
	});
	try {
		let { parserOptions: n, parseOptions: i } = t;
		await S(n);
		let { schema: o, diagnostics: s } = await (await x()).parse(e, i ?? t), c = o ? u(o) : null;
		return c ? r.parseSuccess({
			parseResult: {
				document: c,
				diagnostics: s
			},
			content: e,
			requestId: a
		}) : r.parseFailure({
			error: /* @__PURE__ */ Error("Document is empty"),
			parseResult: { diagnostics: s },
			content: e,
			requestId: a
		});
	} catch (t) {
		return r.parseFailure({
			error: t,
			content: e,
			requestId: a
		});
	}
}, M = e((e, t) => ({ content: e }) => {
	let { editorSelectors: n, editorPreviewAsyncAPIActions: r } = t;
	n.selectIsContentTypeAsyncAPI() && r.parse(e);
}), N = "idle", P = "parsing", F = "success", I = "failure", L = {
	parseStatus: N,
	parseRequestId: null,
	parseResult: null,
	parseErrors: null
}, R = (e) => {
	let { parseStatus: t, parseRequestId: n, parseResult: r, parseErrors: i } = L;
	return e.merge({
		parseStatus: t,
		parseRequestId: n,
		parseResult: r,
		parseErrors: i
	});
}, z = (e, t) => e.merge({
	parseStatus: P,
	parseRequestId: t.meta.requestId
}), B = (e, t) => {
	let n = e.get("parseStatus") || "idle", r = e.get("parseRequestId");
	return n === "parsing" && r === t.meta.requestId ? e.merge({
		parseStatus: F,
		parseRequestId: null,
		parseResult: t.payload.document,
		parseErrors: null
	}) : e;
}, V = (e, t) => {
	let n = e.get("parseStatus") || "idle", r = e.get("parseRequestId");
	return n === "parsing" && r === t.meta.requestId ? e.merge({
		parseStatus: I,
		parseRequestId: null,
		parseResult: null,
		parseErrors: l(t.meta.parseResult?.diagnostics.filter((e) => e.severity === 0)) ?? L.parseResult
	}) : e;
}, H = {
	[C]: R,
	[w]: z,
	[T]: B,
	[E]: V
}, U = (e) => e, W = (e) => e.get("parseStatus", N), G = c(U, (e) => e.get("parseResult", L.parseResult)), K = c(U, (e) => {
	let t = e.get("parseErrors", L.parseErrors);
	return t === null ? [] : t.toJS();
}), q = c(K, (e, { monaco: t }) => t, (e, { modelVersionId: t }) => t, (e, t, n) => e.map((e) => ({
	message: e.message,
	startLineNumber: e.range.start.line + 1,
	endLineNumber: e.range.end.line + 1,
	startColumn: e.range.start.character + 1,
	endColumn: e.range.end.character + 1,
	code: "ASNCPRSR",
	severity: t.MarkerSeverity.Error,
	source: "@asyncapi/parser",
	modelVersionId: n
}))), J = c(W, (e) => e === P), Y = c(W, (e) => e === F), X = c(W, (e) => e === I), Z = class extends Array {
	dispose() {
		this.forEach((e) => e.dispose()), this.length = 0;
	}
}, Q = ({ system: e }) => {
	let { fn: t, editorPreviewAsyncAPISelectors: n, editorSelectors: r, monaco: i } = e, a = new Z();
	return a.push(t.registerMarkerDataProvider("apidom", {
		owner: "asyncapi-parser",
		async provideMarkerData(e) {
			let a = e.getValue(), o = e.getVersionId(), s = () => r.selectIsContentTypeAsyncAPI() && a === r.selectContent() && !n.selectIsParseInProgress();
			try {
				return await t.waitUntil(s), n.selectIsParseSuccess() ? [] : n.selectParseMarkers({
					monaco: i,
					modelVersionId: o
				});
			} catch {
				return [];
			}
		}
	})), a.push(i.editor.onDidCreateEditor((e) => {
		a.push(e.onDidDispose(() => {
			a.forEach((e) => {
				e.dispose();
			}), a.length = 0;
		}));
	})), a;
};
//#endregion
//#region src/plugins/editor-preview-asyncapi/after-load.js
function $(e) {
	let { monaco: t, fn: n } = e, r = t !== void 0, i = n.getApiDOMWorker !== void 0, a = this.statePlugins.editorPreviewAsyncAPI;
	r && i && (a.disposables && a.disposables.dispose(), a.disposables = Q({ system: e }));
}
//#endregion
//#region src/plugins/editor-preview-asyncapi/index.js
var ee = () => ({
	afterLoad: $,
	components: {
		EditorPreviewAsyncAPI: p,
		EditorPreviewAsyncAPIParseErrors: h,
		EditorPreviewAsyncAPIReactComponent: m
	},
	wrapComponents: { EditorPreview: g },
	statePlugins: {
		editor: { wrapActions: { detectContentTypeSuccess: M } },
		editorPreviewAsyncAPI: {
			actions: {
				previewUnmounted: D,
				parse: j,
				parseStarted: O,
				parseSuccess: k,
				parseFailure: A
			},
			selectors: {
				selectParseStatus: W,
				selectIsParseInProgress: J,
				selectIsParseSuccess: Y,
				selectIsParseFailure: X,
				selectParseResult: G,
				selectParseErrors: K,
				selectParseMarkers: q
			},
			reducers: H
		}
	}
});
//#endregion
export { ee as default };

//# sourceMappingURL=index.js.map