
//#region src/index.ts
const plugin = { importer: {
	name: "Yaak",
	description: "Yaak official format",
	onImport(_ctx, args) {
		return migrateImport(args.text);
	}
} };
function migrateImport(contents) {
	let parsed;
	try {
		parsed = JSON.parse(contents);
	} catch {
		return;
	}
	if (!isJSObject(parsed)) return;
	if (!("yaakSchema" in parsed)) return;
	if ("requests" in parsed.resources) {
		parsed.resources.httpRequests = parsed.resources.requests;
		parsed.resources.requests = void 0;
	}
	for (const workspace of parsed.resources.workspaces ?? []) if ("variables" in workspace) {
		const baseEnvironment = {
			id: `GENERATE_ID::base_env_${workspace.id}`,
			name: "Global Variables",
			variables: workspace.variables,
			workspaceId: workspace.id
		};
		parsed.resources.environments = parsed.resources.environments ?? [];
		parsed.resources.environments.push(baseEnvironment);
		workspace.variables = void 0;
		for (const environment of parsed.resources.environments) if (environment.workspaceId === workspace.id && environment.id !== baseEnvironment.id) environment.environmentId = baseEnvironment.id;
	}
	for (const environment of parsed.resources.environments ?? []) if ("environmentId" in environment) {
		environment.base = environment.environmentId == null;
		environment.environmentId = void 0;
	}
	for (const environment of parsed.resources.environments ?? []) if ("base" in environment && environment.base && environment.parentModel == null) {
		environment.parentModel = "workspace";
		environment.parentId = null;
		environment.base = void 0;
	} else if ("base" in environment && !environment.base && environment.parentModel == null) {
		environment.parentModel = "environment";
		environment.parentId = null;
		environment.base = void 0;
	}
	return { resources: parsed.resources };
}
function isJSObject(obj) {
	return Object.prototype.toString.call(obj) === "[object Object]";
}

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