import { _ as derived, $ as store_get, a1 as attr, a3 as escape_html, a2 as attr_class, a9 as stringify, a8 as unsubscribe_stores, a4 as ensure_array_like, Y as writable, a5 as spread_props, a6 as sanitize_props, a7 as slot } from './internal-B3VUlLoc.js';
import { c as activeProjectId, p as projects, I as Icon, A as subscribe, B as loadProjectSummary } from './Header-DF-atx4l.js';
import { a as appSettings } from './formatting-Cq2KfAoL.js';
import { W as WorkspaceEmptyState } from './WorkspaceEmptyState-CpWtvgum.js';

//#region ../../node_modules/.pnpm/lucide-svelte@1.0.1_svelte@5.55.5/node_modules/lucide-svelte/dist/icons/wand-sparkles.svelte
function Wand_sparkles($$renderer, $$props) {
	Icon($$renderer, spread_props([
		{ name: "wand-sparkles" },
		sanitize_props($$props),
		{
			/**
			* @component @name WandSparkles
			* @description Lucide SVG icon component, renders SVG Element with children.
			*
			* @preview ![img](data:image/svg+xml;base64,PHN2ZyAgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIgogIHdpZHRoPSIyNCIKICBoZWlnaHQ9IjI0IgogIHZpZXdCb3g9IjAgMCAyNCAyNCIKICBmaWxsPSJub25lIgogIHN0cm9rZT0iIzAwMCIgc3R5bGU9ImJhY2tncm91bmQtY29sb3I6ICNmZmY7IGJvcmRlci1yYWRpdXM6IDJweCIKICBzdHJva2Utd2lkdGg9IjIiCiAgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIgogIHN0cm9rZS1saW5lam9pbj0icm91bmQiCj4KICA8cGF0aCBkPSJtMjEuNjQgMy42NC0xLjI4LTEuMjhhMS4yMSAxLjIxIDAgMCAwLTEuNzIgMEwyLjM2IDE4LjY0YTEuMjEgMS4yMSAwIDAgMCAwIDEuNzJsMS4yOCAxLjI4YTEuMiAxLjIgMCAwIDAgMS43MiAwTDIxLjY0IDUuMzZhMS4yIDEuMiAwIDAgMCAwLTEuNzIiIC8+CiAgPHBhdGggZD0ibTE0IDcgMyAzIiAvPgogIDxwYXRoIGQ9Ik01IDZ2NCIgLz4KICA8cGF0aCBkPSJNMTkgMTR2NCIgLz4KICA8cGF0aCBkPSJNMTAgMnYyIiAvPgogIDxwYXRoIGQ9Ik03IDhIMyIgLz4KICA8cGF0aCBkPSJNMjEgMTZoLTQiIC8+CiAgPHBhdGggZD0iTTExIDNIOSIgLz4KPC9zdmc+Cg==) - https://lucide.dev/icons/wand-sparkles
			* @see https://lucide.dev/guide/packages/lucide-svelte - Documentation
			*
			* @param {Object} props - Lucide icons props and any valid SVG attribute
			* @returns {FunctionalComponent} Svelte component
			*
			*/
			iconNode: [
				["path", { "d": "m21.64 3.64-1.28-1.28a1.21 1.21 0 0 0-1.72 0L2.36 18.64a1.21 1.21 0 0 0 0 1.72l1.28 1.28a1.2 1.2 0 0 0 1.72 0L21.64 5.36a1.2 1.2 0 0 0 0-1.72" }],
				["path", { "d": "m14 7 3 3" }],
				["path", { "d": "M5 6v4" }],
				["path", { "d": "M19 14v4" }],
				["path", { "d": "M10 2v2" }],
				["path", { "d": "M7 8H3" }],
				["path", { "d": "M21 16h-4" }],
				["path", { "d": "M11 3H9" }]
			],
			children: ($$renderer) => {
				$$renderer.push(`<!--[-->`);
				slot($$renderer, $$props, "default", {});
				$$renderer.push(`<!--]-->`);
			},
			$$slots: { default: true }
		}
	]));
}
//#endregion
//#region src/lib/stores/git-state.ts
var gitStatus = writable(null);
var gitDiff = writable(null);
var gitLoading = writable(false);
var gitError = writable(null);
subscribe((msg) => {
	if (msg.type === "git.status") {
		gitStatus.set(msg.result);
		gitLoading.set(false);
		loadProjectSummary(msg.projectId);
	} else if (msg.type === "error" && msg.method.startsWith("git.")) {
		gitError.set(msg.message);
		gitLoading.set(false);
	}
});
//#endregion
//#region src/lib/components/git/git-utils.ts
function uniqueBy(items, keyFn) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const item of items) {
		const key = keyFn(item);
		if (seen.has(key)) continue;
		seen.add(key);
		result.push(item);
	}
	return result;
}
//#endregion
//#region src/lib/components/git/FileChanges.svelte
function FileChanges($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { staged, unstaged } = $$props;
		let uniqueStaged = derived(() => uniqueBy(staged, (file) => file.path));
		let uniqueUnstaged = derived(() => uniqueBy(unstaged, (file) => file.path));
		const statusIcon = {
			added: "A",
			modified: "M",
			deleted: "D",
			renamed: "R",
			untracked: "U"
		};
		const statusColor = {
			added: "text-added",
			modified: "text-modified",
			deleted: "text-deleted",
			renamed: "text-accent",
			untracked: "text-text-secondary"
		};
		function fileName(path) {
			return path.split("/").pop() ?? path;
		}
		function dirName(path) {
			const parts = path.split("/");
			return parts.length > 1 ? parts.slice(0, -1).join("/") + "/" : "";
		}
		function fileRow($$renderer, file, action) {
			$$renderer.push(`<div class="flex items-center w-full gap-2 px-3 py-2.5 hover:bg-bg-hover active:bg-bg-tertiary transition-colors cursor-pointer" role="button" tabindex="0"><span${attr_class(`font-mono text-xs font-bold w-5 text-center shrink-0 ${stringify(statusColor[file.status])}`)}>${escape_html(statusIcon[file.status])}</span> <span class="flex-1 min-w-0 text-sm truncate"><span class="text-text-muted">${escape_html(dirName(file.path))}</span><span class="text-text">${escape_html(fileName(file.path))}</span></span> <button class="shrink-0 w-7 h-7 flex items-center justify-center rounded text-text-muted hover:text-text hover:bg-bg-secondary transition-colors"${attr("aria-label", action === "stage" ? "Stage file" : "Unstage file")}>`);
			if (action === "stage") {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<svg class="w-4 h-4" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M8 3v10M3 8h10"></path></svg>`);
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<svg class="w-4 h-4" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M3 8h10"></path></svg>`);
			}
			$$renderer.push(`<!--]--></button></div>`);
		}
		$$renderer.push(`<div class="flex flex-col">`);
		if (uniqueStaged().length > 0) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="flex items-center justify-between px-3 py-2 border-b border-border"><span class="text-xs font-semibold text-success uppercase tracking-wider">Staged (${escape_html(uniqueStaged().length)})</span> <button class="text-xs text-text-muted hover:text-text transition-colors">Unstage all</button></div> <!--[-->`);
			const each_array = ensure_array_like(uniqueStaged());
			for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
				let file = each_array[$$index];
				fileRow($$renderer, file, "unstage");
			}
			$$renderer.push(`<!--]-->`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> `);
		if (uniqueUnstaged().length > 0) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div${attr_class(`flex items-center justify-between px-3 py-2 border-b border-border ${stringify(uniqueStaged().length > 0 ? "border-t" : "")}`)}><span class="text-xs font-semibold text-modified uppercase tracking-wider">Changes (${escape_html(uniqueUnstaged().length)})</span> <button class="text-xs text-text-muted hover:text-text transition-colors">Stage all</button></div> <!--[-->`);
			const each_array_1 = ensure_array_like(uniqueUnstaged());
			for (let $$index_1 = 0, $$length = each_array_1.length; $$index_1 < $$length; $$index_1++) {
				let file = each_array_1[$$index_1];
				fileRow($$renderer, file, "stage");
			}
			$$renderer.push(`<!--]-->`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> `);
		if (uniqueStaged().length === 0 && uniqueUnstaged().length === 0) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="flex items-center justify-center py-12 text-text-muted text-sm">No changes</div>`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--></div>`);
	});
}
//#endregion
//#region src/lib/components/git/DiffViewer.svelte
function DiffViewer($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		let diff = derived(() => store_get($$store_subs ??= {}, "$gitDiff", gitDiff));
		function parseDiffLines(raw) {
			if (!raw) return [];
			return raw.split("\n").map((line) => {
				if (line.startsWith("+++") || line.startsWith("---") || line.startsWith("diff ") || line.startsWith("index ")) return {
					type: "hdr",
					text: line
				};
				else if (line.startsWith("@@")) return {
					type: "hdr",
					text: line
				};
				else if (line.startsWith("+")) return {
					type: "add",
					text: line
				};
				else if (line.startsWith("-")) return {
					type: "del",
					text: line
				};
				return {
					type: "ctx",
					text: line
				};
			});
		}
		let lines = derived(() => diff() ? parseDiffLines(diff().content) : []);
		if (diff()) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="flex flex-col h-full bg-bg"><div class="flex items-center justify-between px-3 py-2 border-b border-border shrink-0"><span class="text-sm font-mono truncate flex-1">${escape_html(diff().filePath)}</span> <button class="ml-2 p-1.5 rounded hover:bg-bg-hover text-text-muted hover:text-text transition-colors" aria-label="Close diff"><svg class="w-4 h-4" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M4 4l8 8M12 4l-8 8"></path></svg></button></div> <div class="flex-1 overflow-auto">`);
			if (lines().length === 0) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="p-4 text-text-muted text-sm">No diff available</div>`);
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<pre class="text-xs leading-5 font-mono"><!--[-->`);
				const each_array = ensure_array_like(lines());
				for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
					let line = each_array[$$index];
					$$renderer.push(`<div${attr_class(`px-3 ${stringify(line.type === "add" ? "bg-added/10 text-added" : line.type === "del" ? "bg-deleted/10 text-deleted" : line.type === "hdr" ? "text-accent bg-accent/5" : "text-text-secondary")}`)}>${escape_html(line.text)}</div>`);
				}
				$$renderer.push(`<!--]--></pre>`);
			}
			$$renderer.push(`<!--]--></div></div>`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]-->`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}
//#endregion
//#region src/lib/components/git/CommitForm.svelte
function CommitForm($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		let { hasStagedFiles } = $$props;
		let message = "";
		let generating = false;
		const modelLabel = derived(() => store_get($$store_subs ??= {}, "$appSettings", appSettings).commitMessageModel ? `${store_get($$store_subs ??= {}, "$appSettings", appSettings).commitMessageProvider} / ${store_get($$store_subs ??= {}, "$appSettings", appSettings).commitMessageModel}` : store_get($$store_subs ??= {}, "$appSettings", appSettings).commitMessageProvider);
		$$renderer.push(`<div class="flex flex-col gap-2 p-3 border-t border-border bg-bg-secondary"><textarea class="w-full bg-bg border border-border rounded-md px-3 py-2 text-sm text-text placeholder:text-text-muted focus:outline-none focus:border-border-focus resize-none font-mono" rows="2" placeholder="Commit message...">`);
		const $$body = escape_html(message);
		if ($$body) $$renderer.push(`${$$body}`);
		$$renderer.push(`</textarea> `);
		$$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> <div class="flex items-center gap-2"><span class="flex-1 truncate text-xs text-text-muted"${attr("title", modelLabel())}>${escape_html(modelLabel())}</span> <button type="button"${attr_class(`inline-flex h-7 w-7 items-center justify-center rounded-md border border-border text-text-muted transition-colors hover:border-border-focus hover:text-text disabled:cursor-not-allowed disabled:opacity-50 ${stringify("")}`)}${attr("disabled", !hasStagedFiles || generating, true)} aria-label="Generate commit message" title="Generate commit message">`);
		$$renderer.push("<!--[-1-->");
		Wand_sparkles($$renderer, {
			size: 14,
			"aria-hidden": "true"
		});
		$$renderer.push(`<!--]--></button> `);
		$$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> <button${attr_class(`px-4 py-1.5 rounded-md text-sm font-medium transition-colors ${stringify(("bg-bg-tertiary text-text-muted cursor-not-allowed"))}`)}${attr("disabled", !hasStagedFiles || !message.trim(), true)}>Commit${escape_html(hasStagedFiles ? "" : " (no staged files)")}</button></div></div>`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}
//#endregion
//#region src/lib/components/git/BranchSwitcher.svelte
function BranchSwitcher($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		let currentBranch = derived(() => store_get($$store_subs ??= {}, "$gitStatus", gitStatus)?.branch ?? "");
		$$renderer.push(`<div class="relative"><button class="flex items-center gap-2 px-3 py-2 rounded-md bg-bg-secondary border border-border hover:border-border-focus transition-colors text-sm w-full"><svg class="w-4 h-4 text-text-muted shrink-0" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M5 2v8m0 0a2 2 0 104 0 2 2 0 00-4 0zm6-6v2a2 2 0 01-2 2H7"></path></svg> <span class="truncate font-mono">${escape_html(currentBranch() || "HEAD")}</span> `);
		if (store_get($$store_subs ??= {}, "$gitStatus", gitStatus)) {
			$$renderer.push("<!--[0-->");
			if (store_get($$store_subs ??= {}, "$gitStatus", gitStatus).ahead > 0) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<span class="text-xs text-success">${escape_html(store_get($$store_subs ??= {}, "$gitStatus", gitStatus).ahead)}↑</span>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--> `);
			if (store_get($$store_subs ??= {}, "$gitStatus", gitStatus).behind > 0) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<span class="text-xs text-warning">${escape_html(store_get($$store_subs ??= {}, "$gitStatus", gitStatus).behind)}↓</span>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]-->`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> <svg${attr_class(`w-3 h-3 text-text-muted ml-auto shrink-0 transition-transform ${stringify("")}`)} viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M3 5l3 3 3-3"></path></svg></button> `);
		$$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--></div>`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}
//#endregion
//#region src/lib/components/git/GitPanel.svelte
function GitPanel($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		let showDiff = derived(() => store_get($$store_subs ??= {}, "$gitDiff", gitDiff) !== null);
		let remoteBusy = null;
		$$renderer.push(`<div class="flex flex-col h-full bg-bg">`);
		if (showDiff()) {
			$$renderer.push("<!--[0-->");
			DiffViewer($$renderer);
		} else {
			$$renderer.push("<!--[-1-->");
			$$renderer.push(`<div class="p-3 border-b border-border shrink-0">`);
			BranchSwitcher($$renderer);
			$$renderer.push(`<!----> <div class="grid grid-cols-3 gap-1.5 mt-2"><button class="git-action svelte-13ynrl5">Refresh</button> <button class="git-action svelte-13ynrl5"${attr("disabled", remoteBusy !== null, true)}>${escape_html("Sync")}</button> <button class="git-action svelte-13ynrl5"${attr("disabled", remoteBusy !== null, true)}>${escape_html("Push")}</button></div></div> <div class="flex border-b border-border shrink-0"><button${attr_class(`flex-1 py-2 text-xs font-semibold text-center transition-colors ${stringify("text-text border-b-2 border-accent" )}`)}>Changes `);
			if (store_get($$store_subs ??= {}, "$gitStatus", gitStatus)) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<span class="ml-1 text-text-muted">(${escape_html(store_get($$store_subs ??= {}, "$gitStatus", gitStatus).staged.length + store_get($$store_subs ??= {}, "$gitStatus", gitStatus).unstaged.length)})</span>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--></button> <button${attr_class(`flex-1 py-2 text-xs font-semibold text-center transition-colors ${stringify("text-text-muted hover:text-text")}`)}>History</button></div> <div class="flex-1 overflow-auto">`);
			{
				$$renderer.push("<!--[0-->");
				if (store_get($$store_subs ??= {}, "$gitStatus", gitStatus)) {
					$$renderer.push("<!--[0-->");
					FileChanges($$renderer, {
						staged: store_get($$store_subs ??= {}, "$gitStatus", gitStatus).staged,
						unstaged: store_get($$store_subs ??= {}, "$gitStatus", gitStatus).unstaged
					});
				} else {
					$$renderer.push("<!--[-1-->");
					$$renderer.push(`<div class="flex items-center justify-center py-12 text-text-muted text-sm">Loading...</div>`);
				}
				$$renderer.push(`<!--]-->`);
			}
			$$renderer.push(`<!--]--></div> `);
			{
				$$renderer.push("<!--[0-->");
				CommitForm($$renderer, {
					hasStagedFiles: (store_get($$store_subs ??= {}, "$gitStatus", gitStatus)?.staged.length ?? 0) > 0
				});
			}
			$$renderer.push(`<!--]-->`);
		}
		$$renderer.push(`<!--]--></div>`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}
