import { B as generateId } from "./dist-RakiOObV.mjs";
import { u as useFileNodesInSelectedDrive } from "./dist-DFMBprYi.mjs";
import { t as DocumentToolbar } from "./connect-B9rqkBQW.mjs";
import { g as actions } from "./module-DYhTlUAr.mjs";
import { t as usePipelineQueueDocumentById } from "./pipeline-queue-CioH53De.mjs";
import { g as actions$1 } from "./module-DZ7wp9Eh.mjs";
import { t as useSelectedSourceDocument } from "./source-_wEaBOI7.mjs";
import { t as TOOLBAR_CLASS } from "./theme-context-DeP5ztQx.mjs";
import { t as MarkdownPreview } from "./markdown-preview-CG0iT9BF.mjs";
import { useState } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
//#region editors/source-editor/editor.tsx
const SOURCE_TYPES = [
	"ARTICLE",
	"PAPER",
	"BOOK_CHAPTER",
	"TRANSCRIPT",
	"DOCUMENTATION",
	"CONVERSATION",
	"WEB_PAGE",
	"MANUAL_ENTRY"
];
const STATUS_COLORS = {
	INBOX: "bg-amber-500/20 text-amber-300 border-amber-500/30",
	EXTRACTING: "bg-blue-500/20 text-blue-300 border-blue-500/30",
	EXTRACTED: "bg-emerald-500/20 text-emerald-300 border-emerald-500/30",
	ARCHIVED: "bg-gray-500/20 text-gray-400 border-gray-500/30"
};
function ts() {
	return (/* @__PURE__ */ new Date()).toISOString();
}
function Editor() {
	const [document, dispatch] = useSelectedSourceDocument();
	const state = document.state.global;
	const initialized = !!state.title;
	const pipelineNodeId = (useFileNodesInSelectedDrive() ?? []).find((n) => n.documentType === "bai/pipeline-queue")?.id;
	const [pipelineDoc, pipelineDispatch] = usePipelineQueueDocumentById(pipelineNodeId);
	const [queued, setQueued] = useState(false);
	const [editing, setEditing] = useState(false);
	if (!initialized) return /* @__PURE__ */ jsx(IngestForm, { dispatch });
	const stats = state.extractionStats;
	const existingTask = (pipelineDoc?.state?.global)?.tasks?.find((t) => t.documentRef === document.header.id);
	const hasActiveTask = existingTask && existingTask.status !== "DONE" && existingTask.status !== "FAILED";
	function handleQueueForProcessing() {
		if (!pipelineDispatch || !document.header.id) return;
		if (hasActiveTask) {
			dispatch(actions$1.setSourceStatus({ status: "EXTRACTING" }));
			setQueued(true);
			return;
		}
		pipelineDispatch(actions.addTask({
			id: generateId(),
			taskType: "claim",
			target: state.title ?? "Untitled source",
			documentRef: document.header.id,
			createdAt: ts()
		}));
		dispatch(actions$1.setSourceStatus({ status: "EXTRACTING" }));
		setQueued(true);
	}
	const canQueue = state.status === "INBOX" && !!pipelineDispatch && !queued;
	if (editing) return /* @__PURE__ */ jsx(EditForm, {
		state,
		dispatch,
		onDone: () => setEditing(false)
	});
	return /* @__PURE__ */ jsx("div", {
		className: "min-h-screen",
		style: {
			backgroundColor: "var(--bai-bg)",
			color: "var(--bai-text)"
		},
		children: /* @__PURE__ */ jsxs("div", {
			className: "mx-auto max-w-5xl",
			children: [/* @__PURE__ */ jsx(DocumentToolbar, { toolbarClassName: TOOLBAR_CLASS }), /* @__PURE__ */ jsxs("div", {
				className: "flex gap-6 p-6",
				children: [/* @__PURE__ */ jsxs("div", {
					className: "min-w-0 flex-1 space-y-5 rounded-xl p-6",
					style: {
						backgroundColor: "var(--bai-surface)",
						border: "1px solid var(--bai-border)"
					},
					children: [
						/* @__PURE__ */ jsxs("div", {
							className: "flex items-center gap-3",
							children: [
								/* @__PURE__ */ jsx("span", {
									className: `rounded-full border px-3 py-1 text-xs font-semibold ${STATUS_COLORS[state.status ?? "INBOX"]}`,
									children: state.status
								}),
								state.sourceType && /* @__PURE__ */ jsx("span", {
									className: "rounded px-2 py-0.5 text-xs",
									style: {
										backgroundColor: "var(--bai-hover)",
										color: "var(--bai-text-tertiary)"
									},
									children: state.sourceType
								}),
								/* @__PURE__ */ jsxs("div", {
									className: "ml-auto flex items-center gap-2",
									children: [
										/* @__PURE__ */ jsx("button", {
											type: "button",
											onClick: () => setEditing(true),
											className: "rounded px-3 py-1 text-xs hover:opacity-80",
											style: {
												backgroundColor: "var(--bai-hover)",
												color: "var(--bai-text-tertiary)"
											},
											children: "Edit"
										}),
										canQueue && /* @__PURE__ */ jsxs("button", {
											type: "button",
											onClick: handleQueueForProcessing,
											className: "flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-xs font-semibold hover:opacity-80",
											style: {
												backgroundColor: "var(--bai-accent)",
												color: "var(--bai-accent-text)"
											},
											children: [/* @__PURE__ */ jsx("svg", {
												className: "h-3.5 w-3.5",
												viewBox: "0 0 24 24",
												fill: "none",
												stroke: "currentColor",
												strokeWidth: "2.5",
												children: /* @__PURE__ */ jsx("path", { d: "M22 12h-4l-3 9L9 3l-3 9H2" })
											}), "Queue for Processing"]
										}),
										queued && /* @__PURE__ */ jsx("span", {
											className: "text-xs text-emerald-400",
											children: "Queued — run /pipeline in Claude Code"
										}),
										!canQueue && !queued && /* @__PURE__ */ jsx("select", {
											value: state.status ?? "INBOX",
											onChange: (e) => dispatch(actions$1.setSourceStatus({ status: e.target.value })),
											className: "rounded px-2 py-1 text-xs",
											style: {
												backgroundColor: "var(--bai-bg)",
												color: "var(--bai-text-tertiary)",
												border: "1px solid var(--bai-border)"
											},
											children: [
												"INBOX",
												"EXTRACTING",
												"EXTRACTED",
												"ARCHIVED"
											].map((s) => /* @__PURE__ */ jsx("option", {
												value: s,
												children: s
											}, s))
										})
									]
								})
							]
						}),
						/* @__PURE__ */ jsx("h1", {
							className: "text-2xl font-bold",
							style: { color: "var(--bai-text)" },
							children: state.title
						}),
						state.description && /* @__PURE__ */ jsx("p", {
							className: "text-sm",
							style: { color: "var(--bai-text-tertiary)" },
							children: state.description
						}),
						/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h3", {
							className: "mb-2 text-xs font-semibold uppercase tracking-wider",
							style: { color: "var(--bai-text-muted)" },
							children: "Source Content"
						}), /* @__PURE__ */ jsx("div", {
							className: "max-h-[600px] overflow-y-auto rounded-lg px-5 py-4 scrollbar-thin",
							style: {
								backgroundColor: "var(--bai-deep)",
								border: "1px solid var(--bai-border)"
							},
							children: state.content ? /* @__PURE__ */ jsx(MarkdownPreview, { content: state.content }) : /* @__PURE__ */ jsx("p", {
								className: "text-sm",
								style: { color: "var(--bai-text-muted)" },
								children: "No content"
							})
						})] })
					]
				}), /* @__PURE__ */ jsxs("div", {
					className: "w-64 shrink-0 space-y-6 self-start rounded-xl p-5",
					style: {
						backgroundColor: "var(--bai-surface)",
						border: "1px solid var(--bai-border)"
					},
					children: [
						/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h4", {
							className: "mb-2 text-xs font-semibold uppercase tracking-wider",
							style: { color: "var(--bai-text-muted)" },
							children: "Provenance"
						}), /* @__PURE__ */ jsxs("div", {
							className: "space-y-1.5 text-xs",
							style: { color: "var(--bai-text-tertiary)" },
							children: [
								state.provenance?.author && /* @__PURE__ */ jsxs("div", {
									className: "flex justify-between",
									children: [/* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-faint)" },
										children: "Author"
									}), /* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-secondary)" },
										children: state.provenance.author
									})]
								}),
								state.provenance?.url && /* @__PURE__ */ jsxs("div", {
									className: "flex justify-between",
									children: [/* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-faint)" },
										children: "URL"
									}), /* @__PURE__ */ jsx("span", {
										className: "truncate max-w-[120px]",
										style: { color: "var(--bai-text-secondary)" },
										title: state.provenance.url,
										children: state.provenance.url
									})]
								}),
								state.provenance?.method && /* @__PURE__ */ jsxs("div", {
									className: "flex justify-between",
									children: [/* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-faint)" },
										children: "Method"
									}), /* @__PURE__ */ jsx("span", { children: state.provenance.method })]
								}),
								state.createdBy && /* @__PURE__ */ jsxs("div", {
									className: "flex justify-between",
									children: [/* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-faint)" },
										children: "Ingested by"
									}), /* @__PURE__ */ jsx("span", { children: state.createdBy })]
								})
							]
						})] }),
						stats && /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("hr", { style: { borderColor: "var(--bai-border)" } }), /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h4", {
							className: "mb-2 text-xs font-semibold uppercase tracking-wider",
							style: { color: "var(--bai-text-muted)" },
							children: "Extraction"
						}), /* @__PURE__ */ jsxs("div", {
							className: "space-y-1.5 text-xs",
							children: [
								/* @__PURE__ */ jsxs("div", {
									className: "flex justify-between",
									children: [/* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-faint)" },
										children: "Claims"
									}), /* @__PURE__ */ jsx("span", {
										className: "text-emerald-400 font-bold",
										children: stats.claimCount
									})]
								}),
								/* @__PURE__ */ jsxs("div", {
									className: "flex justify-between",
									children: [/* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-faint)" },
										children: "Skipped"
									}), /* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-tertiary)" },
										children: stats.skippedCount
									})]
								}),
								/* @__PURE__ */ jsxs("div", {
									className: "flex justify-between",
									children: [/* @__PURE__ */ jsx("span", {
										style: { color: "var(--bai-text-faint)" },
										children: "Skip rate"
									}), /* @__PURE__ */ jsxs("span", {
										className: stats.skipRate > .1 ? "text-red-400" : "text-emerald-400",
										children: [(stats.skipRate * 100).toFixed(1), "%"]
									})]
								})
							]
						})] })] }),
						/* @__PURE__ */ jsx("hr", { style: { borderColor: "var(--bai-border)" } }),
						/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsxs("h4", {
							className: "mb-2 text-xs font-semibold uppercase tracking-wider",
							style: { color: "var(--bai-text-muted)" },
							children: [
								"Extracted Claims (",
								(state.extractedClaims ?? []).length,
								")"
							]
						}), /* @__PURE__ */ jsx("div", {
							className: "space-y-1 max-h-48 overflow-y-auto",
							children: (state.extractedClaims ?? []).map((ref, i) => /* @__PURE__ */ jsx("p", {
								className: "truncate text-[10px] font-mono",
								style: { color: "var(--bai-text-faint)" },
								children: ref
							}, i))
						})] })
					]
				})]
			})]
		})
	});
}
/** Edit form — re-dispatches INGEST_SOURCE with updated values */
function EditForm({ state, dispatch, onDone }) {
	const [title, setTitle] = useState(state.title ?? "");
	const [desc, setDesc] = useState(state.description ?? "");
	const [content, setContent] = useState(state.content ?? "");
	const [sourceType, setSourceType] = useState(state.sourceType ?? "ARTICLE");
	const [author, setAuthor] = useState(state.provenance?.author ?? "");
	const [url, setUrl] = useState(state.provenance?.url ?? "");
	function handleSave() {
		dispatch(actions$1.ingestSource({
			title,
			content,
			sourceType,
			description: desc || void 0,
			author: author || void 0,
			url: url || void 0,
			createdAt: state.createdAt ?? ts(),
			createdBy: state.createdBy || void 0
		}));
		dispatch(actions$1.setSourceStatus({ status: "INBOX" }));
		onDone();
	}
	return /* @__PURE__ */ jsx("div", {
		className: "min-h-screen",
		style: {
			backgroundColor: "var(--bai-bg)",
			color: "var(--bai-text)"
		},
		children: /* @__PURE__ */ jsxs("div", {
			className: "mx-auto max-w-3xl",
			children: [/* @__PURE__ */ jsx(DocumentToolbar, { toolbarClassName: TOOLBAR_CLASS }), /* @__PURE__ */ jsx("div", {
				className: "p-6",
				children: /* @__PURE__ */ jsxs("div", {
					className: "rounded-xl p-8 space-y-4",
					style: {
						backgroundColor: "var(--bai-surface)",
						border: "1px solid var(--bai-border)"
					},
					children: [
						/* @__PURE__ */ jsxs("div", {
							className: "flex items-center justify-between",
							children: [/* @__PURE__ */ jsx("h2", {
								className: "text-lg font-bold",
								style: { color: "var(--bai-text)" },
								children: "Edit Source"
							}), /* @__PURE__ */ jsx("button", {
								type: "button",
								onClick: onDone,
								className: "rounded px-3 py-1 text-xs hover:opacity-80",
								style: {
									backgroundColor: "var(--bai-hover)",
									color: "var(--bai-text-tertiary)"
								},
								children: "Cancel"
							})]
						}),
						/* @__PURE__ */ jsx("input", {
							type: "text",
							value: title,
							onChange: (e) => setTitle(e.target.value),
							placeholder: "Source title",
							className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
							style: {
								backgroundColor: "var(--bai-bg)",
								color: "var(--bai-text-secondary)",
								border: "1px solid var(--bai-border)"
							}
						}),
						/* @__PURE__ */ jsx("input", {
							type: "text",
							value: desc,
							onChange: (e) => setDesc(e.target.value),
							placeholder: "Brief description (optional)",
							className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
							style: {
								backgroundColor: "var(--bai-bg)",
								color: "var(--bai-text-secondary)",
								border: "1px solid var(--bai-border)"
							}
						}),
						/* @__PURE__ */ jsxs("div", {
							className: "grid grid-cols-3 gap-3",
							children: [
								/* @__PURE__ */ jsx("select", {
									value: sourceType,
									onChange: (e) => setSourceType(e.target.value),
									className: "rounded-lg px-3 py-2 text-sm",
									style: {
										backgroundColor: "var(--bai-bg)",
										color: "var(--bai-text-secondary)",
										border: "1px solid var(--bai-border)"
									},
									children: SOURCE_TYPES.map((t) => /* @__PURE__ */ jsx("option", {
										value: t,
										children: t
									}, t))
								}),
								/* @__PURE__ */ jsx("input", {
									type: "text",
									value: author,
									onChange: (e) => setAuthor(e.target.value),
									placeholder: "Author",
									className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
									style: {
										backgroundColor: "var(--bai-bg)",
										color: "var(--bai-text-secondary)",
										border: "1px solid var(--bai-border)"
									}
								}),
								/* @__PURE__ */ jsx("input", {
									type: "text",
									value: url,
									onChange: (e) => setUrl(e.target.value),
									placeholder: "URL (optional)",
									className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
									style: {
										backgroundColor: "var(--bai-bg)",
										color: "var(--bai-text-secondary)",
										border: "1px solid var(--bai-border)"
									}
								})
							]
						}),
						/* @__PURE__ */ jsx("textarea", {
							value: content,
							onChange: (e) => setContent(e.target.value),
							placeholder: "Source content...",
							rows: 16,
							className: "w-full rounded-lg px-4 py-3 font-mono text-sm outline-none focus:border-[#cba6f7]/50",
							style: {
								backgroundColor: "var(--bai-deep)",
								color: "var(--bai-text-secondary)",
								border: "1px solid var(--bai-border)"
							}
						}),
						/* @__PURE__ */ jsx("button", {
							type: "button",
							disabled: !title.trim() || !content.trim(),
							onClick: handleSave,
							className: "rounded-lg px-4 py-2 text-sm font-medium hover:opacity-80 disabled:opacity-40",
							style: {
								backgroundColor: "var(--bai-accent)",
								color: "var(--bai-accent-text)"
							},
							children: "Save Changes"
						})
					]
				})
			})]
		})
	});
}
/** Initial form for new (empty) source documents */
function IngestForm({ dispatch }) {
	const [title, setTitle] = useState("");
	const [content, setContent] = useState("");
	const [sourceType, setSourceType] = useState("ARTICLE");
	const [desc, setDesc] = useState("");
	const [url, setUrl] = useState("");
	const [author, setAuthor] = useState("");
	return /* @__PURE__ */ jsx("div", {
		className: "min-h-screen",
		style: {
			backgroundColor: "var(--bai-bg)",
			color: "var(--bai-text)"
		},
		children: /* @__PURE__ */ jsxs("div", {
			className: "mx-auto max-w-3xl",
			children: [/* @__PURE__ */ jsx(DocumentToolbar, { toolbarClassName: TOOLBAR_CLASS }), /* @__PURE__ */ jsx("div", {
				className: "p-6",
				children: /* @__PURE__ */ jsxs("div", {
					className: "rounded-xl p-8 space-y-4",
					style: {
						backgroundColor: "var(--bai-surface)",
						border: "1px solid var(--bai-border)"
					},
					children: [
						/* @__PURE__ */ jsx("h2", {
							className: "text-lg font-bold",
							style: { color: "var(--bai-text)" },
							children: "Add Source Material"
						}),
						/* @__PURE__ */ jsx("p", {
							className: "text-xs",
							style: { color: "var(--bai-text-muted)" },
							children: "Paste raw content here — articles, notes, transcripts. The AI agent will extract atomic claims from it."
						}),
						/* @__PURE__ */ jsx("input", {
							type: "text",
							value: title,
							onChange: (e) => setTitle(e.target.value),
							placeholder: "Source title",
							className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
							style: {
								backgroundColor: "var(--bai-bg)",
								color: "var(--bai-text-secondary)",
								border: "1px solid var(--bai-border)"
							}
						}),
						/* @__PURE__ */ jsx("input", {
							type: "text",
							value: desc,
							onChange: (e) => setDesc(e.target.value),
							placeholder: "Brief description (optional)",
							className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
							style: {
								backgroundColor: "var(--bai-bg)",
								color: "var(--bai-text-secondary)",
								border: "1px solid var(--bai-border)"
							}
						}),
						/* @__PURE__ */ jsxs("div", {
							className: "grid grid-cols-3 gap-3",
							children: [
								/* @__PURE__ */ jsx("select", {
									value: sourceType,
									onChange: (e) => setSourceType(e.target.value),
									className: "rounded-lg px-3 py-2 text-sm",
									style: {
										backgroundColor: "var(--bai-bg)",
										color: "var(--bai-text-secondary)",
										border: "1px solid var(--bai-border)"
									},
									children: SOURCE_TYPES.map((t) => /* @__PURE__ */ jsx("option", {
										value: t,
										children: t
									}, t))
								}),
								/* @__PURE__ */ jsx("input", {
									type: "text",
									value: author,
									onChange: (e) => setAuthor(e.target.value),
									placeholder: "Author",
									className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
									style: {
										backgroundColor: "var(--bai-bg)",
										color: "var(--bai-text-secondary)",
										border: "1px solid var(--bai-border)"
									}
								}),
								/* @__PURE__ */ jsx("input", {
									type: "text",
									value: url,
									onChange: (e) => setUrl(e.target.value),
									placeholder: "URL (optional)",
									className: "w-full rounded-lg px-3 py-2 text-sm outline-none focus:border-[#cba6f7]/50",
									style: {
										backgroundColor: "var(--bai-bg)",
										color: "var(--bai-text-secondary)",
										border: "1px solid var(--bai-border)"
									}
								})
							]
						}),
						/* @__PURE__ */ jsx("textarea", {
							value: content,
							onChange: (e) => setContent(e.target.value),
							placeholder: "Paste source content here...",
							rows: 12,
							className: "w-full rounded-lg px-4 py-3 font-mono text-sm outline-none focus:border-[#cba6f7]/50",
							style: {
								backgroundColor: "var(--bai-deep)",
								color: "var(--bai-text-secondary)",
								border: "1px solid var(--bai-border)"
							}
						}),
						/* @__PURE__ */ jsx("button", {
							type: "button",
							disabled: !title.trim() || !content.trim(),
							onClick: () => dispatch(actions$1.ingestSource({
								title,
								content,
								sourceType,
								description: desc || void 0,
								author: author || void 0,
								url: url || void 0,
								createdAt: ts()
							})),
							className: "rounded-lg px-4 py-2 text-sm font-medium hover:opacity-80 disabled:opacity-40",
							style: {
								backgroundColor: "var(--bai-accent)",
								color: "var(--bai-accent-text)"
							},
							children: "Ingest Source"
						})
					]
				})
			})]
		})
	});
}
//#endregion
export { Editor as default };

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