
const require_openApi = require('./openApi-BZ7m5ia5.cjs');
let _hey_api_codegen_core = require("@hey-api/codegen-core");
let ansi_colors = require("ansi-colors");
ansi_colors = require_openApi.__toESM(ansi_colors);
let color_support = require("color-support");
color_support = require_openApi.__toESM(color_support);
let node_fs = require("node:fs");
node_fs = require_openApi.__toESM(node_fs);
let node_path = require("node:path");
node_path = require_openApi.__toESM(node_path);
let _hey_api_json_schema_ref_parser = require("@hey-api/json-schema-ref-parser");

//#region src/config/engine.ts
const checkNodeVersion = () => {
	if (typeof Bun !== "undefined") {
		const [major] = Bun.version.split(".").map(Number);
		if (major < 1) throw new require_openApi.ConfigError(`Unsupported Bun version ${Bun.version}. Please use Bun 1.0.0 or newer.`);
	} else if (typeof process !== "undefined" && process.versions?.node) {
		const [major] = process.versions.node.split(".").map(Number);
		if (major < 20) throw new require_openApi.ConfigError(`Unsupported Node version ${process.versions.node}. Please use Node 20 or newer.`);
	}
};

//#endregion
//#region src/ir/intents.ts
var IntentContext = class {
	spec;
	constructor(spec) {
		this.spec = spec;
	}
	getOperation(path$2, method) {
		const paths = this.spec.paths;
		if (!paths) return;
		return paths[path$2]?.[method];
	}
	setExample(operation, example) {
		const source = this.getOperation(operation.path, operation.method);
		if (!source) return;
		source["x-codeSamples"] ||= [];
		source["x-codeSamples"].push(example);
	}
};

//#endregion
//#region src/generate/output.ts
const generateOutput = async ({ context }) => {
	const outputPath = node_path.default.resolve(context.config.output.path);
	if (context.config.output.clean) {
		if (node_fs.default.existsSync(outputPath)) node_fs.default.rmSync(outputPath, {
			force: true,
			recursive: true
		});
	}
	const client = require_openApi.getClientPlugin(context.config);
	if ("bundle" in client.config && client.config.bundle && !context.config.dryRun) context.config._FRAGILE_CLIENT_BUNDLE_RENAMED = require_openApi.generateClientBundle({
		meta: { importFileExtension: context.config.output.importFileExtension },
		outputPath,
		plugin: client,
		project: context.gen
	});
	for (const plugin of context.registerPlugins()) await plugin.run();
	context.gen.plan();
	const ctx = new IntentContext(context.spec);
	for (const intent of context.intents) await intent.run(ctx);
	for (const file of context.gen.render()) {
		const filePath = node_path.default.resolve(outputPath, file.path);
		const dir = node_path.default.dirname(filePath);
		if (!context.config.dryRun) {
			node_fs.default.mkdirSync(dir, { recursive: true });
			node_fs.default.writeFileSync(filePath, file.content, { encoding: "utf8" });
		}
	}
	const { source } = context.config.output;
	if (source.enabled) {
		const sourcePath = source.path === null ? void 0 : node_path.default.resolve(outputPath, source.path);
		if (!context.config.dryRun && sourcePath && sourcePath !== outputPath) node_fs.default.mkdirSync(sourcePath, { recursive: true });
		const serialized = await source.serialize(context.spec);
		if (!context.config.dryRun && sourcePath) node_fs.default.writeFileSync(node_path.default.resolve(sourcePath, `${source.fileName}.${source.extension}`), serialized, { encoding: "utf8" });
		if (source.callback) await source.callback(serialized);
	}
};

