import { replaceTemplateVariables } from "../../../../libs/string-template.js";
import { buildRequestSecurity } from "../../../../libs/send-request/build-request-security.js";
import { convertToHarRequest } from "./convert-to-har-request.js";
import { REGEX } from "@scalar/oas-utils/helpers";
//#region src/views/Components/CodeSnippet/helpers/get-har-request.ts
var EMPTY_TOKEN_PLACEHOLDER = "YOUR_SECRET_TOKEN";
/**
* Creates a snippetz-compatible HarRequest from OpenAPI-like store entities
*
* @deprecated, can be removed once we've migrated to the new code snippet component
*/
var getHarRequest = ({ operation, example, server, securitySchemes = [], environment }) => {
	const env = environment && Array.isArray(environment) ? Object.fromEntries(environment.map((v) => [v.key, v.value])) : environment || {};
	const encodedPathVars = (example?.parameters?.path ?? []).reduce((vars, param) => {
		if (param.enabled) {
			const substitutedValue = replaceTemplateVariables(param.value, env);
			vars[param.key] = encodeURIComponent(substitutedValue);
		}
		return vars;
	}, {});
	const serverString = (() => {
		if (server?.url && (REGEX.VARIABLES.test(server.url) || REGEX.PATH.test(server.url))) {
			const serverParams = Object.entries(server?.variables || {}).reduce((acc, [key, variable]) => {
				if (encodedPathVars[key]) acc[key] = encodedPathVars[key];
				else if (variable.default) acc[key] = replaceTemplateVariables(variable.default, env);
				return acc;
			}, {});
			return replaceTemplateVariables(replaceTemplateVariables(server.url, env), serverParams);
		}
		return server?.url;
	})();
	const pathString = (() => {
		const path = operation?.path ?? "/";
		if (path && (REGEX.VARIABLES.test(path) || REGEX.PATH.test(path))) return replaceTemplateVariables(replaceTemplateVariables(path, env), encodedPathVars);
		return path;
	})();
	const security = buildRequestSecurity(securitySchemes, env, EMPTY_TOKEN_PLACEHOLDER);
	const headers = [...(example?.parameters.headers ?? []).map((h) => ({
		...h,
		value: REGEX.VARIABLES.test(h.value) || REGEX.PATH.test(h.value) ? replaceTemplateVariables(h.value, env) : h.value
	})) ?? [], ...Object.entries(security.headers).map(([key, value]) => ({
		key,
		value,
		enabled: true
	}))];
	const cookies = [...(example?.parameters.cookies ?? []).map((c) => ({
		...c,
		value: REGEX.VARIABLES.test(c.value) || REGEX.PATH.test(c.value) ? replaceTemplateVariables(c.value, env) : c.value
	})) ?? [], ...security.cookies.map((cookie) => ({
		key: cookie.name,
		value: cookie.value,
		enabled: true
	}))];
	const query = [...(example?.parameters.query ?? []).map((q) => ({
		...q,
		value: REGEX.VARIABLES.test(q.value) || REGEX.PATH.test(q.value) ? replaceTemplateVariables(q.value, env) : q.value
	})) ?? [], ...Array.from(security.urlParams.entries()).map(([key, value]) => ({
		key,
		value,
		enabled: true
	}))];
	const body = (() => {
		const body = example?.body;
		if (body?.raw?.value && (REGEX.VARIABLES.test(body.raw.value) || REGEX.PATH.test(body.raw.value))) return {
			...body,
			raw: {
				...body.raw,
				value: replaceTemplateVariables(body.raw.value, env)
			}
		};
		return body;
	})();
	return convertToHarRequest({
		baseUrl: serverString,
		method: operation?.method ?? "get",
		path: pathString,
		body,
		cookies,
		headers,
		query
	});
};
//#endregion
export { getHarRequest };

//# sourceMappingURL=get-har-request.js.map