import { bt as unsubscribe_stores, ft as store_get } from "./events-Bhh2w-5S.js";
import { a as resolve, r as goto } from "./client-bZcBshMs.js";
import { t as page } from "./state-DwdUA00F.js";
import { D as PLException, E as PLErrors, N as ProjectLoader, b as Loading_button, ht as toastError, i as Card, it as getConfig, pt as runSafe, tt as createQuery } from "./error-DYeUQ7mQ.js";
import { a as Form$1, c as FormFieldSwitch, l as useForm, n as boolean, r as object, s as FormFieldSelect, t as _enum } from "./schemas-BDVbWglI.js";
import { t as Full_page_project_spinner } from "./project-loader-DaQVgPID.js";
//#region .svelte-kit/adapter-bun/entries/pages/load/_page.svelte.js
function Complete_project_form($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		const { gatewayId } = $$props;
		let isLoading = false;
		const schema = object({
			language: _enum(["ts", "js"]).default("ts"),
			multiplayerServer: boolean().default(false)
		});
		const completeProject = async (values) => {
			isLoading = true;
			await runSafe("create project", async () => {
				await goto(`/dashboard?id=${(await ProjectLoader.complete({
					...values,
					gatewayId
				})).id}`);
			}, () => {
				isLoading = false;
			});
		};
		const formCtx = useForm({
			schema,
			defaultValues: {
				language: "ts",
				multiplayerServer: false
			},
			onSubmit: completeProject
		});
		const { submitting } = formCtx;
		if (Card) {
			$$renderer.push("<!--[-->");
			Card($$renderer, {
				class: "w-full max-w-lg p-4",
				children: ($$renderer) => {
					Form$1($$renderer, {
						form: formCtx,
						class: "w-full",
						children: ($$renderer) => {
							$$renderer.push(`<div class="modal-header"><div class="modal-icon-badge"><span class="i-ic-baseline-check text-primary text-lg"></span></div> <div><h2 class="text-sm font-semibold text-foreground tracking-tight leading-tight">Init project</h2> <p class="text-xs text-muted-foreground mt-0.5">Configure and scaffold your project</p></div></div> <div class="modal-body">`);
							FormFieldSelect($$renderer, {
								name: "language",
								label: "Language",
								classNames: {
									trigger: "w-40 p-4",
									content: "p-2",
									item: "p-2"
								},
								options: [{
									value: "ts",
									label: "Typescript"
								}, {
									value: "js",
									label: "Javascript"
								}]
							});
							$$renderer.push(`<!----> <div class="flex flex-col gap-1">`);
							FormFieldSwitch($$renderer, {
								label: "Multiplayer game",
								name: "multiplayerServer"
							});
							$$renderer.push(`<!----></div></div> <div class="modal-footer">`);
							Loading_button($$renderer, {
								type: "submit",
								size: "lg",
								loading: store_get($$store_subs ??= {}, "$submitting", submitting) || isLoading,
								class: "btn-primary px-6",
								children: ($$renderer) => {
									$$renderer.push(`<!---->Init Project`);
								},
								$$slots: { default: true }
							});
							$$renderer.push(`<!----></div>`);
						},
						$$slots: { default: true }
					});
				},
				$$slots: { default: true }
			});
			$$renderer.push("<!--]-->");
		} else {
			$$renderer.push("<!--[!-->");
			$$renderer.push("<!--]-->");
		}
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		const getResolvable = () => {
			if (getConfig().mode === "offline") {
				const path = page.url.searchParams.get("path");
				if (!path) return null;
				return { path };
			} else {
				const gatewayId = page.url.searchParams.get("gatewayId");
				if (!gatewayId) return null;
				return { gatewayId };
			}
		};
		const id = page.url.searchParams.get("id");
		const resolvable = getResolvable();
		const query = createQuery(() => ({
			queryKey: [
				"load",
				"verify",
				resolvable ? "path" in resolvable ? resolvable.path : resolvable.gatewayId : ""
			],
			queryFn: async () => {
				if (!id && !resolvable) throw new Error("No resolvable");
				if (id) try {
					await goto(resolve(`/dashboard?id=${(await ProjectLoader.loadFromId(id)).id}`));
				} catch (e) {
					if (e instanceof PLException) {
						if (e.reason === PLErrors.Empty_Project && getConfig().mode === "online") return e.args[1];
					}
					console.error(e);
					toastError("Failed to load project from id", e.message);
				}
				if (resolvable) try {
					await goto(resolve(`/dashboard?id=${(await ("path" in resolvable ? ProjectLoader.loadFromPath(resolvable.path) : ProjectLoader.loadFromGatewayId(resolvable.gatewayId))).id}`));
					return null;
				} catch (e) {
					if (e instanceof PLException) {
						if (e.reason === PLErrors.Empty_Project && getConfig().mode === "online") return e.args[1];
					}
					console.error(e);
					toastError("Failed to load project", e.message);
				}
				await goto(resolve("/"));
				return null;
			}
		}));
		if (query.isFetched && !query.isLoading && query.data) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="flex h-100vh w-full items-center justify-center">`);
			Complete_project_form($$renderer, { gatewayId: query.data });
			$$renderer.push(`<!----></div>`);
		} else {
			$$renderer.push("<!--[-1-->");
			Full_page_project_spinner($$renderer, {});
		}
		$$renderer.push(`<!--]-->`);
	});
}
//#endregion
export { _page as default };

//# sourceMappingURL=_page.svelte-DR4jsdM6.js.map