//#endregion
//#region src/openApi/shared/utils/patch.ts
const patchOpenApiSpec = ({ patchOptions, spec: _spec }) => {
	if (!patchOptions) return;
	const spec = _spec;
	if ("swagger" in spec) {
		if (patchOptions.version && spec.swagger) spec.swagger = typeof patchOptions.version === "string" ? patchOptions.version : patchOptions.version(spec.swagger);
		if (patchOptions.meta && spec.info) patchOptions.meta(spec.info);
		if (patchOptions.schemas && spec.definitions) for (const key in patchOptions.schemas) {
			const schema = spec.definitions[key];
			if (!schema || typeof schema !== "object") continue;
			const patchFn = patchOptions.schemas[key];
			patchFn(schema);
		}
		if (patchOptions.operations && spec.paths) for (const key in patchOptions.operations) {
			const [method, path$2] = key.split(" ");
			if (!method || !path$2) continue;
			const pathItem = spec.paths[path$2];
			if (!pathItem) continue;
			const operation = pathItem[method.toLocaleLowerCase()] || pathItem[method.toLocaleUpperCase()];
			if (!operation || typeof operation !== "object") continue;
			const patchFn = patchOptions.operations[key];
			patchFn(operation);
		}
		return;
	}
	if (patchOptions.version && spec.openapi) spec.openapi = typeof patchOptions.version === "string" ? patchOptions.version : patchOptions.version(spec.openapi);
	if (patchOptions.meta && spec.info) patchOptions.meta(spec.info);
	if (spec.components) {
		if (patchOptions.schemas && spec.components.schemas) for (const key in patchOptions.schemas) {
			const schema = spec.components.schemas[key];
			if (!schema || typeof schema !== "object") continue;
			const patchFn = patchOptions.schemas[key];
			patchFn(schema);
		}
		if (patchOptions.parameters && spec.components.parameters) for (const key in patchOptions.parameters) {
			const schema = spec.components.parameters[key];
			if (!schema || typeof schema !== "object") continue;
			const patchFn = patchOptions.parameters[key];
			patchFn(schema);
		}
		if (patchOptions.requestBodies && spec.components.requestBodies) for (const key in patchOptions.requestBodies) {
			const schema = spec.components.requestBodies[key];
			if (!schema || typeof schema !== "object") continue;
			const patchFn = patchOptions.requestBodies[key];
			patchFn(schema);
		}
		if (patchOptions.responses && spec.components.responses) for (const key in patchOptions.responses) {
			const schema = spec.components.responses[key];
			if (!schema || typeof schema !== "object") continue;
			const patchFn = patchOptions.responses[key];
			patchFn(schema);
		}
	}
	if (patchOptions.operations && spec.paths) for (const key in patchOptions.operations) {
		const [method, path$2] = key.split(" ");
		if (!method || !path$2) continue;
		const pathItem = spec.paths[path$2];
		if (!pathItem) continue;
		const operation = pathItem[method.toLocaleLowerCase()] || pathItem[method.toLocaleUpperCase()];
		if (!operation || typeof operation !== "object") continue;
		const patchFn = patchOptions.operations[key];
		patchFn(operation);
	}
};

