import { getResolvedRefDeep } from "../../operation-code-sample/helpers/get-resolved-ref-deep.js";
//#region src/v2/blocks/operation-block/helpers/get-example.ts
/** Helper to get example from examples object with fallback to example field */
var getExampleFromExamples = (examples, exampleField, exampleKey) => {
	if (!examples && !exampleField) return;
	const hasExamples = !!examples && Object.keys(examples).length > 0;
	const key = exampleKey || Object.keys(examples ?? {})[0] || "";
	const example = getResolvedRefDeep(examples?.[key]);
	if (example !== void 0) return example;
	if ((!hasExamples || !exampleKey) && exampleField !== void 0) return { value: getResolvedRefDeep(exampleField) };
};
/**
* Resolve an example value for a parameter or requestBody from either `examples` or `content.*.examples`.
* Or the [deprecated] `example` field.
* If no exampleKey is provided it will fallback to the first example in the examples object then the [deprecated]
* `example` field.
* Used both for send-request and generating code snippets.
*/
var getExample = (param, exampleKey, contentType) => {
	if ("content" in param) {
		const content = param.content?.[contentType ?? Object.keys(param.content)[0] ?? ""];
		const result = getExampleFromExamples(content?.examples, content?.example, exampleKey);
		if (result !== void 0) return result;
	}
	if ("examples" in param || "example" in param) {
		const result = getExampleFromExamples(param.examples, param.example, exampleKey);
		if (result !== void 0) return result;
	}
	const resolvedParam = getResolvedRefDeep(param);
	if ("schema" in resolvedParam && resolvedParam.schema) {
		const schema = resolvedParam.schema;
		if ("default" in schema && schema.default !== void 0) return { value: schema.default };
		if ("enum" in schema && schema.enum?.[0] !== void 0) return { value: schema.enum[0] };
		if ("examples" in schema && schema.examples?.[0] !== void 0) return { value: schema.examples[0] };
		if ("example" in schema && schema.example !== void 0) return { value: schema.example };
	}
};
//#endregion
export { getExample };

//# sourceMappingURL=get-example.js.map