import "./dist-JCXjvFc8.mjs";
import "./module-DajjN_0a.mjs";
import { _ as joinVoice, b as undeafen, g as deafen, v as leaveVoice, x as unmute, y as mute } from "./module-CTq-iEnt.mjs";
import "./upgrade-manifest-CZmc2sTl.mjs";
import "./upgrade-manifest-BLTLWrlM.mjs";
import { t as useChannelDocumentById } from "./channel-CgMvjz3u.mjs";
import { s as useRenownAuth } from "./dist-Bg4aWBAL.mjs";
import "./chunk-VBDAOXYI-DNRF7ECX.mjs";
import { t as useSelectedVoiceDocument } from "./voice-BDiXIm7P.mjs";
import { n as Avatar, t as PowerChatStyles } from "./PowerChatStyles-YoQHnBRk.mjs";
import { useCallback, useEffect, useRef, useState } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
//#region editors/power-chat-editor/components/VoiceRoom.tsx
function VoiceRoom({ channelName, participants, currentUserId, isInVoice, isMuted, isDeafened, isMicTesting, onJoin, onLeave, onMute, onUnmute, onDeafen, onUndeafen, onToggleMicTest }) {
	return /* @__PURE__ */ jsxs("div", {
		className: "powerchat-voice-room",
		children: [
			/* @__PURE__ */ jsxs("div", {
				className: "powerchat-voice-room-header",
				children: [/* @__PURE__ */ jsxs("svg", {
					width: "24",
					height: "24",
					viewBox: "0 0 16 16",
					children: [
						/* @__PURE__ */ jsx("path", {
							fill: "#23a559",
							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: "#23a559",
							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: "#23a559",
							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"
						})
					]
				}), /* @__PURE__ */ jsx("h2", { children: channelName })]
			}),
			/* @__PURE__ */ jsxs("div", {
				className: "powerchat-voice-participants-grid",
				children: [participants.map((p) => /* @__PURE__ */ jsxs("div", {
					className: `powerchat-voice-participant-card ${p.id === currentUserId ? "self" : ""}`,
					children: [/* @__PURE__ */ jsxs("div", {
						className: "powerchat-voice-avatar-wrap",
						children: [/* @__PURE__ */ jsx(Avatar, {
							name: p.name,
							size: 80
						}), p.isMuted && /* @__PURE__ */ jsx("div", {
							className: "powerchat-voice-mute-badge",
							children: /* @__PURE__ */ jsx("svg", {
								width: "16",
								height: "16",
								viewBox: "0 0 16 16",
								children: /* @__PURE__ */ jsx("path", {
									fill: "#fff",
									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"
								})
							})
						})]
					}), /* @__PURE__ */ jsx("span", {
						className: "powerchat-voice-participant-name",
						children: p.name
					})]
				}, p.id)), participants.length === 0 && /* @__PURE__ */ jsxs("div", {
					className: "powerchat-voice-empty",
					children: [/* @__PURE__ */ jsx("svg", {
						width: "48",
						height: "48",
						viewBox: "0 0 16 16",
						opacity: "0.3",
						children: /* @__PURE__ */ jsx("path", {
							fill: "currentColor",
							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 8z"
						})
					}), /* @__PURE__ */ jsx("p", { children: "No one is in this voice channel yet" })]
				})]
			}),
			/* @__PURE__ */ jsx("div", {
				className: "powerchat-voice-controls",
				children: isInVoice ? /* @__PURE__ */ jsxs(Fragment, { children: [
					/* @__PURE__ */ jsx("button", {
						className: `powerchat-voice-control-btn ${isMuted ? "active" : ""}`,
						onClick: isMuted ? onUnmute : onMute,
						title: isMuted ? "Unmute" : "Mute",
						children: isMuted ? /* @__PURE__ */ jsx("svg", {
							width: "24",
							height: "24",
							viewBox: "0 0 16 16",
							children: /* @__PURE__ */ jsx("path", {
								fill: "currentColor",
								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"
							})
						}) : /* @__PURE__ */ jsx("svg", {
							width: "24",
							height: "24",
							viewBox: "0 0 16 16",
							children: /* @__PURE__ */ jsx("path", {
								fill: "currentColor",
								d: "M8 12c2.2 0 4-1.8 4-4V4c0-2.2-1.8-4-4-4S4 1.8 4 4v4c0 2.2 1.8 4 4 4zm5.3-4c0 3-2.3 5.3-5.3 5.3S2.7 11 2.7 8H1.3c0 3.4 2.7 6.2 6 6.6V17c0 .6.4 1 1 1s1-.4 1-1v-2.4c3.4-.4 6.1-3.2 6.1-6.6H13.3z"
							})
						})
					}),
					/* @__PURE__ */ jsx("button", {
						className: `powerchat-voice-control-btn ${isDeafened ? "active" : ""}`,
						onClick: isDeafened ? onUndeafen : onDeafen,
						title: isDeafened ? "Undeafen" : "Deafen",
						children: isDeafened ? /* @__PURE__ */ jsx("svg", {
							width: "24",
							height: "24",
							viewBox: "0 0 16 16",
							children: /* @__PURE__ */ jsx("path", {
								fill: "currentColor",
								d: "M1.3 1.7L0 3l3.5 3.5C2 7.5 1 9.1 1 11v1c0 .6.4 1 1 1h2l4 3v-5.2l3.7 3.7c-.6.4-1.3.7-2 .9v1.1c1-.2 1.9-.6 2.7-1.2L14.7 17l1.3-1.3L1.3 1.7zM12 8.6V4l-4.3 3.3L12 8.6zM8 1L5.5 3.2 8 5.5V1z"
							})
						}) : /* @__PURE__ */ jsx("svg", {
							width: "24",
							height: "24",
							viewBox: "0 0 16 16",
							children: /* @__PURE__ */ jsx("path", {
								fill: "currentColor",
								d: "M12 8V4c0-2.2-1.8-4-4-4L4 4v4c0 1.1.4 2 1 2.7V12l3 2.3V16l4-3h2c.6 0 1-.4 1-1v-1c0-1.9-1-3.5-2.5-4.5L12 8zm-2.5 0A2.5 2.5 0 0 0 8 5.5v4.6A2.5 2.5 0 0 0 9.5 8z"
							})
						})
					}),
					/* @__PURE__ */ jsx("button", {
						className: `powerchat-voice-control-btn mic-test ${isMicTesting ? "active" : ""}`,
						onClick: onToggleMicTest,
						title: isMicTesting ? "Stop mic test" : "Test microphone",
						children: /* @__PURE__ */ jsx("svg", {
							width: "24",
							height: "24",
							viewBox: "0 0 24 24",
							children: /* @__PURE__ */ jsx("path", {
								fill: "currentColor",
								d: "M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm-1-9c0-.55.45-1 1-1s1 .45 1 1v6c0 .55-.45 1-1 1s-1-.45-1-1V5zm6 6c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z"
							})
						})
					}),
					/* @__PURE__ */ jsx("button", {
						className: "powerchat-voice-control-btn disconnect",
						onClick: onLeave,
						title: "Disconnect",
						children: /* @__PURE__ */ jsx("svg", {
							width: "24",
							height: "24",
							viewBox: "0 0 24 24",
							children: /* @__PURE__ */ jsx("path", {
								fill: "currentColor",
								d: "M12 9c-1.6 0-3.15.25-4.6.72v3.1c0 .39-.23.75-.56.9-.98.49-1.87 1.12-2.66 1.85-.18.18-.43.28-.7.28-.28 0-.53-.11-.71-.29L.29 13.08a.956.956 0 0 1 0-1.36C3.44 8.7 7.5 7 12 7s8.56 1.7 11.71 4.72c.18.18.29.44.29.71 0 .28-.11.53-.29.71l-2.48 2.48c-.18.18-.43.29-.71.29-.27 0-.52-.11-.7-.28a11.27 11.27 0 0 0-2.67-1.85.996.996 0 0 1-.56-.9v-3.1C15.15 9.25 13.6 9 12 9z"
							})
						})
					})
				] }) : /* @__PURE__ */ jsxs("button", {
					className: "powerchat-voice-join-btn",
					onClick: onJoin,
					children: [/* @__PURE__ */ jsx("svg", {
						width: "20",
						height: "20",
						viewBox: "0 0 16 16",
						children: /* @__PURE__ */ jsx("path", {
							fill: "currentColor",
							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 8z"
						})
					}), "Join Voice Channel"]
				})
			})
		]
	});
}
//#endregion
//#region editors/power-chat-editor/hooks/useVoiceEngine.ts
/**
* Generates a short tone using the Web Audio API.
* Used for mute/unmute/deafen/undeafen feedback sounds.
*/
function playTone(frequency, duration, volume = .15) {
	try {
		const ctx = new AudioContext();
		const osc = ctx.createOscillator();
		const gain = ctx.createGain();
		osc.type = "sine";
		osc.frequency.value = frequency;
		gain.gain.value = volume;
		gain.gain.setValueAtTime(volume, ctx.currentTime);
		gain.gain.exponentialRampToValueAtTime(.001, ctx.currentTime + duration);
		osc.connect(gain);
		gain.connect(ctx.destination);
		osc.start();
		osc.stop(ctx.currentTime + duration);
		osc.onended = () => ctx.close().catch(() => {});
	} catch {}
}
/** Two-tone "boop" for mute */
function playMuteSound() {
	playTone(480, .12, .12);
	setTimeout(() => playTone(340, .15, .1), 80);
}
/** Two-tone "boop" (ascending) for unmute */
function playUnmuteSound() {
	playTone(340, .12, .12);
	setTimeout(() => playTone(480, .15, .1), 80);
}
/** Lower two-tone for deafen */
function playDeafenSound() {
	playTone(380, .12, .12);
	setTimeout(() => playTone(260, .18, .1), 80);
}
/** Ascending tone for undeafen */
function playUndeafenSound() {
	playTone(260, .12, .12);
	setTimeout(() => playTone(440, .15, .1), 80);
}
/** Join sound - pleasant chime */
function playJoinSound() {
	playTone(523, .1, .1);
	setTimeout(() => playTone(659, .1, .1), 100);
	setTimeout(() => playTone(784, .15, .08), 200);
}
/** Leave/disconnect sound */
function playLeaveSound() {
	playTone(784, .1, .1);
	setTimeout(() => playTone(523, .2, .08), 100);
}
function useVoiceEngine(_roomId, _userId, _isActive) {
	const streamRef = useRef(null);
	const audioContextRef = useRef(null);
	const loopbackNodeRef = useRef(null);
	return {
		startAudio: useCallback(async () => {
			try {
				streamRef.current = await navigator.mediaDevices.getUserMedia({ audio: {
					echoCancellation: true,
					noiseSuppression: true,
					autoGainControl: true
				} });
				audioContextRef.current = new AudioContext();
				playJoinSound();
				console.log("[VoiceEngine] Microphone captured, ready for connections");
			} catch (err) {
				console.error("[VoiceEngine] Failed to capture microphone:", err);
			}
		}, []),
		stopAudio: useCallback(() => {
			if (loopbackNodeRef.current) {
				loopbackNodeRef.current.disconnect();
				loopbackNodeRef.current = null;
			}
			if (streamRef.current) {
				for (const track of streamRef.current.getTracks()) track.stop();
				streamRef.current = null;
			}
			if (audioContextRef.current) {
				audioContextRef.current.close().catch(() => {});
				audioContextRef.current = null;
			}
			playLeaveSound();
		}, []),
		setMuted: useCallback((muted) => {
			if (streamRef.current) for (const track of streamRef.current.getAudioTracks()) track.enabled = !muted;
		}, []),
		playMute: useCallback(() => playMuteSound(), []),
		playUnmute: useCallback(() => playUnmuteSound(), []),
		playDeafen: useCallback(() => playDeafenSound(), []),
		playUndeafen: useCallback(() => playUndeafenSound(), []),
		toggleMicTest: useCallback(() => {
			const ctx = audioContextRef.current;
			const stream = streamRef.current;
			if (loopbackNodeRef.current) {
				loopbackNodeRef.current.disconnect();
				loopbackNodeRef.current = null;
				return false;
			}
			if (ctx && stream) {
				const source = ctx.createMediaStreamSource(stream);
				source.connect(ctx.destination);
				loopbackNodeRef.current = source;
				return true;
			}
			return false;
		}, [])
	};
}
//#endregion
//#region editors/power-chat-editor/voice-editor.tsx
function useCurrentUser() {
	const auth = useRenownAuth();
	const did = auth.user?.did;
	const displayName = auth.displayName || auth.user?.profile?.username || auth.ensName || auth.displayAddress || "Anonymous";
	const avatarUrl = auth.avatarUrl || auth.user?.profile?.userImage || null;
	return {
		id: did || "anonymous",
		name: displayName,
		avatarUrl,
		isLoggedIn: auth.status === "authorized" && !!did
	};
}
function VoiceEditor() {
	const [voiceDoc, dispatch] = useSelectedVoiceDocument();
	const currentUser = useCurrentUser();
	const channelId = voiceDoc.state.global.channelId;
	const [channelDoc] = useChannelDocumentById(channelId);
	const voiceState = voiceDoc.state.global;
	const channelName = channelDoc?.state.global.name || channelDoc?.header.name || voiceDoc.header.name || "Voice";
	const me = voiceState.activeParticipants.find((p) => p.id === currentUser.id);
	const isInVoice = !!me;
	const isMuted = me?.isMuted ?? false;
	const isDeafened = me?.isDeafened ?? false;
	const [isMicTesting, setIsMicTesting] = useState(false);
	const { startAudio, stopAudio, setMuted: setAudioMuted, playMute, playUnmute, playDeafen, playUndeafen, toggleMicTest } = useVoiceEngine(voiceDoc.header.id, currentUser.id, isInVoice);
	const handleJoin = useCallback(() => {
		if (!currentUser.isLoggedIn) return;
		dispatch(joinVoice({
			id: currentUser.id,
			name: currentUser.name,
			avatar: currentUser.avatarUrl,
			joinedAt: (/* @__PURE__ */ new Date()).toISOString()
		}));
		startAudio();
	}, [
		dispatch,
		currentUser,
		startAudio
	]);
	const handleLeave = useCallback(() => {
		dispatch(leaveVoice({ participantId: currentUser.id }));
		setIsMicTesting(false);
		stopAudio();
	}, [
		dispatch,
		currentUser.id,
		stopAudio
	]);
	const handleMute = useCallback(() => {
		dispatch(mute({ participantId: currentUser.id }));
		setAudioMuted(true);
		playMute();
	}, [
		dispatch,
		currentUser.id,
		setAudioMuted,
		playMute
	]);
	const handleUnmute = useCallback(() => {
		dispatch(unmute({ participantId: currentUser.id }));
		setAudioMuted(false);
		playUnmute();
	}, [
		dispatch,
		currentUser.id,
		setAudioMuted,
		playUnmute
	]);
	const handleDeafen = useCallback(() => {
		dispatch(deafen({ participantId: currentUser.id }));
		setAudioMuted(true);
		playDeafen();
	}, [
		dispatch,
		currentUser.id,
		setAudioMuted,
		playDeafen
	]);
	const handleUndeafen = useCallback(() => {
		dispatch(undeafen({ participantId: currentUser.id }));
		playUndeafen();
	}, [
		dispatch,
		currentUser.id,
		playUndeafen
	]);
	const handleToggleMicTest = useCallback(() => {
		setIsMicTesting(toggleMicTest());
	}, [toggleMicTest]);
	useEffect(() => {
		return () => {
			stopAudio();
		};
	}, [stopAudio]);
	return /* @__PURE__ */ jsxs("div", {
		className: "powerchat-channel-editor",
		children: [/* @__PURE__ */ jsx(PowerChatStyles, {}), /* @__PURE__ */ jsx(VoiceRoom, {
			channelName,
			participants: voiceState.activeParticipants,
			currentUserId: currentUser.id,
			isInVoice,
			isMuted,
			isDeafened,
			isMicTesting,
			onJoin: handleJoin,
			onLeave: handleLeave,
			onMute: handleMute,
			onUnmute: handleUnmute,
			onDeafen: handleDeafen,
			onUndeafen: handleUndeafen,
			onToggleMicTest: handleToggleMicTest
		})]
	});
}
//#endregion
export { VoiceEditor as default };

//# sourceMappingURL=voice-editor-DZuGUG4n.mjs.map