import { isUrl } from "../../../helpers/is-url.js";
import { json, yaml } from "@scalar/oas-utils/helpers";
//#region src/v2/features/command-palette/helpers/get-openapi-document-details.ts
/**
* Extracts the title from the info object of an API description.
* Returns undefined if the title is not a string or does not exist.
*/
var extractTitle = (info) => {
	return typeof info?.title === "string" ? info.title : void 0;
};
/**
* Attempts to extract OpenAPI document details from a parsed document.
* Checks for both OpenAPI and Swagger specifications.
*/
var parseDocumentDetails = (document, type) => {
	if (typeof document.openapi === "string") return {
		version: `OpenAPI ${document.openapi}`,
		type,
		title: extractTitle(document.info)
	};
	if (typeof document.swagger === "string") return {
		version: `Swagger ${document.swagger}`,
		type,
		title: extractTitle(document.info)
	};
};
/**
* Attempts to parse the input string with the given parser function.
* Returns undefined if parsing fails.
*/
var tryParse = (input, parser, type) => {
	try {
		return parseDocumentDetails(parser(input), type);
	} catch {
		return;
	}
};
/**
* Extracts OpenAPI or Swagger specification details from a string.
* Attempts to parse as JSON first, then YAML if JSON parsing fails.
*
* Returns undefined if:
* - Input is null, empty, or a URL
* - Input is not a valid JSON or YAML document
* - Document does not contain OpenAPI or Swagger version information
*/
var getOpenApiDocumentDetails = (input) => {
	if (!input || isUrl(input)) return;
	const jsonResult = tryParse(input, json.parse, "json");
	if (jsonResult) return jsonResult;
	return tryParse(input, yaml.parse, "yaml");
};
//#endregion
export { getOpenApiDocumentDetails };

//# sourceMappingURL=get-openapi-document-details.js.map