
//#region src/index.ts
const plugin = { importer: {
	name: "Postman Environment",
	description: "Import postman environment exports",
	onImport(_ctx, args) {
		return convertPostmanEnvironment(args.text);
	}
} };
function convertPostmanEnvironment(contents) {
	const root = parseJSONToRecord(contents);
	if (root == null) return;
	const values = toArray(root.values);
	const hasEnvMarkers = typeof root._postman_variable_scope === "string";
	if (values.length === 0 || !hasEnvMarkers && typeof root.name !== "string") return;
	const exportResources = {
		workspaces: [],
		environments: []
	};
	const envVariables = values.map((v) => ({
		enabled: v.enabled ?? true,
		name: String(v.key ?? ""),
		value: String(v.value),
		description: v.description ? String(v.description) : null
	})).filter((v) => v.name.length > 0);
	const environment = {
		model: "environment",
		id: generateId("environment"),
		name: root.name ? String(root.name) : "Environment",
		workspaceId: "CURRENT_WORKSPACE",
		parentModel: "environment",
		parentId: null,
		variables: envVariables
	};
	exportResources.environments.push(environment);
	return { resources: deleteUndefinedAttrs(convertTemplateSyntax(exportResources)) };
}
function parseJSONToRecord(jsonStr) {
	try {
		return toRecord(JSON.parse(jsonStr));
	} catch {
		return null;
	}
}
function toRecord(value) {
	if (value && typeof value === "object" && !Array.isArray(value)) return value;
	return {};
}
function toArray(value) {
	if (Object.prototype.toString.call(value) === "[object Array]") return value;
	return [];
}
/** Recursively render all nested object properties */
function convertTemplateSyntax(obj) {
	if (typeof obj === "string") return obj.replace(/{{\s*(_\.)?([^}]*)\s*}}/g, (_m, _dot, expr) => `\${[${expr.trim()}]}`);
	if (Array.isArray(obj) && obj != null) return obj.map(convertTemplateSyntax);
	if (typeof obj === "object" && obj != null) return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, convertTemplateSyntax(v)]));
	return obj;
}
function deleteUndefinedAttrs(obj) {
	if (Array.isArray(obj) && obj != null) return obj.map(deleteUndefinedAttrs);
	if (typeof obj === "object" && obj != null) return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== void 0).map(([k, v]) => [k, deleteUndefinedAttrs(v)]));
	return obj;
}
const idCount = {};
function generateId(model) {
	idCount[model] = (idCount[model] ?? -1) + 1;
	return `GENERATE_ID::${model.toUpperCase()}_${idCount[model]}`;
}

//#endregion
exports.convertPostmanEnvironment = convertPostmanEnvironment;
exports.plugin = plugin;