import "./dist-JCXjvFc8.mjs";
import { C as setChannelType, x as setChannelName } from "./module-DajjN_0a.mjs";
import { w as setChannelId } from "./module-D2kCeCli.mjs";
import { _ as removeChannel, h as addChannel, v as reorderChannels, y as setGroupName } from "./module-Du2F4HUY.mjs";
import { C as setChannelId$1 } from "./module-CTq-iEnt.mjs";
import "./upgrade-manifest-CZmc2sTl.mjs";
import "./upgrade-manifest-BPYXTVvG.mjs";
import "./upgrade-manifest-BSZO2Je2.mjs";
import "./upgrade-manifest-BLTLWrlM.mjs";
import { n as useChannelDocumentsInSelectedDrive } from "./channel-CgMvjz3u.mjs";
import { d as useSetPHAppConfig, l as useSelectedDrive, n as dispatchActions, r as setSelectedNode, s as useRenownAuth, t as addDocument, u as useSelectedNode } from "./dist-Bg4aWBAL.mjs";
import "./chunk-VBDAOXYI-DNRF7ECX.mjs";
import { n as useChatDocumentsInSelectedDrive } from "./chat-B7rmcBJa.mjs";
import { n as useGroupDocumentsInSelectedDrive } from "./group-DqixM7sY.mjs";
import { r as useVoiceDocumentsInSelectedDrive } from "./voice-BDiXIm7P.mjs";
import { n as Avatar, t as PowerChatStyles } from "./PowerChatStyles-YoQHnBRk.mjs";
import React, { useCallback, useMemo, useRef, useState } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
//#region editors/power-chat/config.ts
const editorConfig = {
	isDragAndDropEnabled: false,
	allowedDocumentTypes: []
};
//#endregion
//#region editors/power-chat/components/ChannelList.tsx
function ChannelList({ groupId, groupName, channels, activeChannelId, isCollapsed, onToggle, onSelectChannel, voiceParticipants, children }) {
	const [dropTarget, setDropTarget] = useState(null);
	const dragRef = useRef(null);
	const handleDragStart = useCallback((e, channelId) => {
		dragRef.current = {
			channelId,
			sourceGroupId: groupId
		};
		e.dataTransfer.setData("application/powerchat-channel", JSON.stringify({
			channelId,
			sourceGroupId: groupId
		}));
		e.dataTransfer.effectAllowed = "move";
	}, [groupId]);
	const handleDragOver = useCallback((e, index) => {
		if (!e.dataTransfer.types.includes("application/powerchat-channel")) return;
		e.preventDefault();
		e.dataTransfer.dropEffect = "move";
		setDropTarget(index);
	}, []);
	const handleDragLeave = useCallback(() => {
		setDropTarget(null);
	}, []);
	const handleDrop = useCallback(async (e, targetIndex) => {
		e.preventDefault();
		setDropTarget(null);
		const data = e.dataTransfer.getData("application/powerchat-channel");
		if (!data) return;
		const { channelId, sourceGroupId } = JSON.parse(data);
		if (sourceGroupId === groupId) {
			if (!groupId) return;
			const currentIds = channels.map((ch) => ch.header.id);
			const fromIndex = currentIds.indexOf(channelId);
			if (fromIndex === -1) return;
			const newIds = [...currentIds];
			newIds.splice(fromIndex, 1);
			newIds.splice(targetIndex > fromIndex ? targetIndex - 1 : targetIndex, 0, channelId);
			await dispatchActions(reorderChannels({ channelIds: newIds }), groupId);
		} else {
			if (sourceGroupId) await dispatchActions(removeChannel({ channelId }), sourceGroupId);
			if (groupId) {
				await dispatchActions(addChannel({ channelId }), groupId);
				const newIds = channels.map((ch) => ch.header.id);
				newIds.splice(targetIndex, 0, channelId);
				await dispatchActions(reorderChannels({ channelIds: newIds }), groupId);
			}
		}
	}, [groupId, channels]);
	const handleGroupDrop = useCallback(async (e) => {
		e.preventDefault();
		setDropTarget(null);
		const data = e.dataTransfer.getData("application/powerchat-channel");
		if (!data || !groupId) return;
		const { channelId, sourceGroupId } = JSON.parse(data);
		if (sourceGroupId === groupId) return;
		if (sourceGroupId) await dispatchActions(removeChannel({ channelId }), sourceGroupId);
		await dispatchActions(addChannel({ channelId }), groupId);
	}, [groupId]);
	return /* @__PURE__ */ jsxs("div", {
		className: "powerchat-category",
		children: [/* @__PURE__ */ jsxs("button", {
			className: "powerchat-category-header",
			onClick: onToggle,
			onDragOver: useCallback((e) => {
				if (!e.dataTransfer.types.includes("application/powerchat-channel") || !groupId) return;
				e.preventDefault();
				e.dataTransfer.dropEffect = "move";
			}, [groupId]),
			onDrop: handleGroupDrop,
			children: [/* @__PURE__ */ jsx("svg", {
				className: `powerchat-category-arrow ${isCollapsed ? "collapsed" : ""}`,
				width: "12",
				height: "12",
				viewBox: "0 0 12 12",
				children: /* @__PURE__ */ jsx("path", {
					fill: "currentColor",
					d: "M2 4l4 4 4-4"
				})
			}), /* @__PURE__ */ jsx("span", { children: groupName.toUpperCase() })]
		}), !isCollapsed && /* @__PURE__ */ jsxs(Fragment, { children: [
			channels.map((ch, index) => {
				const isPrivate = ch.state.global.visibility === "PRIVATE";
				const isVoice = ch.state.global.channelType === "VOICE";
				const isSelected = activeChannelId === ch.header.id;
				const name = ch.state.global.name || ch.header.name || "unnamed";
				const voiceUsers = voiceParticipants.get(ch.header.id) || [];
				return /* @__PURE__ */ jsxs(React.Fragment, { children: [dropTarget === index && /* @__PURE__ */ jsx("div", { className: "powerchat-drop-indicator" }), /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsxs("button", {
					className: `powerchat-channel-item ${isSelected ? "active" : ""}`,
					onClick: () => onSelectChannel(ch.header.id),
					draggable: true,
					onDragStart: (e) => handleDragStart(e, ch.header.id),
					onDragOver: (e) => handleDragOver(e, index),
					onDragLeave: handleDragLeave,
					children: [/* @__PURE__ */ jsx("span", {
						className: "powerchat-channel-icon",
						children: isVoice ? /* @__PURE__ */ jsxs("svg", {
							width: "16",
							height: "16",
							viewBox: "0 0 16 16",
							children: [
								/* @__PURE__ */ jsx("path", {
									fill: "currentColor",
									d: "M11.536 14.01A8.47 8.47 0 0 0 14.026 8a8.47 8.47 0 0 0-2.49-6.01l-.708.707A7.48 7.48 0 0 1 13.026 8c0 2.071-.84 3.946-2.198 5.303l.708.707z"
								}),
								/* @__PURE__ */ jsx("path", {
									fill: "currentColor",
									d: "M10.121 12.596A6.48 6.48 0 0 0 12.026 8a6.48 6.48 0 0 0-1.905-4.596l-.707.707A5.48 5.48 0 0 1 11.026 8a5.48 5.48 0 0 1-1.612 3.889l.707.707z"
								}),
								/* @__PURE__ */ jsx("path", {
									fill: "currentColor",
									d: "M8.707 11.182A4.49 4.49 0 0 0 10.026 8a4.49 4.49 0 0 0-1.319-3.182l-.707.707A3.49 3.49 0 0 1 9.026 8a3.49 3.49 0 0 1-1.026 2.475l.707.707zM6.026 4v8l-3-3H1.026v-2h2l3-3z"
								})
							]
						}) : isPrivate ? /* @__PURE__ */ jsx("svg", {
							width: "16",
							height: "16",
							viewBox: "0 0 16 16",
							children: /* @__PURE__ */ jsx("path", {
								fill: "currentColor",
								d: "M13 6h-1V4.5C12 2.019 9.981 0 7.5 0S3 2.019 3 4.5V6H2a1 1 0 0 0-1 1v7a1 1 0 0 0 1 1h11a1 1 0 0 0 1-1V7a1 1 0 0 0-1-1zM4.5 4.5C4.5 2.846 5.846 1.5 7.5 1.5S10.5 2.846 10.5 4.5V6h-6V4.5z"
							})
						}) : /* @__PURE__ */ jsx("span", {
							className: "powerchat-hash",
							children: "#"
						})
					}), /* @__PURE__ */ jsx("span", {
						className: "powerchat-channel-name",
						children: name
					})]
				}), isVoice && voiceUsers.length > 0 && /* @__PURE__ */ jsx("div", {
					className: "powerchat-voice-users",
					children: voiceUsers.map((u) => /* @__PURE__ */ jsxs("div", {
						className: "powerchat-voice-user",
						children: [
							/* @__PURE__ */ jsx(Avatar, {
								name: u.name,
								size: 20
							}),
							/* @__PURE__ */ jsx("span", {
								className: "powerchat-voice-user-name",
								children: u.name
							}),
							u.isMuted && /* @__PURE__ */ jsx("span", {
								className: "powerchat-voice-muted-icon",
								children: /* @__PURE__ */ jsx("svg", {
									width: "12",
									height: "12",
									viewBox: "0 0 16 16",
									children: /* @__PURE__ */ jsx("path", {
										fill: "#f23f43",
										d: "M13 8c0-.6-.1-1.2-.3-1.7l-1.1 1.1c0 .2.1.4.1.6 0 2.2-1.8 4-4 4-.2 0-.4 0-.6-.1l-1.1 1.1c.5.2 1.1.3 1.7.3 3 0 5.3-2.3 5.3-5.3zM9 3.1V1c0-.6-.4-1-1-1S7 .4 7 1v5.2L9 3.1zM1.3 1.7L0 3l4.3 4.3C4.1 7.5 4 7.7 4 8c0 2.2 1.8 4 4 4 .3 0 .5-.1.7-.1L10.4 13.6c-.7.3-1.5.4-2.4.4-3 0-5.3-2.3-5.3-5.3H1.3c0 3.4 2.7 6.2 6 6.6V17c0 .6.4 1 1 1s1-.4 1-1v-1.7c1-.1 1.8-.5 2.6-1L14.7 17l1.3-1.3L1.3 1.7z"
									})
								})
							})
						]
					}, u.id))
				})] })] }, ch.header.id);
			}),
			dropTarget === channels.length && /* @__PURE__ */ jsx("div", { className: "powerchat-drop-indicator" }),
			/* @__PURE__ */ jsx("div", {
				className: "powerchat-drop-zone",
				onDragOver: (e) => handleDragOver(e, channels.length),
				onDragLeave: handleDragLeave,
				onDrop: (e) => handleDrop(e, channels.length)
			}),
			children
		] })]
	});
}
//#endregion
//#region editors/power-chat/components/CreateChannel.tsx
function CreateChannel({ groupId }) {
	const [selectedDrive] = useSelectedDrive();
	const [isCreating, setIsCreating] = useState(false);
	const [showInput, setShowInput] = useState(null);
	const [name, setName] = useState("");
	const handleCreate = async () => {
		const trimmed = name.trim();
		if (!trimmed || isCreating || !showInput) return;
		const kind = showInput;
		setIsCreating(true);
		try {
			const driveId = selectedDrive.header.id;
			const channelNode = await addDocument(driveId, trimmed, "powerchat/channel");
			await dispatchActions(setChannelName({ name: trimmed }), channelNode.id);
			if (kind === "voice") await dispatchActions(setChannelType({ channelType: "VOICE" }), channelNode.id);
			let linkedDocId;
			if (kind === "voice") {
				const voiceNode = await addDocument(driveId, `${trimmed} (voice)`, "powerchat/voice");
				await dispatchActions(setChannelId$1({ channelId: channelNode.id }), voiceNode.id);
				linkedDocId = voiceNode.id;
			} else {
				const chatNode = await addDocument(driveId, `${trimmed} (chat)`, "powerchat/chat");
				await dispatchActions(setChannelId({ channelId: channelNode.id }), chatNode.id);
				linkedDocId = chatNode.id;
			}
			if (groupId) await dispatchActions(addChannel({ channelId: channelNode.id }), groupId);
			setSelectedNode(linkedDocId);
			setName("");
			setShowInput(null);
		} catch (e) {
			console.error("Failed to create channel:", e);
		} finally {
			setIsCreating(false);
		}
	};
	if (showInput) return /* @__PURE__ */ jsx("div", {
		className: "powerchat-create-inline",
		children: /* @__PURE__ */ jsxs("div", {
			className: "powerchat-create-input-row",
			children: [
				/* @__PURE__ */ jsx("span", {
					className: "powerchat-create-input-icon",
					children: showInput === "voice" ? /* @__PURE__ */ jsx("svg", {
						width: "14",
						height: "14",
						viewBox: "0 0 16 16",
						children: /* @__PURE__ */ jsx("path", {
							fill: "#23a559",
							d: "M6 4v8l-3-3H1v-2h2l3-3zm4.5 4a2.5 2.5 0 0 0-1.5-2.3v4.6A2.5 2.5 0 0 0 10.5 8zm-1.5-4.8v1.1A4 4 0 0 1 12 8a4 4 0 0 1-3 3.9v1.1A5 5 0 0 0 13 8a5 5 0 0 0-4-4.8z"
						})
					}) : /* @__PURE__ */ jsx("span", {
						style: {
							color: "#949ba4",
							fontWeight: 600
						},
						children: "#"
					})
				}),
				/* @__PURE__ */ jsx("input", {
					className: "powerchat-create-input",
					type: "text",
					placeholder: showInput === "voice" ? "Voice channel name..." : "Channel name...",
					value: name,
					onChange: (e) => setName(e.target.value),
					onKeyDown: (e) => {
						if (e.key === "Enter") handleCreate();
						if (e.key === "Escape") {
							setShowInput(null);
							setName("");
						}
					},
					autoFocus: true,
					disabled: isCreating
				}),
				/* @__PURE__ */ jsx("button", {
					className: "powerchat-create-confirm",
					onClick: handleCreate,
					disabled: !name.trim() || isCreating,
					children: isCreating ? "..." : "✓"
				}),
				/* @__PURE__ */ jsx("button", {
					className: "powerchat-create-cancel",
					onClick: () => {
						setShowInput(null);
						setName("");
					},
					children: "✕"
				})
			]
		})
	});
	return /* @__PURE__ */ jsxs("div", {
		className: "powerchat-create-inline",
		children: [/* @__PURE__ */ jsxs("button", {
			className: "powerchat-create-btn-inline",
			onClick: () => setShowInput("text"),
			children: [/* @__PURE__ */ jsx("span", {
				style: {
					fontWeight: 700,
					fontSize: 14
				},
				children: "#"
			}), /* @__PURE__ */ jsx("span", { children: "Add Text Channel" })]
		}), /* @__PURE__ */ jsxs("button", {
			className: "powerchat-create-btn-inline",
			onClick: () => setShowInput("voice"),
			children: [/* @__PURE__ */ jsx("svg", {
				width: "12",
				height: "12",
				viewBox: "0 0 16 16",
				fill: "currentColor",
				children: /* @__PURE__ */ jsx("path", { d: "M6 4v8l-3-3H1v-2h2l3-3zm4.5 4a2.5 2.5 0 0 0-1.5-2.3v4.6A2.5 2.5 0 0 0 10.5 8zm-1.5-4.8v1.1A4 4 0 0 1 12 8a4 4 0 0 1-3 3.9v1.1A5 5 0 0 0 13 8a5 5 0 0 0-4-4.8z" })
			}), /* @__PURE__ */ jsx("span", { children: "Add Voice Channel" })]
		})]
	});
}
//#endregion
//#region editors/power-chat/components/CreateGroup.tsx
function CreateGroup() {
	const [selectedDrive] = useSelectedDrive();
	const [isCreating, setIsCreating] = useState(false);
	const [showInput, setShowInput] = useState(false);
	const [name, setName] = useState("");
	const handleCreate = async () => {
		const trimmed = name.trim();
		if (!trimmed || isCreating) return;
		setIsCreating(true);
		try {
			const driveId = selectedDrive.header.id;
			const groupNode = await addDocument(driveId, trimmed, "powerchat/group");
			await dispatchActions(setGroupName({ name: trimmed }), groupNode.id);
			setName("");
			setShowInput(false);
		} catch (e) {
			console.error("Failed to create group:", e);
		} finally {
			setIsCreating(false);
		}
	};
	if (showInput) return /* @__PURE__ */ jsx("div", {
		className: "powerchat-create-section",
		children: /* @__PURE__ */ jsxs("div", {
			className: "powerchat-create-input-row",
			children: [
				/* @__PURE__ */ jsx("input", {
					className: "powerchat-create-input",
					type: "text",
					placeholder: "Group name...",
					value: name,
					onChange: (e) => setName(e.target.value),
					onKeyDown: (e) => {
						if (e.key === "Enter") handleCreate();
						if (e.key === "Escape") {
							setShowInput(false);
							setName("");
						}
					},
					autoFocus: true,
					disabled: isCreating
				}),
				/* @__PURE__ */ jsx("button", {
					className: "powerchat-create-confirm",
					onClick: handleCreate,
					disabled: !name.trim() || isCreating,
					children: isCreating ? "..." : "✓"
				}),
				/* @__PURE__ */ jsx("button", {
					className: "powerchat-create-cancel",
					onClick: () => {
						setShowInput(false);
						setName("");
					},
					children: "✕"
				})
			]
		})
	});
	return /* @__PURE__ */ jsx("div", {
		className: "powerchat-create-section",
		children: /* @__PURE__ */ jsxs("button", {
			className: "powerchat-create-btn",
			onClick: () => setShowInput(true),
			children: [/* @__PURE__ */ jsx("svg", {
				width: "14",
				height: "14",
				viewBox: "0 0 14 14",
				fill: "currentColor",
				children: /* @__PURE__ */ jsx("path", { d: "M7 0a1 1 0 0 1 1 1v5h5a1 1 0 1 1 0 2H8v5a1 1 0 1 1-2 0V8H1a1 1 0 0 1 0-2h5V1a1 1 0 0 1 1-1z" })
			}), /* @__PURE__ */ jsx("span", { children: "Add Group" })]
		})
	});
}
//#endregion
//#region editors/power-chat/editor.tsx
function buildSidebarGroups(groups, channels, currentUserId) {
	const channelMap = new Map(channels.map((ch) => [ch.header.id, ch]));
	const isVisible = (ch) => {
		if (ch.state.global.visibility !== "PRIVATE") return true;
		if (!currentUserId) return false;
		return ch.state.global.participants.some((p) => p.id === currentUserId);
	};
	const usedChannelIds = /* @__PURE__ */ new Set();
	const result = [];
	for (const group of groups) {
		const groupChannels = [];
		for (const chId of group.state.global.channelIds) {
			const ch = channelMap.get(chId);
			if (ch && isVisible(ch)) {
				groupChannels.push(ch);
				usedChannelIds.add(chId);
			}
		}
		result.push({
			id: group.header.id,
			name: group.state.global.name || group.header.name || "Unnamed Group",
			channels: groupChannels
		});
	}
	const ungrouped = channels.filter((ch) => !usedChannelIds.has(ch.header.id) && isVisible(ch));
	if (ungrouped.length > 0) result.push({
		id: null,
		name: "Channels",
		channels: ungrouped
	});
	return result;
}
function Editor({ children }) {
	useSetPHAppConfig(editorConfig);
	const allChannels = useChannelDocumentsInSelectedDrive();
	const allChats = useChatDocumentsInSelectedDrive();
	const allGroups = useGroupDocumentsInSelectedDrive();
	const allVoice = useVoiceDocumentsInSelectedDrive();
	const selectedNode = useSelectedNode();
	const currentUserId = useRenownAuth().user?.did;
	const [collapsedGroups, setCollapsedGroups] = useState(/* @__PURE__ */ new Set());
	const sidebarGroups = useMemo(() => buildSidebarGroups(allGroups || [], allChannels || [], currentUserId), [
		allGroups,
		allChannels,
		currentUserId
	]);
	const voiceParticipants = useMemo(() => {
		const map = /* @__PURE__ */ new Map();
		for (const v of allVoice || []) {
			const chId = v.state.global.channelId;
			if (chId && v.state.global.activeParticipants.length > 0) map.set(chId, v.state.global.activeParticipants);
		}
		return map;
	}, [allVoice]);
	const channelToDocId = useMemo(() => {
		const map = /* @__PURE__ */ new Map();
		for (const chat of allChats || []) {
			const chId = chat.state.global.channelId;
			if (chId) map.set(chId, chat.header.id);
		}
		for (const voice of allVoice || []) {
			const chId = voice.state.global.channelId;
			if (chId) map.set(chId, voice.header.id);
		}
		return map;
	}, [allChats, allVoice]);
	const showDocumentEditor = !!children;
	const toggleGroup = useCallback((key) => {
		setCollapsedGroups((prev) => {
			const next = new Set(prev);
			if (next.has(key)) next.delete(key);
			else next.add(key);
			return next;
		});
	}, []);
	const handleSelectChannel = useCallback((channelDocId) => {
		const docId = channelToDocId.get(channelDocId);
		if (docId) setSelectedNode(docId);
		else setSelectedNode(channelDocId);
	}, [channelToDocId]);
	const activeChannelId = useMemo(() => {
		if (!selectedNode) return null;
		const selectedChat = (allChats || []).find((c) => c.header.id === selectedNode.id);
		if (selectedChat?.state.global.channelId) return selectedChat.state.global.channelId;
		const selectedVoice = (allVoice || []).find((v) => v.header.id === selectedNode.id);
		if (selectedVoice?.state.global.channelId) return selectedVoice.state.global.channelId;
		return selectedNode.id;
	}, [selectedNode, allChats]);
	return /* @__PURE__ */ jsxs("div", {
		className: "powerchat-root",
		children: [/* @__PURE__ */ jsx(PowerChatStyles, {}), /* @__PURE__ */ jsxs("div", {
			className: "powerchat-container",
			children: [/* @__PURE__ */ jsxs("div", {
				className: "powerchat-sidebar",
				children: [/* @__PURE__ */ jsx("div", {
					className: "powerchat-sidebar-header",
					children: /* @__PURE__ */ jsx("h2", { children: "PowerChat" })
				}), /* @__PURE__ */ jsxs("div", {
					className: "powerchat-sidebar-channels",
					children: [
						sidebarGroups.map((group) => {
							const groupKey = group.id || "__ungrouped__";
							return /* @__PURE__ */ jsx(ChannelList, {
								groupId: group.id,
								groupName: group.name,
								channels: group.channels,
								activeChannelId,
								isCollapsed: collapsedGroups.has(groupKey),
								onToggle: () => toggleGroup(groupKey),
								onSelectChannel: handleSelectChannel,
								voiceParticipants,
								children: group.id && /* @__PURE__ */ jsx(CreateChannel, { groupId: group.id })
							}, groupKey);
						}),
						sidebarGroups.length === 0 && /* @__PURE__ */ jsxs("div", {
							className: "powerchat-empty-channels",
							children: [/* @__PURE__ */ jsx("p", { children: "No channels yet" }), /* @__PURE__ */ jsx("p", {
								className: "powerchat-text-muted",
								children: "Create a group to get started"
							})]
						}),
						/* @__PURE__ */ jsxs("div", {
							style: { padding: "8px 8px 0" },
							children: [
								/* @__PURE__ */ jsx(CreateChannel, { groupId: null }),
								/* @__PURE__ */ jsx("div", { style: { height: 4 } }),
								/* @__PURE__ */ jsx(CreateGroup, {})
							]
						})
					]
				})]
			}), /* @__PURE__ */ jsx("div", {
				className: "powerchat-main",
				children: showDocumentEditor ? children : /* @__PURE__ */ jsx("div", {
					className: "powerchat-no-channel",
					children: /* @__PURE__ */ jsxs("div", {
						className: "powerchat-no-channel-inner",
						children: [
							/* @__PURE__ */ jsx("div", {
								className: "powerchat-no-channel-icon",
								children: "#"
							}),
							/* @__PURE__ */ jsx("h3", { children: "Select a channel" }),
							/* @__PURE__ */ jsx("p", { children: "Pick a channel from the sidebar to start chatting" })
						]
					})
				})
			})]
		})]
	});
}
//#endregion
export { Editor as default };

//# sourceMappingURL=editor-Dbp8DFEQ.mjs.map