/**
 * rDocs applet definitions — Doc Summary.
 */

import type { AppletDefinition, AppletLiveData } from "../../shared/applet-types";

const docSummary: AppletDefinition = {
	id: "doc-summary",
	label: "Doc Summary",
	icon: "📄",
	accentColor: "#d97706",
	ports: [
		{ name: "doc-in", type: "json", direction: "input" },
		{ name: "text-out", type: "text", direction: "output" },
	],
	renderCompact(data: AppletLiveData): string {
		const { snapshot } = data;
		const title = (snapshot.title as string) || "Untitled";
		const wordCount = (snapshot.wordCount as number) || 0;
		const lastEdit = (snapshot.lastEdit as string) || "";
		const preview = (snapshot.preview as string) || "No content";

		return `
			<div>
				<div style="font-size:13px;font-weight:600;margin-bottom:4px">${title}</div>
				<div style="font-size:10px;color:#94a3b8;margin-bottom:6px">${wordCount} words${lastEdit ? ` · ${lastEdit}` : ""}</div>
				<div style="font-size:11px;color:#cbd5e1;line-height:1.4;max-height:60px;overflow:hidden">${preview}</div>
			</div>
		`;
	},
	onInputReceived(portName, value, ctx) {
		if (portName === "doc-in" && value && typeof value === "object") {
			const doc = value as Record<string, unknown>;
			ctx.emitOutput("text-out", (doc.content as string) || (doc.preview as string) || "");
		}
	},
};

export const docsApplets: AppletDefinition[] = [docSummary];