//#endregion
//#region src/lib/components/panels/SourcePanel.svelte
function SourcePanel($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		var $$store_subs;
		let projectId = derived(() => store_get($$store_subs ??= {}, "$activeProjectId", activeProjectId) ?? "");
		let project = derived(() => store_get($$store_subs ??= {}, "$projects", projects).find((item) => item.id === projectId()) ?? null);
		let emptyTitle = derived(() => store_get($$store_subs ??= {}, "$projects", projects).length === 0 ? "No projects yet" : "No project selected");
		if (!projectId() || !project()) {
			$$renderer.push("<!--[0-->");
			WorkspaceEmptyState($$renderer, {
				title: emptyTitle(),
				description: "Select or create a folder with the + button in the project bar, then switch to Source."
			});
		} else if (project().isGitRepo) {
			$$renderer.push("<!--[1-->");
			$$renderer.push(`<div class="panel-fill svelte-26nvof">`);
			GitPanel($$renderer, { projectId: projectId() });
			$$renderer.push(`<!----></div>`);
		} else {
			$$renderer.push("<!--[-1-->");
			WorkspaceEmptyState($$renderer, {
				title: "Not a git repository",
				description: "Run git init in the terminal to enable source control for this folder."
			});
		}
		$$renderer.push(`<!--]-->`);
		if ($$store_subs) unsubscribe_stores($$store_subs);
	});
}

export { SourcePanel as S };
//# sourceMappingURL=SourcePanel-FU3TiMKP.js.map
