import { n as e } from "../../fn-BbK27qJY.js";
import { detectionRegExp as t } from "@swagger-api/apidom-parser-adapter-asyncapi-json-2";
import { detectionRegExp as n } from "@swagger-api/apidom-parser-adapter-asyncapi-yaml-2";
import { detectionRegExp as r } from "@swagger-api/apidom-parser-adapter-openapi-json-2";
import { detectionRegExp as i } from "@swagger-api/apidom-parser-adapter-openapi-yaml-2";
import { detectionRegExp as a } from "@swagger-api/apidom-parser-adapter-openapi-json-3-0";
import { detectionRegExp as o } from "@swagger-api/apidom-parser-adapter-openapi-yaml-3-0";
import { detectionRegExp as s } from "@swagger-api/apidom-parser-adapter-openapi-json-3-1";
import { detectionRegExp as c } from "@swagger-api/apidom-parser-adapter-openapi-yaml-3-1";
import { detectionRegExp as l } from "@swagger-api/apidom-parser-adapter-openapi-json-3-2";
import { detectionRegExp as u } from "@swagger-api/apidom-parser-adapter-openapi-yaml-3-2";
import { detect as d, detectionRegExp as f } from "@swagger-api/apidom-parser-adapter-api-design-systems-json";
import { detect as p, detectionRegExp as m } from "@swagger-api/apidom-parser-adapter-api-design-systems-yaml";
import { createSelector as h } from "reselect";
import g from "js-yaml";
//#region src/plugins/editor-content-type/actions.js
var _ = "editor_content_type_detect_started", v = "editor_content_type_detect_success", y = "editor_content_type_detect_failure", b = ({ content: e, requestId: t }) => ({
	type: _,
	payload: e,
	meta: { requestId: t }
}), x = ({ contentType: e, content: t, requestId: n }) => ({
	type: v,
	payload: e,
	meta: {
		content: t,
		requestId: n
	}
}), S = ({ error: e, content: t, requestId: n }) => ({
	type: y,
	payload: e,
	error: !0,
	meta: {
		content: t,
		requestId: n
	}
}), C = (e) => async (h) => {
	let { editorActions: g, fn: _ } = h, v = _.generateRequestId();
	g.detectContentTypeStarted({
		content: e,
		requestId: v
	});
	try {
		let h = e.match(t);
		if (h !== null && _.isValidJSONObject(e)) {
			let { groups: t } = h, n = `application/vnd.aai.asyncapi+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let y = e.match(n);
		if (y !== null && _.isValidYAMLObject(e)) {
			let { groups: t } = y, n = `application/vnd.aai.asyncapi+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let b = e.match(r);
		if (b !== null && _.isValidJSONObject(e)) {
			let { groups: t } = b, n = `application/vnd.oai.openapi+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let x = e.match(i);
		if (x !== null && _.isValidYAMLObject(e)) {
			let { groups: t } = x, n = `application/vnd.oai.openapi+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let S = e.match(/"asyncapi"\s*:\s*"(?<version_json>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))"/);
		if (S !== null && _.isValidJSONObject(e)) {
			let { groups: t } = S, n = `application/vnd.aai.asyncapi+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let C = e.match(/(?<YAML>^(["']?)asyncapi\2\s*:\s*(["']?)(?<version_yaml>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?<JSON>"asyncapi"\s*:\s*"(?<version_json>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))")/m);
		if (C !== null && _.isValidYAMLObject(e)) {
			let { groups: t } = C, n = `application/vnd.aai.asyncapi+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let w = e.match(a);
		if (w !== null && _.isValidJSONObject(e)) {
			let { groups: t } = w, n = `application/vnd.oai.openapi+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let T = e.match(o);
		if (T !== null && _.isValidYAMLObject(e)) {
			let { groups: t } = T, n = `application/vnd.oai.openapi+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let E = e.match(s);
		if (E !== null && _.isValidJSONObject(e)) {
			let { groups: t } = E, n = `application/vnd.oai.openapi+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let D = e.match(c);
		if (D !== null && _.isValidYAMLObject(e)) {
			let { groups: t } = D, n = `application/vnd.oai.openapi+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let O = e.match(l);
		if (O !== null && _.isValidJSONObject(e)) {
			let { groups: t } = O, n = `application/vnd.oai.openapi+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let k = e.match(u);
		if (k !== null && _.isValidYAMLObject(e)) {
			let { groups: t } = k, n = `application/vnd.oai.openapi+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let A = e.match(f);
		if (A !== null && await d(e)) {
			let { groups: t } = A, n = `application/vnd.aai.apidesignsystems+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let j = e.match(m);
		if (j !== null && await p(e)) {
			let { groups: t } = j, n = `application/vnd.aai.apidesignsystems+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let M = e.match(/"\$schema"\s*:\s*"(?<version_json>https:\/\/json-schema.org\/draft\/2020-12\/schema)"/);
		if (M !== null && _.isValidJSONObject(e)) {
			let { groups: t } = M, n = `application/schema+json;version=${t?.version_json}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		let N = e.match(/(?<YAML>^(["']?)\$schema\2\s*:\s*(["']?)(?<version_yaml>https:\/\/json-schema.org\/draft\/2020-12\/schema)|(?<JSON>"\$schema"\s*:\s*"(?<version_json>https:\/\/json-schema.org\/draft\/2020-12\/schema)"))/m);
		if (N !== null && _.isValidYAMLObject(e)) {
			let { groups: t } = N, n = `application/schema+yaml;version=${t?.version_json ?? t?.version_yaml}`;
			return g.detectContentTypeSuccess({
				contentType: n,
				content: e,
				requestId: v
			});
		}
		return _.isValidJSON(e) ? g.detectContentTypeSuccess({
			contentType: "application/json",
			content: e,
			requestId: v
		}) : _.isValidYAML(e) ? g.detectContentTypeSuccess({
			contentType: "text/yaml",
			content: e,
			requestId: v
		}) : g.detectContentTypeFailure({
			error: /* @__PURE__ */ Error("No content type detected"),
			content: e,
			requestId: v
		});
	} catch (t) {
		return g.detectContentTypeFailure({
			error: t,
			content: e,
			requestId: v
		});
	}
}, w = e((e, t) => (e) => {
	let { editorActions: n } = t;
	n.detectContentType(e, t);
}), T = "idle", E = "detecting", D = "success", O = "failure", k = {
	contentTypeDetectionStatus: T,
	contentTypeDetectionRequestId: null,
	contentType: null,
	contentTypeError: null
}, A = (e, t) => e.merge({
	contentTypeDetectionStatus: E,
	contentTypeDetectionRequestId: t.meta.requestId
}), j = (e, t) => {
	let n = e.get("contentTypeDetectionStatus") || "idle", r = e.get("contentTypeDetectionRequestId");
	return n === "detecting" && r === t.meta.requestId ? e.merge({
		contentTypeDetectionStatus: D,
		contentTypeDetectionRequestId: null,
		contentType: t.payload,
		contentTypeError: null
	}) : e;
}, M = (e, t) => {
	let n = e.get("contentTypeDetectionStatus") || "idle", r = e.get("contentTypeDetectionRequestId");
	return n === "detecting" && r === t.meta.requestId ? e.merge({
		contentTypeDetectionStatus: O,
		contentTypeDetectionRequestId: null,
		contentType: null,
		contentTypeError: t.payload.message
	}) : e;
}, N = {
	[_]: A,
	[v]: j,
	[y]: M
}, P = (e) => e.get("contentTypeDetectionStatus"), F = h(P, (e) => e === E), I = (e) => e.get("contentType") || k.contentType, L = h(I, (e) => e !== null && e.startsWith("application/vnd.oai.openapi")), R = h(L, I, (e, t) => e && /version=2\.0$/.test(t)), z = h(L, I, (e, t) => e && /version=3\.0\.\d+$/.test(t)), B = h(L, I, (e, t) => e && /version=3\.1\.\d+$/.test(t)), V = h(L, I, (e, t) => e && /version=3\.2\.\d+$/.test(t)), H = h(I, (e) => e !== null && e.startsWith("application/vnd.aai.asyncapi")), U = h(H, I, (e, t) => e && /version=2.\d+\.\d+$/.test(t)), W = h(I, (e) => e !== null && e.startsWith("application/vnd.aai.apidesignsystems")), G = h(I, (e) => e !== null && e.startsWith("application/schema")), K = h(G, I, (e, t) => e && t.endsWith("https://json-schema.org/draft/2020-12/schema")), q = h(I, (e) => e !== null && (e === "application/json" || e.includes("+json"))), J = h(I, (e) => e !== null && (e === "text/yaml" || e.includes("+yaml"))), Y = h(q, J, (e, t) => e ? ".json" : t ? ".yaml" : ".txt"), X = h(R, z, B, V, U, (e, t, n, r, i) => e ? "openapi2" : t ? "openapi3_0" : n ? "openapi3_1" : r ? "openapi3_2" : i ? "asyncapi2" : "definition"), Z = h(Y, X, (e, t) => `${t}${e}`), Q = (e) => {
	try {
		return JSON.parse(e) && !0;
	} catch {
		return !1;
	}
}, $ = (e) => {
	try {
		let t = JSON.parse(e);
		return Object.prototype.toString.call(t) === "[object Object]";
	} catch {
		return !1;
	}
}, ee = (e) => {
	try {
		let t = JSON.parse(e);
		return Array.isArray(t);
	} catch {
		return !1;
	}
}, te = (e) => {
	try {
		return g.load(e) && !0;
	} catch {
		return !1;
	}
}, ne = (e) => {
	try {
		let t = g.load(e);
		return Object.prototype.toString.call(t) === "[object Object]";
	} catch {
		return !1;
	}
}, re = () => ({
	statePlugins: { editor: {
		wrapActions: { setContent: w },
		actions: {
			detectContentType: C,
			detectContentTypeStarted: b,
			detectContentTypeSuccess: x,
			detectContentTypeFailure: S
		},
		selectors: {
			selectIsContentFormatYAML: J,
			selectIsContentFormatJSON: q,
			selectContentType: I,
			selectContentTypeDetectionStatus: P,
			selectIsContentTypeDetectionInProgress: F,
			selectIsContentTypeAsyncAPI: H,
			selectIsContentTypeAsyncAPI2: U,
			selectIsContentTypeOpenAPI: L,
			selectIsContentTypeOpenAPI20: R,
			selectIsContentTypeOpenAPI30x: z,
			selectIsContentTypeOpenAPI31x: B,
			selectIsContentTypeOpenAPI32x: V,
			selectIsContentTypeAPIDesignSystems: W,
			selectIsContentTypeJSONSchema: G,
			selectIsContentTypeJSONSchema202012: K,
			selectInferFileExtensionFromContent: Y,
			selectInferFileNameFromContent: X,
			selectInferFileNameWithExtensionFromContent: Z
		},
		reducers: N
	} },
	fn: {
		isValidJSON: Q,
		isValidJSONObject: $,
		isValidJSONArray: ee,
		isValidYAML: te,
		isValidYAMLObject: ne
	}
});
//#endregion
export { re as default };

//# sourceMappingURL=index.js.map