import { t as __exportAll } from "./chunk-D7D4PA-g.js";
import { t as Exception } from "./const-Dy5RfQqP.js";
import { n as loadProjectFromId, r as useActionHandler, t as loadProject } from "./request-handler-guINVc1G.js";
import { t as __decorate } from "./decorate-CQ_JIwqD.js";
import { Expose } from "class-transformer";
import { IsBoolean, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID, Matches, isBoolean, isString, registerDecorator } from "class-validator";
import { join } from "node:path";
//#region .svelte-kit/adapter-bun/entries/pages/actions/project/_page.server.ts.js
var _page_server_ts_exports = /* @__PURE__ */ __exportAll({ actions: () => actions });
var LoadProjectBody = class {
	id;
	path;
	gitUrl;
	gatewayId;
};
__decorate([
	Expose(),
	IsString(),
	IsOptional()
], LoadProjectBody.prototype, "id", void 0);
__decorate([
	Expose(),
	IsString(),
	IsOptional()
], LoadProjectBody.prototype, "path", void 0);
__decorate([
	Expose(),
	IsString(),
	IsOptional()
], LoadProjectBody.prototype, "gitUrl", void 0);
__decorate([
	Expose(),
	IsString(),
	IsOptional()
], LoadProjectBody.prototype, "gatewayId", void 0);
var resolveSessionFromGatewayId = async (gatewayId, { api, git, context }) => {
	if (!context.online) throw new Exception("Bad Request", "Cannot load project from gatewayId while offline", 400);
	const project = await api.projects.getProject(gatewayId);
	return {
		path: join(await git.clone(project.gatewayProjectRegistryUrl, { sshKey: project.gatewayProjectRegistryMetadata.sshKey }), project.gatewayProjectRegistryMetadata.dir ?? ""),
		gateway: {
			id: gatewayId,
			sshKey: project.gatewayProjectRegistryMetadata.sshKey
		}
	};
};
var resolveSessionFromGitUrl = async (gitUrl, { git, context }) => {
	if (context.online) throw new Exception("Bad Request", "Cannot load project from gitUrl while online", 400);
	return { path: await git.clone(gitUrl) };
};
var resolveSessionFromPath = async (path, { context }) => {
	if (context.online) throw new Exception("Bad Request", "Cannot load project from path while online", 400);
	return { path };
};
var resolveSessionFunctions = {
	path: resolveSessionFromPath,
	gitUrl: resolveSessionFromGitUrl,
	gatewayId: resolveSessionFromGatewayId
};
var loadProjectAction = useActionHandler(async (handler) => {
	const { body } = handler;
	if (body.id) return await loadProjectFromId(body.id, handler);
	const el = Object.entries(body).find(([, value]) => value);
	if (!el) throw new Exception("Bad Request", "No load origin provided", 400);
	const resolveSessionFunction = resolveSessionFunctions[el[0]];
	return await loadProject(await resolveSessionFunction(el[1], handler), handler);
}, {
	projectOptional: true,
	body: LoadProjectBody
});
var CompleteProjectBody = class {
	gatewayId;
	language;
	multiplayerServer;
};
__decorate([
	Expose(),
	IsUUID(8),
	IsNotEmpty()
], CompleteProjectBody.prototype, "gatewayId", void 0);
__decorate([
	Expose(),
	IsString(),
	IsOptional(),
	IsEnum(["js", "ts"])
], CompleteProjectBody.prototype, "language", void 0);
__decorate([
	Expose(),
	IsBoolean(),
	IsOptional()
], CompleteProjectBody.prototype, "multiplayerServer", void 0);
var completeProjectAction = useActionHandler(async (handler) => {
	const { body, cli, api } = handler;
	const project = await api.projects.getProject(body.gatewayId);
	const resolver = await resolveSessionFunctions.gatewayId(body.gatewayId, handler);
	const parts = resolver.path.split("/");
	const end = parts.pop();
	cli.new({
		editor: true,
		directory: parts.join("/"),
		name: project.name,
		path: end,
		packageManager: "bun",
		language: body.language,
		server: body.multiplayerServer,
		docker: false,
		lint: false,
		initFunctions: true,
		strict: false,
		git: false
	});
	return await loadProject(resolver, handler);
}, {
	body: CompleteProjectBody,
	onlineOnly: true,
	projectOptional: true
});
var getGatewayProjectsAction = useActionHandler(async ({ api }) => {
	return (await api.projects.getProjects()).map((project) => ({
		gatewayId: project.id,
		name: project.name
	}));
}, {
	onlineOnly: true,
	projectOptional: true
});
var InfoProjectBody = class {
	name;
};
__decorate([
	Expose(),
	IsString(),
	IsOptional()
], InfoProjectBody.prototype, "name", void 0);
var getInfoProjectAction = useActionHandler(async ({ fs }) => {
	const config = await fs.getFile("nanoforge.config.json").readJson();
	return {
		name: config.name,
		language: config.language,
		hasServer: config.server?.enabled ?? false
	};
});
var setInfoProjectAction = useActionHandler(async ({ body, fs }) => {
	const configFile = fs.getFile("nanoforge.config.json");
	const config = await configFile.readJson();
	if (body.name) config.name = body.name;
	configFile.writeJson(config);
}, { body: InfoProjectBody });
var IsFalseOrString = (validationOptions) => {
	return (object, propertyName) => {
		registerDecorator({
			target: object.constructor,
			propertyName,
			...validationOptions ? { options: validationOptions } : {},
			constraints: [],
			validator: {
				validate(value) {
					return isBoolean(value) && !value || isString(value);
				},
				defaultMessage() {
					return `$value must be a valid IP address or FQDN`;
				}
			}
		});
	};
};
var CreateProjectBody = class {
	projectName;
	projectPath;
	packageManager;
	language;
	multiplayerServer;
	dockerContainerization;
	createGitRepository;
	gitRemote;
};
__decorate([
	Expose(),
	IsString(),
	IsNotEmpty(),
	Matches(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)
], CreateProjectBody.prototype, "projectName", void 0);
__decorate([
	Expose(),
	IsString(),
	IsOptional()
], CreateProjectBody.prototype, "projectPath", void 0);
__decorate([
	Expose(),
	IsString(),
	IsOptional(),
	IsEnum([
		"npm",
		"pnpm",
		"yarn",
		"bun"
	])
], CreateProjectBody.prototype, "packageManager", void 0);
__decorate([
	Expose(),
	IsString(),
	IsOptional(),
	IsEnum(["js", "ts"])
], CreateProjectBody.prototype, "language", void 0);
__decorate([
	Expose(),
	IsBoolean(),
	IsOptional()
], CreateProjectBody.prototype, "multiplayerServer", void 0);
__decorate([
	Expose(),
	IsBoolean(),
	IsOptional()
], CreateProjectBody.prototype, "dockerContainerization", void 0);
__decorate([
	Expose(),
	IsBoolean(),
	IsOptional()
], CreateProjectBody.prototype, "createGitRepository", void 0);
__decorate([
	Expose(),
	IsFalseOrString(),
	IsOptional()
], CreateProjectBody.prototype, "gitRemote", void 0);
var actions = {
	load: loadProjectAction,
	new: useActionHandler(async (handler) => {
		const { body, cli } = handler;
		cli.new({
			editor: true,
			directory: body.projectPath || void 0,
			name: body.projectName,
			path: body.projectName,
			packageManager: body.packageManager,
			language: body.language,
			server: body.multiplayerServer,
			docker: body.dockerContainerization,
			git: body.createGitRepository,
			gitRemote: body.gitRemote,
			lint: false
		});
		return await loadProject({ path: join(body.projectPath ?? "", body.projectName) }, handler);
	}, {
		body: CreateProjectBody,
		offlineOnly: true,
		projectOptional: true
	}),
	complete: completeProjectAction,
	"get-info": getInfoProjectAction,
	"set-info": setInfoProjectAction,
	"get-gateway-projects": getGatewayProjectsAction
};
//#endregion
//#region .svelte-kit/adapter-bun/nodes/3.js
const index = 3;
const server_id = "src/routes/actions/project/+page.server.ts";
const imports = [];
const stylesheets = [];
const fonts = [];
//#endregion
export { fonts, imports, index, _page_server_ts_exports as server, server_id, stylesheets };

//# sourceMappingURL=3-CtfjP4uZ.js.map