import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
//#region src/v2/blocks/operation-block/helpers/de-serialize-parameter.ts
/** Helper that de-serializes the example value based on the parameter type */
var deSerializeParameter = (example, param) => {
	if ("content" in param) return deSerializeContentExample(example, Object.keys(param.content ?? {})[0] ?? "");
	if ("schema" in param) return deSerializeSchemaExample(example, param.schema);
	return example;
};
/** De-serialize the example value based on the content type */
var deSerializeContentExample = (example, contentType) => {
	if (typeof example === "string" && contentType.includes("json")) try {
		return JSON.parse(example);
	} catch {}
	return example;
};
/** Create a set of all the types we wish to parse as JSON */
var parseableTypesSet = new Set([
	"array",
	"object",
	"boolean",
	"number",
	"integer",
	"null"
]);
/** De-serialize the example value based on the schema type */
var deSerializeSchemaExample = (example, schema) => {
	const resolvedSchema = getResolvedRef(schema);
	if (typeof example === "string" && resolvedSchema && "type" in resolvedSchema) {
		const type = Array.isArray(resolvedSchema.type) ? resolvedSchema.type[0] : resolvedSchema.type;
		if (type && parseableTypesSet.has(type)) try {
			return JSON.parse(example);
		} catch {
			if (type === "array") return example.split(/,\s?/).filter((v) => v !== "");
		}
	}
	return example;
};
//#endregion
export { deSerializeParameter };

//# sourceMappingURL=de-serialize-parameter.js.map