//#endregion
//#region src/createClient.ts
const compileInputPath = (input) => {
	const result = {
		...input,
		path: ""
	};
	if (input.path && (typeof input.path !== "string" || input.registry !== "hey-api")) {
		result.path = input.path;
		return result;
	}
	const [basePath, baseQuery] = input.path.split("?");
	const queryPath = (baseQuery || "").split("&").map((part) => part.split("="));
	let path$2 = basePath || "";
	if (path$2.endsWith("/")) path$2 = path$2.slice(0, path$2.length - 1);
	const [, pathUrl] = path$2.split("://");
	const [baseUrl, organization, project] = (pathUrl || "").split("/");
	result.organization = organization || input.organization;
	result.project = project || input.project;
	const queryParams = [];
	const kApiKey = "api_key";
	result.api_key = queryPath.find(([key]) => key === kApiKey)?.[1] || input.api_key || process.env.HEY_API_TOKEN;
	if (result.api_key) queryParams.push(`${kApiKey}=${result.api_key}`);
	const kBranch = "branch";
	result.branch = queryPath.find(([key]) => key === kBranch)?.[1] || input.branch;
	if (result.branch) queryParams.push(`${kBranch}=${result.branch}`);
	const kCommitSha = "commit_sha";
	result.commit_sha = queryPath.find(([key]) => key === kCommitSha)?.[1] || input.commit_sha;
	if (result.commit_sha) queryParams.push(`${kCommitSha}=${result.commit_sha}`);
	const kTags = "tags";
	result.tags = queryPath.find(([key]) => key === kTags)?.[1]?.split(",") || input.tags;
	if (result.tags?.length) queryParams.push(`${kTags}=${result.tags.join(",")}`);
	const kVersion = "version";
	result.version = queryPath.find(([key]) => key === kVersion)?.[1] || input.version;
	if (result.version) queryParams.push(`${kVersion}=${result.version}`);
	if (!result.organization) throw new Error("missing organization - from which Hey API Platform organization do you want to generate your output?");
	if (!result.project) throw new Error("missing project - from which Hey API Platform project do you want to generate your output?");
	const query = queryParams.join("&");
	const platformUrl = baseUrl || "get.heyapi.dev";
	const isLocalhost = platformUrl.startsWith("localhost");
	const platformUrlWithProtocol = [isLocalhost ? "http" : "https", platformUrl].join("://");
	const compiledPath = isLocalhost ? [
		platformUrlWithProtocol,
		"v1",
		"get",
		result.organization,
		result.project
	].join("/") : [
		platformUrlWithProtocol,
		result.organization,
		result.project
	].join("/");
	result.path = query ? `${compiledPath}?${query}` : compiledPath;
	return result;
};
const logInputPaths = (inputPaths, jobIndex) => {
	const lines = [];
	const jobPrefix = ansi_colors.default.gray(`[Job ${jobIndex + 1}] `);
	const count = inputPaths.length;
	const baseString = ansi_colors.default.cyan(`Generating from ${count} ${count === 1 ? "input" : "inputs"}:`);
	lines.push(`${jobPrefix}⏳ ${baseString}`);
	inputPaths.forEach((inputPath, index) => {
		const itemPrefixStr = `  [${index + 1}] `;
		const itemPrefix = ansi_colors.default.cyan(itemPrefixStr);
		const detailIndent = " ".repeat(itemPrefixStr.length);
		if (typeof inputPath.path !== "string") {
			lines.push(`${jobPrefix}${itemPrefix}raw OpenAPI specification`);
			return;
		}
		switch (inputPath.registry) {
			case "hey-api": {
				const baseInput = [inputPath.organization, inputPath.project].filter(Boolean).join("/");
				lines.push(`${jobPrefix}${itemPrefix}${baseInput}`);
				if (inputPath.branch) lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("branch:")} ${ansi_colors.default.green(inputPath.branch)}`);
				if (inputPath.commit_sha) lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("commit:")} ${ansi_colors.default.green(inputPath.commit_sha)}`);
				if (inputPath.tags?.length) lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("tags:")} ${ansi_colors.default.green(inputPath.tags.join(", "))}`);
				if (inputPath.version) lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("version:")} ${ansi_colors.default.green(inputPath.version)}`);
				lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("registry:")} ${ansi_colors.default.green("Hey API")}`);
				break;
			}
			case "readme": {
				const baseInput = [inputPath.organization, inputPath.project].filter(Boolean).join("/");
				if (!baseInput) lines.push(`${jobPrefix}${itemPrefix}${inputPath.path}`);
				else lines.push(`${jobPrefix}${itemPrefix}${baseInput}`);
				if (inputPath.uuid) lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("uuid:")} ${ansi_colors.default.green(inputPath.uuid)}`);
				lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("registry:")} ${ansi_colors.default.green("ReadMe")}`);
				break;
			}
			case "scalar": {
				const baseInput = [inputPath.organization, inputPath.project].filter(Boolean).join("/");
				lines.push(`${jobPrefix}${itemPrefix}${baseInput}`);
				lines.push(`${jobPrefix}${detailIndent}${ansi_colors.default.gray("registry:")} ${ansi_colors.default.green("Scalar")}`);
				break;
			}
			default:
				lines.push(`${jobPrefix}${itemPrefix}${inputPath.path}`);
				break;
		}
	});
	for (const line of lines) console.log(line);
};
const createClient$1 = async ({ config, dependencies, jobIndex, logger, watches: _watches }) => {
	const watches = _watches || Array.from({ length: config.input.length }, () => ({ headers: new Headers() }));
	const inputPaths = config.input.map((input) => compileInputPath(input));
	if (config.logs.level !== "silent" && !_watches) logInputPaths(inputPaths, jobIndex);
	const getSpecData = async (input, index) => {
		const eventSpec = logger.timeEvent("spec");
		const { arrayBuffer, error, resolvedInput, response } = await require_openApi.getSpec({
			fetchOptions: input.fetch,
			inputPath: inputPaths[index].path,
			timeout: input.watch.timeout,
			watch: watches[index]
		});
		eventSpec.timeEnd();
		if (error && !_watches) throw new Error(`Request failed with status ${response.status}: ${response.statusText}`);
		return {
			arrayBuffer,
			resolvedInput
		};
	};
	const specData = (await Promise.all(config.input.map((input, index) => getSpecData(input, index)))).filter((data) => data.arrayBuffer || data.resolvedInput);
	let context;
	if (specData.length) {
		const refParser = new _hey_api_json_schema_ref_parser.$RefParser();
		const data = specData.length > 1 ? await refParser.bundleMany({
			arrayBuffer: specData.map((data$1) => data$1.arrayBuffer),
			pathOrUrlOrSchemas: [],
			resolvedInputs: specData.map((data$1) => data$1.resolvedInput)
		}) : await refParser.bundle({
			arrayBuffer: specData[0].arrayBuffer,
			pathOrUrlOrSchema: void 0,
			resolvedInput: specData[0].resolvedInput
		});
		if (config.logs.level !== "silent" && _watches) {
			console.clear();
			logInputPaths(inputPaths, jobIndex);
		}
		const eventInputPatch = logger.timeEvent("input.patch");
		patchOpenApiSpec({
			patchOptions: config.parser.patch,
			spec: data
		});
		eventInputPatch.timeEnd();
		const eventParser = logger.timeEvent("parser");
		context = require_openApi.parseOpenApiSpec({
			config,
			dependencies,
			logger,
			spec: data
		});
		context.graph = require_openApi.buildGraph(context.ir, logger).graph;
		eventParser.timeEnd();
		const eventGenerator = logger.timeEvent("generator");
		await generateOutput({ context });
		eventGenerator.timeEnd();
		const eventPostprocess = logger.timeEvent("postprocess");
		if (!config.dryRun) {
			const jobPrefix = ansi_colors.default.gray(`[Job ${jobIndex + 1}] `);
			require_openApi.postprocessOutput(config.output, jobPrefix);
			if (config.logs.level !== "silent") {
				const outputPath = process.env.INIT_CWD ? `./${node_path.default.relative(process.env.INIT_CWD, config.output.path)}` : config.output.path;
				console.log(`${jobPrefix}${ansi_colors.default.green("✅ Done!")} Your output is in ${ansi_colors.default.cyanBright(outputPath)}`);
			}
		}
		eventPostprocess.timeEnd();
	}
	const watchedInput = config.input.find((input, index) => input.watch.enabled && typeof inputPaths[index].path === "string");
	if (watchedInput) setTimeout(() => {
		createClient$1({
			config,
			dependencies,
			jobIndex,
			logger,
			watches
		});
	}, watchedInput.watch.interval);
	return context;
};

//#endregion
//#region src/utils/cli.ts
const textAscii = `
888   |                           e      888~-_   888
888___|  e88~~8e  Y88b  /        d8b     888   \\  888
888   | d888  88b  Y888/        /Y88b    888    | 888
888   | 8888__888   Y8/        /  Y88b   888   /  888
888   | Y888    ,    Y        /____Y88b  888_-~   888
888   |  "88___/    /        /      Y88b 888      888
                  _/
`;
const asciiToLines = (ascii, options) => {
	const lines = [];
	const padding = Array.from({ length: options?.padding ?? 0 }).fill("");
	lines.push(...padding);
	let maxLineLength = 0;
	let line = "";
	for (const char of ascii) if (char === "\n") {
		if (line) {
			lines.push(line);
			maxLineLength = Math.max(maxLineLength, line.length);
			line = "";
		}
	} else line += char;
	lines.push(...padding);
	return {
		lines,
		maxLineLength
	};
};
function printCliIntro() {
	const packageJson = require_openApi.loadPackageJson();
	const text = asciiToLines(textAscii, { padding: 1 });
	for (const line of text.lines) console.log(ansi_colors.default.cyan(line));
	console.log(ansi_colors.default.gray(`${packageJson.name} v${packageJson.version}`));
	console.log("");
}

//#endregion
//#region src/utils/logger.ts
let loggerCounter = 0;
const nameToId = (name) => `${name}-${loggerCounter++}`;
const idEnd = (id) => `${id}-end`;
const idLength = (id) => `${id}-length`;
const idStart = (id) => `${id}-start`;
const getSeverity = (duration, percentage) => {
	if (duration > 200) return {
		color: ansi_colors.default.red,
		type: "duration"
	};
	if (percentage > 30) return {
		color: ansi_colors.default.red,
		type: "percentage"
	};
	if (duration > 50) return {
		color: ansi_colors.default.yellow,
		type: "duration"
	};
	if (percentage > 10) return {
		color: ansi_colors.default.yellow,
		type: "percentage"
	};
};
var Logger = class {
	events = [];
	end(result) {
		let event;
		let events = this.events;
		for (const index of result.position) {
			event = events[index];
			if (event?.events) events = event.events;
		}
		if (event && !event.end) event.end = performance.mark(idEnd(event.id));
	}
	/**
	* Recursively end all unended events in the event tree.
	* This ensures all events have end marks before measuring.
	*/
	endAllEvents(events) {
		for (const event of events) {
			if (!event.end) event.end = performance.mark(idEnd(event.id));
			if (event.events.length > 0) this.endAllEvents(event.events);
		}
	}
	report(print = true) {
		const firstEvent = this.events[0];
		if (!firstEvent) return;
		this.endAllEvents(this.events);
		const lastEvent = this.events[this.events.length - 1];
		const name = "root";
		const id = nameToId(name);
		try {
			const measure = performance.measure(idLength(id), idStart(firstEvent.id), idEnd(lastEvent.id));
			if (print) this.reportEvent({
				end: lastEvent.end,
				events: this.events,
				id,
				indent: 0,
				measure,
				name,
				start: firstEvent.start
			});
			return measure;
		} catch {
			return;
		}
	}
	reportEvent({ indent, ...parent }) {
		const color = !indent ? ansi_colors.default.cyan : ansi_colors.default.gray;
		const lastIndex = parent.events.length - 1;
		parent.events.forEach((event, index) => {
			try {
				const measure = performance.measure(idLength(event.id), idStart(event.id), idEnd(event.id));
				const duration = Math.ceil(measure.duration * 100) / 100;
				const percentage = Math.ceil(measure.duration / parent.measure.duration * 100 * 100) / 100;
				const severity = indent ? getSeverity(duration, percentage) : void 0;
				let durationLabel = `${duration.toFixed(2).padStart(8)}ms`;
				if (severity?.type === "duration") durationLabel = severity.color(durationLabel);
				const branch = index === lastIndex ? "└─ " : "├─ ";
				const prefix = !indent ? "" : "│  ".repeat(indent - 1) + branch;
				const maxLength = 38 - prefix.length;
				const percentageBranch = !indent ? "" : "↳ ";
				let percentageLabel = `${indent ? " ".repeat(indent - 1) + percentageBranch : ""}${percentage.toFixed(2)}%`;
				if (severity?.type === "percentage") percentageLabel = severity.color(percentageLabel);
				const jobPrefix = ansi_colors.default.gray("[root] ");
				console.log(`${jobPrefix}${ansi_colors.default.gray(prefix)}${color(`${event.name.padEnd(maxLength)} ${durationLabel} (${percentageLabel})`)}`);
				this.reportEvent({
					...event,
					indent: indent + 1,
					measure
				});
			} catch {}
		});
	}
	start(id) {
		return performance.mark(idStart(id));
	}
	storeEvent({ result, ...event }) {
		const lastEventIndex = event.events.length - 1;
		const lastEvent = event.events[lastEventIndex];
		if (lastEvent && !lastEvent.end) {
			result.position = [...result.position, lastEventIndex];
			this.storeEvent({
				...event,
				events: lastEvent.events,
				result
			});
			return;
		}
		const length = event.events.push({
			...event,
			events: []
		});
		result.position = [...result.position, length - 1];
	}
	timeEvent(name) {
		const id = nameToId(name);
		const start = this.start(id);
		const event = {
			events: this.events,
			id,
			name,
			start
		};
		const result = { position: [] };
		this.storeEvent({
			...event,
			result
		});
		return {
			mark: start,
			timeEnd: () => this.end(result)
		};
	}
};

//#endregion
//#region src/generate.ts
/**
* Generate a client from the provided configuration.
*
* @param userConfig User provided {@link UserConfig} configuration(s).
*/
const createClient = async (userConfig, logger = new Logger()) => {
	const resolvedConfig = typeof userConfig === "function" ? await userConfig() : userConfig;
	const userConfigs = resolvedConfig ? resolvedConfig instanceof Array ? resolvedConfig : [resolvedConfig] : [];
	let rawLogs = userConfigs.find((config) => require_openApi.getLogs(config).level !== "silent")?.logs;
	if (typeof rawLogs === "string") rawLogs = require_openApi.getLogs({ logs: rawLogs });
	let configs;
	try {
		checkNodeVersion();
		const eventCreateClient = logger.timeEvent("createClient");
		const eventConfig = logger.timeEvent("config");
		configs = await require_openApi.initConfigs({
			logger,
			userConfigs
		});
		if (configs.results.some((result$1) => result$1.config.logs.level !== "silent")) printCliIntro();
		eventConfig.timeEnd();
		const allConfigErrors = configs.results.flatMap((result$1) => result$1.errors.map((error) => ({
			error,
			jobIndex: result$1.jobIndex
		})));
		if (allConfigErrors.length) throw new require_openApi.ConfigValidationError(allConfigErrors);
		const result = (await Promise.all(configs.results.map(async (result$1) => {
			try {
				return await createClient$1({
					config: result$1.config,
					dependencies: configs.dependencies,
					jobIndex: result$1.jobIndex,
					logger
				});
			} catch (error) {
				throw new require_openApi.JobError("", {
					error,
					jobIndex: result$1.jobIndex
				});
			}
		}))).filter((client) => Boolean(client));
		eventCreateClient.timeEnd();
		const printLogs = configs.results.some((result$1) => result$1.config.logs.level === "debug");
		logger.report(printLogs);
		return result;
	} catch (error) {
		const results = configs?.results ?? [];
		const logs = results.find((result) => result.config.logs.level !== "silent")?.config.logs ?? results[0]?.config.logs ?? rawLogs;
		const dryRun = results.some((result) => result.config.dryRun) ?? userConfigs.some((config) => config.dryRun) ?? false;
		const logPath = logs?.file && !dryRun ? require_openApi.logCrashReport(error, logs.path ?? "") : void 0;
		if (!logs || logs.level !== "silent") {
			require_openApi.printCrashReport({
				error,
				logPath
			});
			if (await require_openApi.shouldReportCrash({
				error,
				isInteractive: results.some((result) => result.config.interactive) ?? userConfigs.some((config) => config.interactive) ?? false
			})) await require_openApi.openGitHubIssueWithCrashReport(error);
		}
		throw error;
	}
};

//#endregion
//#region src/utils/exports.ts
/**
* Utilities shared across the package.
*/
const utils = {
	stringCase({ case: casing, stripLeadingSeparators, value }) {
		return require_openApi.toCase(value, casing, { stripLeadingSeparators });
	},
	toCase: require_openApi.toCase
};

//#endregion
//#region src/index.ts
ansi_colors.default.enabled = (0, color_support.default)().hasBasic;
/**
* Type helper for openapi-ts.config.ts, returns {@link MaybeArray<UserConfig>} object(s)
*/
const defineConfig = async (config) => typeof config === "function" ? await config() : config;

//#endregion
Object.defineProperty(exports, 'Logger', {
  enumerable: true,
  get: function () {
    return Logger;
  }
});
Object.defineProperty(exports, 'createClient', {
  enumerable: true,
  get: function () {
    return createClient;
  }
});
Object.defineProperty(exports, 'defineConfig', {
  enumerable: true,
  get: function () {
    return defineConfig;
  }
});
Object.defineProperty(exports, 'utils', {
  enumerable: true,
  get: function () {
    return utils;
  }
});
//# sourceMappingURL=src-BkOFTEi8.cjs.map