sanity-plugin-seo-copilot 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,2706 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ seoCopilot: () => seoCopilot
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_sanity = require("sanity");
37
+
38
+ // src/inspector.ts
39
+ var import_react11 = require("react");
40
+
41
+ // src/components/SEOCopilotPanel.tsx
42
+ var import_react10 = require("react");
43
+ var import_react_dom = require("react-dom");
44
+
45
+ // src/hooks/useDocumentContent.ts
46
+ var import_react = require("react");
47
+ var import_structure = require("sanity/structure");
48
+ function extractText(blocks) {
49
+ if (!blocks || !Array.isArray(blocks)) return "";
50
+ const parts = [];
51
+ for (const block of blocks) {
52
+ if (block._type === "block" && block.children) {
53
+ const blockText = block.children.map((child) => child.text || "").join("");
54
+ if (blockText) parts.push(blockText);
55
+ } else if (typeof block === "string") {
56
+ parts.push(block);
57
+ }
58
+ }
59
+ return parts.join("\n");
60
+ }
61
+ function extractHeadings(blocks) {
62
+ if (!blocks || !Array.isArray(blocks)) return [];
63
+ const headings = [];
64
+ for (const block of blocks) {
65
+ if (block._type === "block" && block.style && /^h[1-6]$/.test(block.style) && block.children) {
66
+ const text = block.children.map((child) => child.text || "").join("");
67
+ if (text) headings.push(text);
68
+ }
69
+ }
70
+ return headings;
71
+ }
72
+ function extractImageNames(value) {
73
+ const names = [];
74
+ const imageFields = ["mainImage", "image", "heroImage", "thumbnail", "featuredImage"];
75
+ for (const field of imageFields) {
76
+ const img = value[field];
77
+ if (img) {
78
+ if (img.alt && typeof img.alt === "string") names.push(img.alt);
79
+ if (img.asset && typeof img.asset === "object") {
80
+ const asset = img.asset;
81
+ if (asset.originalFilename && typeof asset.originalFilename === "string") {
82
+ names.push(asset.originalFilename);
83
+ }
84
+ }
85
+ }
86
+ }
87
+ return names;
88
+ }
89
+ function useDocumentContent() {
90
+ const { value, ready } = (0, import_structure.useDocumentPane)();
91
+ const documentId = value?._id || void 0;
92
+ const title = value?.title;
93
+ const body = value?.body;
94
+ const [debouncedText, setDebouncedText] = (0, import_react.useState)("");
95
+ const [isLoading, setIsLoading] = (0, import_react.useState)(!ready);
96
+ const [documentFields, setDocumentFields] = (0, import_react.useState)({});
97
+ const timerRef = (0, import_react.useRef)(null);
98
+ (0, import_react.useEffect)(() => {
99
+ const bodyText = extractText(body);
100
+ const fullText = [title, bodyText].filter(Boolean).join("\n\n");
101
+ const val = value;
102
+ const fields = {
103
+ title: title || void 0,
104
+ metaDescription: val?.metaDescription || val?.summary || val?.description || val?.seoDescription,
105
+ slug: val?.slug?.current || void 0,
106
+ imageNames: val ? extractImageNames(val) : [],
107
+ headings: extractHeadings(body),
108
+ contentCategory: val?.contentCategory || val?.category,
109
+ contentPlacement: Array.isArray(val?.contentPlacement) ? val.contentPlacement : void 0
110
+ };
111
+ if (timerRef.current) clearTimeout(timerRef.current);
112
+ timerRef.current = setTimeout(() => {
113
+ setDebouncedText(fullText);
114
+ setDocumentFields(fields);
115
+ setIsLoading(false);
116
+ }, 2e3);
117
+ return () => {
118
+ if (timerRef.current) clearTimeout(timerRef.current);
119
+ };
120
+ }, [title, body, value]);
121
+ return { text: debouncedText, isLoading, documentFields, documentId };
122
+ }
123
+
124
+ // src/hooks/useKeywordAnalysis.ts
125
+ var import_react2 = require("react");
126
+
127
+ // src/lib/api.ts
128
+ function safeParseJson(text) {
129
+ try {
130
+ return JSON.parse(text);
131
+ } catch {
132
+ for (let i = text.length - 1; i >= 0; i--) {
133
+ if (text[i] === "}" || text[i] === "]") {
134
+ try {
135
+ return JSON.parse(text.substring(0, i + 1));
136
+ } catch {
137
+ continue;
138
+ }
139
+ }
140
+ }
141
+ throw new Error("Invalid JSON response from server");
142
+ }
143
+ }
144
+ async function fetchAnalysis(apiUrl, content, mode, publication, seedKeywords, documentFields, focusKeyword) {
145
+ const response = await fetch(`${apiUrl}/api/seo-copilot/analyze`, {
146
+ method: "POST",
147
+ headers: { "Content-Type": "application/json" },
148
+ body: JSON.stringify({ content, mode, publication, seedKeywords, documentFields, focusKeyword })
149
+ });
150
+ const text = await response.text();
151
+ if (!response.ok) {
152
+ const err = safeParseJson(text);
153
+ throw new Error(err.error || `HTTP ${response.status}`);
154
+ }
155
+ return safeParseJson(text);
156
+ }
157
+ async function fetchChatStream(apiUrl, messages, documentContext, publication, keywordPanelData) {
158
+ const response = await fetch(`${apiUrl}/api/seo-copilot/chat`, {
159
+ method: "POST",
160
+ headers: { "Content-Type": "application/json" },
161
+ body: JSON.stringify({ messages, documentContext, publication, keywordPanelData })
162
+ });
163
+ if (!response.ok) {
164
+ const err = await response.json().catch(() => ({ error: "Request failed" }));
165
+ throw new Error(err.error || `HTTP ${response.status}`);
166
+ }
167
+ return response.body;
168
+ }
169
+ async function submitFeedback(apiUrl, data) {
170
+ await fetch(`${apiUrl}/api/seo-copilot/feedback`, {
171
+ method: "POST",
172
+ headers: { "Content-Type": "application/json" },
173
+ body: JSON.stringify(data)
174
+ });
175
+ }
176
+ async function lookupKeywords(apiUrl, keywords) {
177
+ const response = await fetch(`${apiUrl}/api/seo-copilot/keywords`, {
178
+ method: "POST",
179
+ headers: { "Content-Type": "application/json" },
180
+ body: JSON.stringify({ keywords })
181
+ });
182
+ if (!response.ok) {
183
+ const err = await response.json().catch(() => ({ error: "Request failed" }));
184
+ throw new Error(err.error || `HTTP ${response.status}`);
185
+ }
186
+ return response.json();
187
+ }
188
+ async function loadChatHistory(apiUrl, userId, documentId) {
189
+ try {
190
+ const res = await fetch(
191
+ `${apiUrl}/api/seo-copilot/chat-history?userId=${encodeURIComponent(userId)}&documentId=${encodeURIComponent(documentId)}`
192
+ );
193
+ if (!res.ok) return [];
194
+ const data = await res.json();
195
+ return data.messages || [];
196
+ } catch {
197
+ return [];
198
+ }
199
+ }
200
+ async function saveChatHistory(apiUrl, userId, documentId, messages, publication) {
201
+ try {
202
+ await fetch(`${apiUrl}/api/seo-copilot/chat-history`, {
203
+ method: "POST",
204
+ headers: { "Content-Type": "application/json" },
205
+ body: JSON.stringify({ userId, documentId, messages, publication })
206
+ });
207
+ } catch {
208
+ }
209
+ }
210
+
211
+ // src/hooks/useKeywordAnalysis.ts
212
+ function mergeVolumes(analysis, volumeMap) {
213
+ const updated = { ...analysis };
214
+ if (updated.primaryKeywordCandidates && updated.primaryKeywordCandidates.length > 0) {
215
+ updated.primaryKeywordCandidates = updated.primaryKeywordCandidates.map((candidate) => {
216
+ const data = volumeMap.get(candidate.term.toLowerCase());
217
+ return data ? { ...candidate, volume: data.volume, cpc: data.cpc, competition: data.competition } : candidate;
218
+ });
219
+ }
220
+ const primaryData = volumeMap.get(updated.primaryKeyword.term.toLowerCase());
221
+ if (primaryData) {
222
+ updated.primaryKeyword = {
223
+ ...updated.primaryKeyword,
224
+ volume: primaryData.volume,
225
+ cpc: primaryData.cpc,
226
+ competition: primaryData.competition
227
+ };
228
+ }
229
+ updated.supportingKeywords = updated.supportingKeywords.map((kw) => {
230
+ const data = volumeMap.get(kw.term.toLowerCase());
231
+ return data ? { ...kw, volume: data.volume } : kw;
232
+ });
233
+ updated.missingKeywords = updated.missingKeywords.map((kw) => {
234
+ const data = volumeMap.get(kw.term.toLowerCase());
235
+ return data ? { ...kw, volume: data.volume } : kw;
236
+ });
237
+ return updated;
238
+ }
239
+ function useKeywordAnalysis(text, apiUrl, publication, seedKeywords, documentFields, focusPrimary, documentId) {
240
+ const cacheKey = documentId ? `seo-copilot-cache-${documentId}` : "seo-copilot-cache-v2";
241
+ const loadCached = () => {
242
+ try {
243
+ if (typeof window !== "undefined" && sessionStorage.getItem("seo-copilot-cache")) {
244
+ sessionStorage.removeItem("seo-copilot-cache");
245
+ }
246
+ const raw = typeof window !== "undefined" ? sessionStorage.getItem(cacheKey) : null;
247
+ if (raw) return JSON.parse(raw);
248
+ } catch {
249
+ }
250
+ return { analysis: null, deepAnalysis: null, seeds: [] };
251
+ };
252
+ const cached = (0, import_react2.useRef)(loadCached());
253
+ const [analysis, setAnalysisRaw] = (0, import_react2.useState)(cached.current.analysis);
254
+ const [isLoading, setIsLoading] = (0, import_react2.useState)(false);
255
+ const [isVolumesLoading, setIsVolumesLoading] = (0, import_react2.useState)(false);
256
+ const [error, setError] = (0, import_react2.useState)(null);
257
+ const [deepAnalysis, setDeepAnalysisRaw] = (0, import_react2.useState)(cached.current.deepAnalysis);
258
+ const [isDeepLoading, setIsDeepLoading] = (0, import_react2.useState)(false);
259
+ const [aeoTextSnapshot, setAeoTextSnapshot] = (0, import_react2.useState)("");
260
+ const [aeoContentChanged, setAeoContentChanged] = (0, import_react2.useState)(false);
261
+ const setAnalysis = (0, import_react2.useCallback)((val) => {
262
+ setAnalysisRaw(val);
263
+ try {
264
+ const prev = loadCached();
265
+ sessionStorage.setItem(cacheKey, JSON.stringify({ ...prev, analysis: val }));
266
+ } catch {
267
+ }
268
+ }, [cacheKey]);
269
+ const setDeepAnalysis = (0, import_react2.useCallback)((val) => {
270
+ setDeepAnalysisRaw(val);
271
+ try {
272
+ const prev = loadCached();
273
+ sessionStorage.setItem(cacheKey, JSON.stringify({ ...prev, deepAnalysis: val }));
274
+ } catch {
275
+ }
276
+ }, [cacheKey]);
277
+ const effectiveSeeds = seedKeywords;
278
+ (0, import_react2.useEffect)(() => {
279
+ if (deepAnalysis && aeoTextSnapshot && text !== aeoTextSnapshot) {
280
+ setAeoContentChanged(true);
281
+ }
282
+ }, [text, deepAnalysis, aeoTextSnapshot]);
283
+ const volumesLoadedRef = (0, import_react2.useRef)(false);
284
+ const lastAnalysisRef = (0, import_react2.useRef)(null);
285
+ const prevSeedsRef = (0, import_react2.useRef)(effectiveSeeds?.join(",") || "");
286
+ const prevFocusRef = (0, import_react2.useRef)(focusPrimary || "");
287
+ const currentSeedsKey = effectiveSeeds?.join(",") || "";
288
+ const currentFocusKey = focusPrimary || "";
289
+ if (currentSeedsKey !== prevSeedsRef.current || currentFocusKey !== prevFocusRef.current) {
290
+ volumesLoadedRef.current = false;
291
+ prevSeedsRef.current = currentSeedsKey;
292
+ prevFocusRef.current = currentFocusKey;
293
+ }
294
+ (0, import_react2.useEffect)(() => {
295
+ if ((!text || text.trim().length < 20) && (!effectiveSeeds || effectiveSeeds.length === 0)) {
296
+ setAnalysis(null);
297
+ volumesLoadedRef.current = false;
298
+ return;
299
+ }
300
+ let cancelled = false;
301
+ setIsLoading(true);
302
+ setError(null);
303
+ fetchAnalysis(apiUrl, text, "realtime", publication, effectiveSeeds, documentFields, focusPrimary).then((result) => {
304
+ if (cancelled) return;
305
+ const analysisResult = result;
306
+ lastAnalysisRef.current = analysisResult;
307
+ setAnalysis(analysisResult);
308
+ setIsLoading(false);
309
+ if (!volumesLoadedRef.current) {
310
+ volumesLoadedRef.current = true;
311
+ doVolumeLookup(analysisResult, cancelled);
312
+ }
313
+ }).catch((err) => {
314
+ if (!cancelled) {
315
+ setError(err.message);
316
+ setIsLoading(false);
317
+ }
318
+ });
319
+ return () => {
320
+ cancelled = true;
321
+ };
322
+ }, [text, apiUrl, publication, effectiveSeeds?.join(","), focusPrimary]);
323
+ const doVolumeLookup = (0, import_react2.useCallback)((analysisResult, cancelled) => {
324
+ setIsVolumesLoading(true);
325
+ const candidateTerms = (analysisResult.primaryKeywordCandidates || []).map((c) => c.term);
326
+ const allTerms = [
327
+ .../* @__PURE__ */ new Set([
328
+ ...candidateTerms,
329
+ analysisResult.primaryKeyword.term,
330
+ ...(analysisResult.supportingKeywords || []).map((kw) => kw.term),
331
+ ...analysisResult.missingKeywords.map((kw) => kw.term)
332
+ ])
333
+ ];
334
+ lookupKeywords(apiUrl, allTerms).then(({ results }) => {
335
+ if (cancelled) return;
336
+ const volumeMap = new Map(
337
+ results.map((r) => [r.keyword.toLowerCase(), r])
338
+ );
339
+ setAnalysis(mergeVolumes(analysisResult, volumeMap));
340
+ setIsVolumesLoading(false);
341
+ }).catch(() => {
342
+ if (!cancelled) {
343
+ setIsVolumesLoading(false);
344
+ }
345
+ });
346
+ }, [apiUrl]);
347
+ const refreshVolumes = (0, import_react2.useCallback)(() => {
348
+ const current = lastAnalysisRef.current || analysis;
349
+ if (!current) return;
350
+ doVolumeLookup(current);
351
+ }, [analysis, doVolumeLookup]);
352
+ const runDeepAnalysis = (0, import_react2.useCallback)(() => {
353
+ if ((!text || text.trim().length < 20) && (!effectiveSeeds || effectiveSeeds.length === 0)) return;
354
+ setIsDeepLoading(true);
355
+ setError(null);
356
+ setAeoContentChanged(false);
357
+ setAeoTextSnapshot(text);
358
+ fetchAnalysis(apiUrl, text, "deep", publication, effectiveSeeds, documentFields, focusPrimary).then((result) => {
359
+ setDeepAnalysis(result);
360
+ setIsDeepLoading(false);
361
+ }).catch((err) => {
362
+ setError(err.message);
363
+ setIsDeepLoading(false);
364
+ });
365
+ }, [text, apiUrl, publication, effectiveSeeds?.join(","), documentFields]);
366
+ return {
367
+ analysis,
368
+ isLoading,
369
+ isVolumesLoading,
370
+ error,
371
+ deepAnalysis,
372
+ isDeepLoading,
373
+ runDeepAnalysis,
374
+ refreshVolumes,
375
+ aeoContentChanged
376
+ };
377
+ }
378
+
379
+ // src/components/ConfigContext.tsx
380
+ var import_react3 = require("react");
381
+ var import_jsx_runtime = require("react/jsx-runtime");
382
+ var ConfigContext = (0, import_react3.createContext)({
383
+ apiUrl: "http://localhost:3000"
384
+ });
385
+ function ConfigProvider({
386
+ config,
387
+ children
388
+ }) {
389
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ConfigContext.Provider, { value: config, children });
390
+ }
391
+ function useConfig() {
392
+ return (0, import_react3.useContext)(ConfigContext);
393
+ }
394
+
395
+ // src/components/KeywordPanel.tsx
396
+ var import_react7 = require("react");
397
+
398
+ // src/components/PlacementIndicators.tsx
399
+ var import_react4 = require("react");
400
+ var import_jsx_runtime2 = require("react/jsx-runtime");
401
+ var indicators = [
402
+ { key: "inBody", letter: "B", tooltip: "Keyword in body" },
403
+ { key: "inTitle", letter: "T", tooltip: "Keyword in title" },
404
+ { key: "inMetaDescription", letter: "D", tooltip: "Keyword in meta description" },
405
+ { key: "inImages", letter: "I", tooltip: "Keyword in image names" },
406
+ { key: "inUrl", letter: "U", tooltip: "Keyword in URL" }
407
+ ];
408
+ function IndicatorDot({ letter, active, tooltip }) {
409
+ const [hovered, setHovered] = (0, import_react4.useState)(false);
410
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
411
+ "span",
412
+ {
413
+ onMouseEnter: () => setHovered(true),
414
+ onMouseLeave: () => setHovered(false),
415
+ style: {
416
+ position: "relative",
417
+ display: "inline-flex",
418
+ alignItems: "center",
419
+ justifyContent: "center",
420
+ width: 16,
421
+ height: 16,
422
+ borderRadius: "50%",
423
+ fontSize: 8,
424
+ fontWeight: 700,
425
+ lineHeight: 1,
426
+ cursor: "default",
427
+ flexShrink: 0,
428
+ background: active ? "#16a34a" : "transparent",
429
+ color: active ? "#ffffff" : "#d1d5db",
430
+ border: active ? "none" : "1.5px solid #d1d5db",
431
+ transition: "all 150ms"
432
+ },
433
+ children: [
434
+ letter,
435
+ hovered && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: {
436
+ position: "absolute",
437
+ bottom: "100%",
438
+ left: "50%",
439
+ transform: "translateX(-50%)",
440
+ marginBottom: 4,
441
+ padding: "2px 6px",
442
+ borderRadius: 4,
443
+ background: "#1f2937",
444
+ color: "#ffffff",
445
+ fontSize: 10,
446
+ whiteSpace: "nowrap",
447
+ pointerEvents: "none",
448
+ zIndex: 10
449
+ }, children: tooltip })
450
+ ]
451
+ }
452
+ );
453
+ }
454
+ function PlacementIndicators({ placement }) {
455
+ if (!placement) return null;
456
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", alignItems: "center", gap: 2 }, children: indicators.map(({ key, letter, tooltip }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(IndicatorDot, { letter, active: placement[key], tooltip }, key)) });
457
+ }
458
+
459
+ // src/components/SeedKeywordInput.tsx
460
+ var import_react5 = require("react");
461
+
462
+ // src/components/styles.ts
463
+ var INJECTED_CSS = `
464
+ @keyframes bar-shift-1 {
465
+ 0%, 100% { height: 8px; }
466
+ 25% { height: 14px; }
467
+ 50% { height: 6px; }
468
+ 75% { height: 12px; }
469
+ }
470
+ @keyframes bar-shift-2 {
471
+ 0%, 100% { height: 12px; }
472
+ 25% { height: 6px; }
473
+ 50% { height: 14px; }
474
+ 75% { height: 8px; }
475
+ }
476
+ @keyframes bar-shift-3 {
477
+ 0%, 100% { height: 6px; }
478
+ 25% { height: 10px; }
479
+ 50% { height: 12px; }
480
+ 75% { height: 5px; }
481
+ }
482
+ @keyframes fade-up {
483
+ from { opacity: 0; transform: translateY(6px); }
484
+ to { opacity: 1; transform: translateY(0); }
485
+ }
486
+ @keyframes spin {
487
+ from { transform: rotate(0deg); }
488
+ to { transform: rotate(360deg); }
489
+ }
490
+ @keyframes content-pulse {
491
+ 0%, 100% { opacity: 0.5; }
492
+ 50% { opacity: 1; }
493
+ }
494
+ .prose-chat { font-size: 12.5px; line-height: 1.65; color: #475569; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif; }
495
+ .prose-chat h1 { font-size: 14px; font-weight: 700; color: #1e293b; margin: 10px 0 5px 0; line-height: 1.35; }
496
+ .prose-chat h2, .prose-chat h3 { font-size: 13px; font-weight: 700; color: #1e293b; margin: 10px 0 5px 0; line-height: 1.35; }
497
+ .prose-chat p { margin: 5px 0; }
498
+ .prose-chat ul, .prose-chat ol { margin: 5px 0; padding-left: 18px; }
499
+ .prose-chat li { margin: 3px 0; }
500
+ .prose-chat strong { font-weight: 600; color: #1e293b; }
501
+ .prose-chat hr { border: none; border-top: 1px solid #f1f5f9; margin: 10px 0; }
502
+ .prose-chat code { background: #f8fafc; padding: 2px 5px; border-radius: 4px; font-size: 11px; border: 1px solid #f1f5f9; }
503
+ .prose-chat a { color: #005DAC; text-decoration: none; border-bottom: 1px solid rgba(0,93,172,0.3); }
504
+ .prose-chat a:hover { border-bottom-color: #005DAC; }
505
+ .prose-chat table { width: 100%; border-collapse: separate; border-spacing: 0; margin: 8px 0; font-size: 11px; border-radius: 8px; overflow: hidden; border: 1px solid #f1f5f9; }
506
+ .prose-chat th { text-align: left; padding: 6px 10px; font-weight: 600; color: #475569; border-bottom: 1px solid #f1f5f9; background: #fafbfc; }
507
+ .prose-chat td { padding: 6px 10px; border-bottom: 1px solid #f8fafc; color: #64748b; }
508
+ .prose-chat tr:last-child td { border-bottom: none; }
509
+
510
+ /* Thin scrollbars */
511
+ .seo-copilot-panel { scrollbar-width: thin; scrollbar-color: #e5e7eb transparent; }
512
+ .seo-copilot-panel ::-webkit-scrollbar { width: 4px; }
513
+ .seo-copilot-panel ::-webkit-scrollbar-track { background: transparent; }
514
+ .seo-copilot-panel ::-webkit-scrollbar-thumb { background: #e5e7eb; border-radius: 4px; }
515
+ .seo-copilot-panel * { scrollbar-width: thin; scrollbar-color: #e5e7eb transparent; }
516
+
517
+ /* Dark mode overrides - Sanity adds data-scheme="dark" or [data-ui-color-scheme="dark"] */
518
+ @media (prefers-color-scheme: dark) {
519
+ .seo-copilot-panel { color-scheme: light; }
520
+ }
521
+ /* Force light text colors when Sanity is in dark mode - the panel bg stays white but text was washing out */
522
+ [data-scheme="dark"] .seo-copilot-panel,
523
+ [data-ui-color-scheme="dark"] .seo-copilot-panel {
524
+ color: #374151;
525
+ }
526
+ [data-scheme="dark"] .seo-copilot-panel *,
527
+ [data-ui-color-scheme="dark"] .seo-copilot-panel * {
528
+ color-scheme: light;
529
+ }
530
+ `;
531
+ var MJH_GOLD = "#E6C01B";
532
+ var MJH_BLUE = "#005DAC";
533
+ var MJH_SLATE = "#4D596A";
534
+ var USER_BLUE = "#DFF4FD";
535
+ var USER_BLUE_BORDER = "#B8E3F9";
536
+
537
+ // src/components/SeedKeywordInput.tsx
538
+ var import_jsx_runtime3 = require("react/jsx-runtime");
539
+ function parseSeedInput(input) {
540
+ return input.split(/[,\n]+/).map((s) => s.trim()).filter((s) => s.length > 0);
541
+ }
542
+ function SeedKeywordInput({ seeds, onSeedsChange }) {
543
+ const [inputValue, setInputValue] = (0, import_react5.useState)("");
544
+ const inputRef = (0, import_react5.useRef)(null);
545
+ const handleAdd = () => {
546
+ const parsed = parseSeedInput(inputValue);
547
+ if (parsed.length > 0) {
548
+ const combined = [.../* @__PURE__ */ new Set([...seeds, ...parsed])];
549
+ onSeedsChange(combined);
550
+ setInputValue("");
551
+ }
552
+ };
553
+ const handleRemove = (term) => {
554
+ onSeedsChange(seeds.filter((s) => s !== term));
555
+ };
556
+ const handleKeyDown = (e) => {
557
+ if (e.key === "Enter" && !e.shiftKey) {
558
+ e.preventDefault();
559
+ handleAdd();
560
+ }
561
+ };
562
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: {
563
+ borderRadius: 14,
564
+ background: "#ffffff",
565
+ boxShadow: "0 1px 3px rgba(0,0,0,0.04), 0 0 0 1px rgba(0,0,0,0.03)",
566
+ padding: "12px 14px"
567
+ }, children: [
568
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontSize: 10.5, textTransform: "uppercase", letterSpacing: "0.06em", color: "#94a3b8", fontWeight: 600 }, children: "Seed Keywords" }),
569
+ seeds.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { display: "flex", flexWrap: "wrap", gap: 5, marginTop: 8 }, children: seeds.map((seed) => /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { style: {
570
+ display: "inline-flex",
571
+ alignItems: "center",
572
+ gap: 5,
573
+ padding: "3px 10px",
574
+ borderRadius: 99,
575
+ fontSize: 11.5,
576
+ fontWeight: 500,
577
+ background: `rgba(230,192,27,0.12)`,
578
+ color: "#92810F",
579
+ transition: "all 150ms"
580
+ }, children: [
581
+ seed,
582
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
583
+ "button",
584
+ {
585
+ onClick: () => handleRemove(seed),
586
+ style: { background: "none", border: "none", cursor: "pointer", padding: 0, fontSize: 12, color: "#8B7310", lineHeight: 1 },
587
+ children: "x"
588
+ }
589
+ )
590
+ ] }, seed)) }),
591
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: { marginTop: 8 }, children: [
592
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
593
+ "textarea",
594
+ {
595
+ ref: inputRef,
596
+ value: inputValue,
597
+ onChange: (e) => setInputValue(e.target.value),
598
+ onKeyDown: handleKeyDown,
599
+ placeholder: "Enter 2-3 word phrases for best results",
600
+ rows: 2,
601
+ style: {
602
+ width: "100%",
603
+ padding: "8px 10px",
604
+ fontSize: 12.5,
605
+ borderRadius: 10,
606
+ border: "1px solid #e2e8f0",
607
+ background: "#fafbfc",
608
+ resize: "vertical",
609
+ outline: "none",
610
+ fontFamily: "inherit",
611
+ boxSizing: "border-box"
612
+ }
613
+ }
614
+ ),
615
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
616
+ "button",
617
+ {
618
+ onClick: handleAdd,
619
+ disabled: !inputValue.trim(),
620
+ style: {
621
+ marginTop: 4,
622
+ padding: "4px 12px",
623
+ fontSize: 11,
624
+ fontWeight: 600,
625
+ borderRadius: 6,
626
+ border: "none",
627
+ background: inputValue.trim() ? MJH_GOLD : "#e5e7eb",
628
+ color: inputValue.trim() ? "#ffffff" : "#9ca3af",
629
+ cursor: inputValue.trim() ? "pointer" : "not-allowed",
630
+ transition: "all 150ms"
631
+ },
632
+ children: "Add"
633
+ }
634
+ ),
635
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { marginTop: 4, fontSize: 10, color: "#9ca3af" }, children: "Tip: Long-tail keywords (2-3+ words) give better results than single broad terms" })
636
+ ] })
637
+ ] });
638
+ }
639
+
640
+ // src/components/FeedbackButton.tsx
641
+ var import_react6 = require("react");
642
+ var import_jsx_runtime4 = require("react/jsx-runtime");
643
+ function FeedbackButton({ suggestionType, suggestionText, documentId, publication }) {
644
+ const { apiUrl } = useConfig();
645
+ const [vote, setVote] = (0, import_react6.useState)(null);
646
+ const [showReasonPopup, setShowReasonPopup] = (0, import_react6.useState)(false);
647
+ const [reason, setReason] = (0, import_react6.useState)("");
648
+ const [submitted, setSubmitted] = (0, import_react6.useState)(false);
649
+ const popupRef = (0, import_react6.useRef)(null);
650
+ const inputRef = (0, import_react6.useRef)(null);
651
+ (0, import_react6.useEffect)(() => {
652
+ if (!showReasonPopup) return;
653
+ const handler = (e) => {
654
+ if (popupRef.current && !popupRef.current.contains(e.target)) {
655
+ setShowReasonPopup(false);
656
+ }
657
+ };
658
+ document.addEventListener("mousedown", handler);
659
+ return () => document.removeEventListener("mousedown", handler);
660
+ }, [showReasonPopup]);
661
+ (0, import_react6.useEffect)(() => {
662
+ if (showReasonPopup) {
663
+ setTimeout(() => inputRef.current?.focus(), 50);
664
+ }
665
+ }, [showReasonPopup]);
666
+ const handleVote = async (v) => {
667
+ if (v === "down" && vote !== "down") {
668
+ setVote("down");
669
+ setShowReasonPopup(true);
670
+ setSubmitted(false);
671
+ try {
672
+ await submitFeedback(apiUrl, {
673
+ suggestion_type: suggestionType,
674
+ suggestion_text: suggestionText,
675
+ vote: "down",
676
+ document_id: documentId,
677
+ publication
678
+ });
679
+ } catch {
680
+ }
681
+ return;
682
+ }
683
+ const newVote = vote === v ? null : v;
684
+ setVote(newVote);
685
+ setShowReasonPopup(false);
686
+ if (newVote) {
687
+ try {
688
+ await submitFeedback(apiUrl, {
689
+ suggestion_type: suggestionType,
690
+ suggestion_text: suggestionText,
691
+ vote: newVote,
692
+ document_id: documentId,
693
+ publication
694
+ });
695
+ } catch {
696
+ }
697
+ }
698
+ };
699
+ const handleSubmitReason = async () => {
700
+ const trimmed = reason.trim();
701
+ if (trimmed) {
702
+ try {
703
+ await submitFeedback(apiUrl, {
704
+ suggestion_type: suggestionType + "_reason",
705
+ suggestion_text: suggestionText + " | REASON: " + trimmed,
706
+ vote: "down",
707
+ document_id: documentId,
708
+ publication
709
+ });
710
+ } catch {
711
+ }
712
+ }
713
+ setSubmitted(true);
714
+ setTimeout(() => {
715
+ setShowReasonPopup(false);
716
+ setReason("");
717
+ }, 1e3);
718
+ };
719
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 2, flexShrink: 0, position: "relative" }, children: [
720
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
721
+ "button",
722
+ {
723
+ onClick: () => handleVote("up"),
724
+ style: {
725
+ background: "none",
726
+ border: "none",
727
+ cursor: "pointer",
728
+ padding: 1,
729
+ fontSize: 12,
730
+ color: vote === "up" ? "#16a34a" : "#d1d5db",
731
+ transition: "color 150ms",
732
+ lineHeight: 1,
733
+ display: "flex"
734
+ },
735
+ title: "Good suggestion",
736
+ children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("path", { d: "M1 8.25a1.25 1.25 0 112.5 0v7.5a1.25 1.25 0 11-2.5 0v-7.5zM11 3V1.7c0-.268.14-.526.395-.607A2 2 0 0114 3c0 .995-.182 1.948-.514 2.826-.204.54.166 1.174.744 1.174h2.52c1.243 0 2.261 1.01 2.146 2.247a23.864 23.864 0 01-1.341 5.974 1.637 1.637 0 01-1.555 1.029H6.5V7.5L11 3z" }) })
737
+ }
738
+ ),
739
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
740
+ "button",
741
+ {
742
+ onClick: () => handleVote("down"),
743
+ style: {
744
+ background: "none",
745
+ border: "none",
746
+ cursor: "pointer",
747
+ padding: 1,
748
+ fontSize: 12,
749
+ color: vote === "down" ? "#dc2626" : "#d1d5db",
750
+ transition: "color 150ms",
751
+ lineHeight: 1,
752
+ display: "flex"
753
+ },
754
+ title: "Not helpful",
755
+ children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("path", { d: "M19 11.75a1.25 1.25 0 11-2.5 0v-7.5a1.25 1.25 0 112.5 0v7.5zM9 17v1.3c0 .268-.14.526-.395.607A2 2 0 016 17c0-.995.182-1.948.514-2.826.204-.54-.166-1.174-.744-1.174H3.25c-1.243 0-2.261-1.01-2.146-2.247a23.864 23.864 0 011.341-5.974A1.637 1.637 0 014 3.75h9.5V12.5L9 17z" }) })
756
+ }
757
+ ),
758
+ showReasonPopup && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
759
+ "div",
760
+ {
761
+ ref: popupRef,
762
+ style: {
763
+ position: "absolute",
764
+ top: "100%",
765
+ right: 0,
766
+ marginTop: 4,
767
+ width: 220,
768
+ padding: 10,
769
+ borderRadius: 10,
770
+ background: "#ffffff",
771
+ boxShadow: "0 4px 16px rgba(0,0,0,0.15), 0 1px 4px rgba(0,0,0,0.1)",
772
+ zIndex: 100,
773
+ border: "1px solid #e5e7eb"
774
+ },
775
+ children: submitted ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { fontSize: 11, color: "#16a34a", textAlign: "center", padding: "4px 0" }, children: "Thanks for the feedback!" }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
776
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { fontSize: 11, fontWeight: 600, color: "#374151", marginBottom: 6 }, children: "Sorry! What went wrong?" }),
777
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
778
+ "input",
779
+ {
780
+ ref: inputRef,
781
+ type: "text",
782
+ value: reason,
783
+ onChange: (e) => setReason(e.target.value),
784
+ onKeyDown: (e) => {
785
+ if (e.key === "Enter") handleSubmitReason();
786
+ },
787
+ placeholder: "Tell us what to improve...",
788
+ style: {
789
+ width: "100%",
790
+ padding: "5px 8px",
791
+ fontSize: 11,
792
+ borderRadius: 6,
793
+ border: "1px solid #e5e7eb",
794
+ outline: "none",
795
+ fontFamily: "inherit",
796
+ boxSizing: "border-box"
797
+ }
798
+ }
799
+ ),
800
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "flex", justifyContent: "flex-end", marginTop: 6, gap: 4 }, children: [
801
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
802
+ "button",
803
+ {
804
+ onClick: () => {
805
+ setShowReasonPopup(false);
806
+ setReason("");
807
+ },
808
+ style: {
809
+ padding: "3px 8px",
810
+ fontSize: 10,
811
+ borderRadius: 4,
812
+ border: "1px solid #e5e7eb",
813
+ background: "#ffffff",
814
+ color: "#6b7280",
815
+ cursor: "pointer"
816
+ },
817
+ children: "Skip"
818
+ }
819
+ ),
820
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
821
+ "button",
822
+ {
823
+ onClick: handleSubmitReason,
824
+ style: {
825
+ padding: "3px 8px",
826
+ fontSize: 10,
827
+ borderRadius: 4,
828
+ border: "none",
829
+ background: "#dc2626",
830
+ color: "#ffffff",
831
+ cursor: "pointer",
832
+ fontWeight: 600
833
+ },
834
+ children: "Submit"
835
+ }
836
+ )
837
+ ] })
838
+ ] })
839
+ }
840
+ )
841
+ ] });
842
+ }
843
+
844
+ // src/components/KeywordPanel.tsx
845
+ var import_jsx_runtime5 = require("react/jsx-runtime");
846
+ var AEO_PURPLE = "#7C3AED";
847
+ function SectionHeader({ label, iconColor, actionButton }) {
848
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 7, marginBottom: 10 }, children: [
849
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { width: 7, height: 7, borderRadius: "50%", background: iconColor, flexShrink: 0 } }),
850
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 10.5, textTransform: "uppercase", letterSpacing: "0.06em", color: "#94a3b8", fontWeight: 600, flex: 1 }, children: label }),
851
+ actionButton
852
+ ] });
853
+ }
854
+ var cardStyle = {
855
+ borderRadius: 14,
856
+ background: "#ffffff",
857
+ boxShadow: "0 1px 3px rgba(0,0,0,0.04), 0 0 0 1px rgba(0,0,0,0.03)",
858
+ padding: "14px"
859
+ };
860
+ function abbrevVol(v) {
861
+ if (v == null) return "";
862
+ if (v === 0) return "N/A";
863
+ if (v >= 1e4) return Math.round(v / 1e3) + "K";
864
+ if (v >= 1e3) return (v / 1e3).toFixed(1) + "K";
865
+ return String(v);
866
+ }
867
+ function placementCount(p) {
868
+ if (!p) return 0;
869
+ return [p.inBody, p.inTitle, p.inMetaDescription, p.inImages, p.inUrl].filter(Boolean).length;
870
+ }
871
+ function buildSuggestedKeywords(supporting, missing, documentText) {
872
+ const merged = [];
873
+ const lowerText = documentText.toLowerCase();
874
+ for (const kw of supporting) {
875
+ const actuallyInContent = lowerText.includes(kw.term.toLowerCase());
876
+ merged.push({ term: kw.term, volume: kw.volume ?? 0, inContent: actuallyInContent, placement: kw.placement });
877
+ }
878
+ for (const kw of missing) {
879
+ const actuallyInContent = lowerText.includes(kw.term.toLowerCase());
880
+ merged.push({ term: kw.term, volume: kw.volume ?? 0, inContent: actuallyInContent, reason: kw.reason, placement: kw.placement });
881
+ }
882
+ merged.sort((a, b) => {
883
+ const pDiff = placementCount(b.placement) - placementCount(a.placement);
884
+ if (pDiff !== 0) return pDiff;
885
+ return b.volume - a.volume;
886
+ });
887
+ return merged;
888
+ }
889
+ function LightbulbButton({ onClick, isLoading }) {
890
+ const [hovered, setHovered] = (0, import_react7.useState)(false);
891
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
892
+ "span",
893
+ {
894
+ onClick: (e) => {
895
+ e.stopPropagation();
896
+ onClick();
897
+ },
898
+ onMouseEnter: () => setHovered(true),
899
+ onMouseLeave: () => setHovered(false),
900
+ style: {
901
+ position: "relative",
902
+ cursor: isLoading ? "wait" : "pointer",
903
+ fontSize: 14,
904
+ lineHeight: 1,
905
+ opacity: isLoading ? 0.5 : 1,
906
+ flexShrink: 0,
907
+ display: "inline-flex",
908
+ alignItems: "center"
909
+ },
910
+ title: "How to use this keyword",
911
+ children: [
912
+ isLoading ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: MJH_GOLD }) : "\u{1F4A1}",
913
+ hovered && !isLoading && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: {
914
+ position: "absolute",
915
+ bottom: "100%",
916
+ left: "50%",
917
+ transform: "translateX(-50%)",
918
+ marginBottom: 4,
919
+ padding: "2px 6px",
920
+ borderRadius: 4,
921
+ background: "#1f2937",
922
+ color: "#ffffff",
923
+ fontSize: 10,
924
+ whiteSpace: "nowrap",
925
+ pointerEvents: "none",
926
+ zIndex: 10
927
+ }, children: "How to use" })
928
+ ]
929
+ }
930
+ );
931
+ }
932
+ function HighlightKeyword({ text, keyword }) {
933
+ if (!keyword || !text) return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: text });
934
+ const regex = new RegExp(`(${keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})`, "gi");
935
+ const parts = text.split(regex);
936
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: parts.map(
937
+ (part, i) => regex.test(part) ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { background: "#FEF08A", fontWeight: 600, borderRadius: 2, padding: "0 1px" }, children: part }, i) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { children: part }, i)
938
+ ) });
939
+ }
940
+ function KeywordHelpDisplay({ suggestions, onRegenerate, isRegenerating, term, publication }) {
941
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { paddingLeft: 8, paddingTop: 4, paddingBottom: 4, display: "flex", flexDirection: "column", gap: 6 }, children: [
942
+ suggestions.map((s, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 11, lineHeight: 1.5, padding: "6px 8px", background: "rgba(0,93,172,0.05)", borderRadius: 6, borderLeft: `2px solid ${MJH_BLUE}` }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "flex-start", justifyContent: "space-between" }, children: [
943
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { flex: 1 }, children: s.type === "change" ? /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
944
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { color: "#9ca3af", fontWeight: 600, fontSize: 10, textTransform: "uppercase", marginBottom: 2 }, children: "Change" }),
945
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { color: "#991b1b", textDecoration: "line-through", marginBottom: 2 }, children: s.original }),
946
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { color: "#166534" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(HighlightKeyword, { text: s.revised || "", keyword: term }) })
947
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
948
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { color: "#9ca3af", fontWeight: 600, fontSize: 10, textTransform: "uppercase", marginBottom: 2 }, children: [
949
+ "Add",
950
+ s.where ? ` - ${s.where}` : ""
951
+ ] }),
952
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { color: "#374151" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(HighlightKeyword, { text: s.sentence || "", keyword: term }) })
953
+ ] }) }),
954
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
955
+ FeedbackButton,
956
+ {
957
+ suggestionType: "keyword_help",
958
+ suggestionText: `${term}: ${s.type === "change" ? s.revised : s.sentence}`,
959
+ publication
960
+ }
961
+ )
962
+ ] }) }, i)),
963
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
964
+ "button",
965
+ {
966
+ onClick: onRegenerate,
967
+ disabled: isRegenerating,
968
+ style: {
969
+ display: "flex",
970
+ alignItems: "center",
971
+ gap: 4,
972
+ padding: "4px 8px",
973
+ fontSize: 10,
974
+ color: isRegenerating ? "#9ca3af" : MJH_BLUE,
975
+ background: "none",
976
+ border: "none",
977
+ cursor: isRegenerating ? "wait" : "pointer",
978
+ alignSelf: "flex-start",
979
+ transition: "color 150ms"
980
+ },
981
+ title: "Regenerate suggestions",
982
+ children: [
983
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", style: { animation: isRegenerating ? "spin 1s linear infinite" : "none" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { fillRule: "evenodd", d: "M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H4.598a.75.75 0 00-.75.75v3.634a.75.75 0 001.5 0v-2.033l.312.311a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm-10.624-2.85a5.5 5.5 0 019.201-2.466l.312.311h-2.433a.75.75 0 000 1.5h3.634a.75.75 0 00.75-.75V3.535a.75.75 0 00-1.5 0v2.033l-.312-.311A7 7 0 002.628 8.395a.75.75 0 001.449.39z", clipRule: "evenodd" }) }),
984
+ isRegenerating ? "Regenerating..." : "Regenerate"
985
+ ]
986
+ }
987
+ )
988
+ ] });
989
+ }
990
+ function PencilButton({ onClick, active }) {
991
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
992
+ "button",
993
+ {
994
+ onClick,
995
+ style: {
996
+ background: "none",
997
+ border: "none",
998
+ cursor: "pointer",
999
+ padding: 2,
1000
+ display: "flex",
1001
+ alignItems: "center",
1002
+ color: active ? MJH_GOLD : "#9ca3af",
1003
+ transition: "color 150ms",
1004
+ flexShrink: 0
1005
+ },
1006
+ title: "Add custom keyword",
1007
+ children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { d: "M2.695 14.763l-1.262 3.154a.5.5 0 00.65.65l3.155-1.262a4 4 0 001.343-.885L17.5 5.5a2.121 2.121 0 00-3-3L3.58 13.42a4 4 0 00-.885 1.343z" }) })
1008
+ }
1009
+ );
1010
+ }
1011
+ function PrimaryKeywordSelector({
1012
+ candidates,
1013
+ selectedTerm,
1014
+ onSelect,
1015
+ publication,
1016
+ documentId
1017
+ }) {
1018
+ const [customInputOpen, setCustomInputOpen] = (0, import_react7.useState)(false);
1019
+ const [customValue, setCustomValue] = (0, import_react7.useState)("");
1020
+ const handleCustomSubmit = () => {
1021
+ const trimmed = customValue.trim();
1022
+ if (trimmed) {
1023
+ onSelect(trimmed);
1024
+ setCustomValue("");
1025
+ setCustomInputOpen(false);
1026
+ }
1027
+ };
1028
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: cardStyle, children: [
1029
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1030
+ SectionHeader,
1031
+ {
1032
+ label: "Primary Keyword",
1033
+ iconColor: "#16a34a",
1034
+ actionButton: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PencilButton, { onClick: () => setCustomInputOpen(!customInputOpen), active: customInputOpen })
1035
+ }
1036
+ ),
1037
+ customInputOpen && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { marginBottom: 8, display: "flex", gap: 6 }, children: [
1038
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1039
+ "input",
1040
+ {
1041
+ type: "text",
1042
+ value: customValue,
1043
+ onChange: (e) => setCustomValue(e.target.value),
1044
+ onKeyDown: (e) => {
1045
+ if (e.key === "Enter") handleCustomSubmit();
1046
+ },
1047
+ placeholder: "Type a custom primary keyword",
1048
+ autoFocus: true,
1049
+ style: {
1050
+ flex: 1,
1051
+ padding: "5px 8px",
1052
+ fontSize: 12,
1053
+ borderRadius: 8,
1054
+ border: "1px solid #e5e7eb",
1055
+ outline: "none",
1056
+ fontFamily: "inherit"
1057
+ }
1058
+ }
1059
+ ),
1060
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1061
+ "button",
1062
+ {
1063
+ onClick: handleCustomSubmit,
1064
+ disabled: !customValue.trim(),
1065
+ style: {
1066
+ padding: "5px 10px",
1067
+ fontSize: 11,
1068
+ fontWeight: 600,
1069
+ borderRadius: 6,
1070
+ border: "none",
1071
+ background: customValue.trim() ? MJH_GOLD : "#e5e7eb",
1072
+ color: customValue.trim() ? "#ffffff" : "#9ca3af",
1073
+ cursor: customValue.trim() ? "pointer" : "not-allowed"
1074
+ },
1075
+ children: "Set"
1076
+ }
1077
+ )
1078
+ ] }),
1079
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { display: "flex", flexDirection: "column", gap: 6 }, children: candidates.map((candidate) => {
1080
+ const isSelected = candidate.term === selectedTerm;
1081
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
1082
+ "div",
1083
+ {
1084
+ onClick: () => onSelect(candidate.term),
1085
+ style: {
1086
+ display: "flex",
1087
+ alignItems: "center",
1088
+ gap: 8,
1089
+ padding: "6px 8px",
1090
+ borderRadius: 8,
1091
+ cursor: "pointer",
1092
+ background: isSelected ? "rgba(22,163,74,0.06)" : "transparent",
1093
+ border: isSelected ? "1px solid rgba(22,163,74,0.2)" : "1px solid transparent",
1094
+ transition: "all 150ms"
1095
+ },
1096
+ children: [
1097
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: {
1098
+ width: 14,
1099
+ height: 14,
1100
+ borderRadius: "50%",
1101
+ border: isSelected ? "none" : "2px solid #d1d5db",
1102
+ background: isSelected ? "#16a34a" : "transparent",
1103
+ flexShrink: 0,
1104
+ display: "flex",
1105
+ alignItems: "center",
1106
+ justifyContent: "center"
1107
+ }, children: isSelected && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { width: 6, height: 6, borderRadius: "50%", background: "#ffffff" } }) }),
1108
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { flex: 1, minWidth: 0 }, children: [
1109
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 6, flexWrap: "wrap" }, children: [
1110
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: {
1111
+ fontSize: 12,
1112
+ fontWeight: isSelected ? 600 : 400,
1113
+ color: isSelected ? "#166534" : "#374151"
1114
+ }, children: candidate.term }),
1115
+ candidate.volume != null && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("span", { style: { fontSize: 11, color: "#9ca3af" }, children: [
1116
+ abbrevVol(candidate.volume),
1117
+ "/mo"
1118
+ ] }),
1119
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("span", { style: { fontSize: 10, color: "#9ca3af" }, children: [
1120
+ Math.round(candidate.confidence * 100),
1121
+ "% confidence"
1122
+ ] })
1123
+ ] }),
1124
+ candidate.placement && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { marginTop: 3 }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PlacementIndicators, { placement: candidate.placement }) })
1125
+ ] })
1126
+ ]
1127
+ },
1128
+ candidate.term
1129
+ );
1130
+ }) })
1131
+ ] });
1132
+ }
1133
+ var PLACEMENT_FILTERS = [
1134
+ { key: "inBody", label: "Body", letter: "B" },
1135
+ { key: "inTitle", label: "Title", letter: "T" },
1136
+ { key: "inMetaDescription", label: "Description", letter: "D" },
1137
+ { key: "inImages", label: "Images", letter: "I" },
1138
+ { key: "inUrl", label: "URL", letter: "U" }
1139
+ ];
1140
+ var INITIAL_SHOW = 8;
1141
+ function computePlacementClient(term, content, fields) {
1142
+ const lower = term.toLowerCase();
1143
+ return {
1144
+ inBody: content.toLowerCase().includes(lower),
1145
+ inTitle: fields?.title?.toLowerCase().includes(lower) ?? false,
1146
+ inMetaDescription: fields?.metaDescription?.toLowerCase().includes(lower) ?? false,
1147
+ inImages: fields?.imageNames?.some((name) => name.toLowerCase().includes(lower)) ?? false,
1148
+ inUrl: fields?.slug?.toLowerCase().includes(lower) ?? false
1149
+ };
1150
+ }
1151
+ function SuggestedKeywordsCard({
1152
+ keywords,
1153
+ text,
1154
+ apiUrl,
1155
+ publication,
1156
+ documentFields,
1157
+ refreshVolumes,
1158
+ isVolumesLoading
1159
+ }) {
1160
+ const [helpData, setHelpData] = (0, import_react7.useState)({});
1161
+ const [loadingTerm, setLoadingTerm] = (0, import_react7.useState)(null);
1162
+ const [regeneratingTerm, setRegeneratingTerm] = (0, import_react7.useState)(null);
1163
+ const [manualInputOpen, setManualInputOpen] = (0, import_react7.useState)(false);
1164
+ const [manualInputValue, setManualInputValue] = (0, import_react7.useState)("");
1165
+ const [manualKeywords, setManualKeywords] = (0, import_react7.useState)([]);
1166
+ const [expanded, setExpanded] = (0, import_react7.useState)(false);
1167
+ const [extraKeywords, setExtraKeywords] = (0, import_react7.useState)([]);
1168
+ const [isGeneratingMore, setIsGeneratingMore] = (0, import_react7.useState)(false);
1169
+ const [sortAsc, setSortAsc] = (0, import_react7.useState)(false);
1170
+ const [activeFilters, setActiveFilters] = (0, import_react7.useState)(/* @__PURE__ */ new Set());
1171
+ const [showFilterDropdown, setShowFilterDropdown] = (0, import_react7.useState)(false);
1172
+ const fetchHelp = async (term, forceRegenerate = false) => {
1173
+ if (helpData[term] && !forceRegenerate) {
1174
+ const next = { ...helpData };
1175
+ delete next[term];
1176
+ setHelpData(next);
1177
+ return;
1178
+ }
1179
+ if (forceRegenerate) {
1180
+ setRegeneratingTerm(term);
1181
+ } else {
1182
+ setLoadingTerm(term);
1183
+ }
1184
+ try {
1185
+ const previousSuggestions = forceRegenerate && helpData[term] ? helpData[term].map((s) => s.revised || s.sentence || "").filter(Boolean) : void 0;
1186
+ const res = await fetch(`${apiUrl}/api/seo-copilot/keyword-help`, {
1187
+ method: "POST",
1188
+ headers: { "Content-Type": "application/json" },
1189
+ body: JSON.stringify({ keyword: term, content: text, publication, previousSuggestions })
1190
+ });
1191
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
1192
+ const data = await res.json();
1193
+ setHelpData((prev) => ({ ...prev, [term]: data.suggestions || [] }));
1194
+ } catch {
1195
+ setHelpData((prev) => ({ ...prev, [term]: [{ type: "add", sentence: "Failed to generate suggestions. Try again.", where: "" }] }));
1196
+ }
1197
+ setLoadingTerm(null);
1198
+ setRegeneratingTerm(null);
1199
+ };
1200
+ const [enrichingTerms, setEnrichingTerms] = (0, import_react7.useState)(/* @__PURE__ */ new Set());
1201
+ const handleAddManual = async () => {
1202
+ const trimmed = manualInputValue.trim();
1203
+ if (!trimmed) return;
1204
+ if (manualKeywords.includes(trimmed) || allKeywords.some((kw) => kw.term.toLowerCase() === trimmed.toLowerCase())) return;
1205
+ setManualKeywords((prev) => [...prev, trimmed]);
1206
+ setManualInputValue("");
1207
+ const placement = computePlacementClient(trimmed, text, documentFields);
1208
+ const inContent = text.toLowerCase().includes(trimmed.toLowerCase());
1209
+ setExtraKeywords((prev) => [...prev, { term: trimmed, volume: 0, inContent, placement }]);
1210
+ setEnrichingTerms((prev) => new Set(prev).add(trimmed));
1211
+ try {
1212
+ const res = await fetch(`${apiUrl}/api/seo-copilot/keywords`, {
1213
+ method: "POST",
1214
+ headers: { "Content-Type": "application/json" },
1215
+ body: JSON.stringify({ keywords: [trimmed] })
1216
+ });
1217
+ if (res.ok) {
1218
+ const data = await res.json();
1219
+ const match = (data.results || []).find((r) => r.keyword.toLowerCase() === trimmed.toLowerCase());
1220
+ if (match) {
1221
+ setExtraKeywords((prev) => prev.map(
1222
+ (kw) => kw.term.toLowerCase() === trimmed.toLowerCase() ? { ...kw, volume: match.volume ?? 0 } : kw
1223
+ ));
1224
+ }
1225
+ }
1226
+ } catch {
1227
+ }
1228
+ setEnrichingTerms((prev) => {
1229
+ const next = new Set(prev);
1230
+ next.delete(trimmed);
1231
+ return next;
1232
+ });
1233
+ fetchHelp(trimmed);
1234
+ };
1235
+ const toggleFilter = (key) => {
1236
+ setActiveFilters((prev) => {
1237
+ if (key === "none") {
1238
+ return prev.has("none") ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set(["none"]);
1239
+ }
1240
+ const next = new Set(prev);
1241
+ next.delete("none");
1242
+ if (next.has(key)) {
1243
+ next.delete(key);
1244
+ } else {
1245
+ next.add(key);
1246
+ }
1247
+ return next;
1248
+ });
1249
+ };
1250
+ let filtered = keywords;
1251
+ if (activeFilters.size > 0) {
1252
+ const hasNoneFilter = activeFilters.has("none");
1253
+ const placementFilters = [...activeFilters].filter((f) => f !== "none");
1254
+ filtered = keywords.filter((kw) => {
1255
+ if (hasNoneFilter) {
1256
+ return placementCount(kw.placement) === 0;
1257
+ }
1258
+ if (placementFilters.length === 0) return true;
1259
+ if (!kw.placement) return false;
1260
+ for (const f of placementFilters) {
1261
+ if (f === "none") continue;
1262
+ if (!kw.placement[f]) return false;
1263
+ }
1264
+ return true;
1265
+ });
1266
+ }
1267
+ const sorted = [...filtered].sort((a, b) => {
1268
+ const pA = placementCount(a.placement);
1269
+ const pB = placementCount(b.placement);
1270
+ if (sortAsc) {
1271
+ if (pA !== pB) return pA - pB;
1272
+ return a.volume - b.volume;
1273
+ }
1274
+ if (pA !== pB) return pB - pA;
1275
+ return b.volume - a.volume;
1276
+ });
1277
+ const allKeywords = [...sorted, ...extraKeywords.filter((ek) => !sorted.some((s) => s.term.toLowerCase() === ek.term.toLowerCase()))];
1278
+ const visible = expanded ? allKeywords : allKeywords.slice(0, INITIAL_SHOW);
1279
+ const hiddenCount = allKeywords.length - INITIAL_SHOW;
1280
+ const missingCount = keywords.filter((kw) => !kw.inContent).length;
1281
+ const generateMoreKeywords = async () => {
1282
+ setIsGeneratingMore(true);
1283
+ try {
1284
+ const existingTerms = allKeywords.map((kw) => kw.term);
1285
+ const res = await fetch(`${apiUrl}/api/seo-copilot/more-keywords`, {
1286
+ method: "POST",
1287
+ headers: { "Content-Type": "application/json" },
1288
+ body: JSON.stringify({ content: text, existingKeywords: existingTerms, publication })
1289
+ });
1290
+ if (res.ok) {
1291
+ const data = await res.json();
1292
+ const newKws = (data.keywords || []).map((kw) => ({
1293
+ term: kw.term,
1294
+ volume: kw.volume ?? 0,
1295
+ inContent: kw.inContent
1296
+ }));
1297
+ setExtraKeywords((prev) => [...prev, ...newKws]);
1298
+ }
1299
+ } catch {
1300
+ }
1301
+ setIsGeneratingMore(false);
1302
+ };
1303
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: cardStyle, children: [
1304
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 6, marginBottom: 8 }, children: [
1305
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { width: 6, height: 6, borderRadius: "50%", background: MJH_BLUE, flexShrink: 0 } }),
1306
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 10, textTransform: "uppercase", letterSpacing: "0.05em", color: "#9ca3af", fontWeight: 600, flex: 1 }, children: "Supporting Keywords" }),
1307
+ refreshVolumes && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1308
+ "button",
1309
+ {
1310
+ onClick: refreshVolumes,
1311
+ disabled: isVolumesLoading,
1312
+ style: { background: "none", border: "none", cursor: isVolumesLoading ? "wait" : "pointer", padding: 2, display: "flex", color: isVolumesLoading ? "#d1d5db" : "#9ca3af", fontSize: 10 },
1313
+ title: "Refresh keyword volumes",
1314
+ children: isVolumesLoading ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: "#9ca3af" }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", style: { transition: "transform 150ms" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { fillRule: "evenodd", d: "M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H4.598a.75.75 0 00-.75.75v3.634a.75.75 0 001.5 0v-2.033l.312.311a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm-10.624-2.85a5.5 5.5 0 019.201-2.466l.312.311h-2.433a.75.75 0 000 1.5h3.634a.75.75 0 00.75-.75V3.535a.75.75 0 00-1.5 0v2.033l-.312-.311A7 7 0 002.628 8.395a.75.75 0 001.449.39z", clipRule: "evenodd" }) })
1315
+ }
1316
+ ),
1317
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1318
+ "button",
1319
+ {
1320
+ onClick: () => setSortAsc(!sortAsc),
1321
+ style: { background: "none", border: "none", cursor: "pointer", padding: 2, display: "flex", color: "#9ca3af", fontSize: 10 },
1322
+ title: sortAsc ? "Sort: least placements first" : "Sort: most placements first",
1323
+ children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", style: { transform: sortAsc ? "rotate(180deg)" : "none", transition: "transform 150ms" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { fillRule: "evenodd", d: "M10 3a.75.75 0 01.55.24l3.25 3.5a.75.75 0 11-1.1 1.02L10 4.852 7.3 7.76a.75.75 0 01-1.1-1.02l3.25-3.5A.75.75 0 0110 3zm-3.76 9.2a.75.75 0 011.06.04l2.7 2.908 2.7-2.908a.75.75 0 111.1 1.02l-3.25 3.5a.75.75 0 01-1.1 0l-3.25-3.5a.75.75 0 01.04-1.06z", clipRule: "evenodd" }) })
1324
+ }
1325
+ ),
1326
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { position: "relative" }, children: [
1327
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
1328
+ "button",
1329
+ {
1330
+ onClick: () => setShowFilterDropdown(!showFilterDropdown),
1331
+ style: {
1332
+ background: activeFilters.size > 0 ? "rgba(0,93,172,0.1)" : "none",
1333
+ border: "none",
1334
+ cursor: "pointer",
1335
+ padding: "2px 4px",
1336
+ display: "flex",
1337
+ alignItems: "center",
1338
+ gap: 2,
1339
+ color: activeFilters.size > 0 ? MJH_BLUE : "#9ca3af",
1340
+ borderRadius: 4,
1341
+ fontSize: 10
1342
+ },
1343
+ title: "Filter by placement",
1344
+ children: [
1345
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { fillRule: "evenodd", d: "M2.628 1.601C5.028 1.206 7.49 1 10 1s4.973.206 7.372.601a.75.75 0 01.628.74v2.288a2.25 2.25 0 01-.659 1.59l-4.682 4.683a2.25 2.25 0 00-.659 1.59v3.037c0 .684-.31 1.33-.844 1.757l-1.937 1.55A.75.75 0 018 18.25v-5.757a2.25 2.25 0 00-.659-1.591L2.659 6.22A2.25 2.25 0 012 4.629V2.34a.75.75 0 01.628-.74z", clipRule: "evenodd" }) }),
1346
+ activeFilters.size > 0 && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { children: activeFilters.size })
1347
+ ]
1348
+ }
1349
+ ),
1350
+ showFilterDropdown && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: {
1351
+ position: "absolute",
1352
+ top: "100%",
1353
+ right: 0,
1354
+ marginTop: 4,
1355
+ width: 150,
1356
+ background: "#ffffff",
1357
+ borderRadius: 8,
1358
+ boxShadow: "0 4px 12px rgba(0,0,0,0.12)",
1359
+ border: "1px solid #e5e7eb",
1360
+ zIndex: 50,
1361
+ padding: "4px 0"
1362
+ }, children: [
1363
+ PLACEMENT_FILTERS.map(({ key, label, letter }) => /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
1364
+ "button",
1365
+ {
1366
+ onClick: () => toggleFilter(key),
1367
+ style: {
1368
+ width: "100%",
1369
+ display: "flex",
1370
+ alignItems: "center",
1371
+ gap: 8,
1372
+ padding: "5px 10px",
1373
+ background: "none",
1374
+ border: "none",
1375
+ cursor: "pointer",
1376
+ fontSize: 11,
1377
+ color: "#374151",
1378
+ textAlign: "left"
1379
+ },
1380
+ children: [
1381
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: {
1382
+ width: 14,
1383
+ height: 14,
1384
+ borderRadius: 3,
1385
+ border: "1.5px solid " + (activeFilters.has(key) ? MJH_BLUE : "#d1d5db"),
1386
+ background: activeFilters.has(key) ? MJH_BLUE : "transparent",
1387
+ display: "flex",
1388
+ alignItems: "center",
1389
+ justifyContent: "center",
1390
+ flexShrink: 0
1391
+ }, children: activeFilters.has(key) && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "8", height: "8", viewBox: "0 0 12 12", fill: "white", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { d: "M10 3L4.5 8.5 2 6", stroke: "white", strokeWidth: "2", fill: "none", strokeLinecap: "round", strokeLinejoin: "round" }) }) }),
1392
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontWeight: 600, color: "#9ca3af", width: 14 }, children: letter }),
1393
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { children: label })
1394
+ ]
1395
+ },
1396
+ key
1397
+ )),
1398
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
1399
+ "button",
1400
+ {
1401
+ onClick: () => toggleFilter("none"),
1402
+ style: {
1403
+ width: "100%",
1404
+ display: "flex",
1405
+ alignItems: "center",
1406
+ gap: 8,
1407
+ padding: "5px 10px",
1408
+ background: "none",
1409
+ border: "none",
1410
+ cursor: "pointer",
1411
+ fontSize: 11,
1412
+ color: "#374151",
1413
+ textAlign: "left",
1414
+ borderTop: "1px solid #f1f5f9"
1415
+ },
1416
+ children: [
1417
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: {
1418
+ width: 14,
1419
+ height: 14,
1420
+ borderRadius: 3,
1421
+ border: "1.5px solid " + (activeFilters.has("none") ? MJH_BLUE : "#d1d5db"),
1422
+ background: activeFilters.has("none") ? MJH_BLUE : "transparent",
1423
+ display: "flex",
1424
+ alignItems: "center",
1425
+ justifyContent: "center",
1426
+ flexShrink: 0
1427
+ }, children: activeFilters.has("none") && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "8", height: "8", viewBox: "0 0 12 12", fill: "white", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { d: "M10 3L4.5 8.5 2 6", stroke: "white", strokeWidth: "2", fill: "none", strokeLinecap: "round", strokeLinejoin: "round" }) }) }),
1428
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontWeight: 600, color: "#9ca3af", width: 14 }, children: "-" }),
1429
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { children: "None (missing all)" })
1430
+ ]
1431
+ }
1432
+ ),
1433
+ activeFilters.size > 0 && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1434
+ "button",
1435
+ {
1436
+ onClick: () => {
1437
+ setActiveFilters(/* @__PURE__ */ new Set());
1438
+ setShowFilterDropdown(false);
1439
+ },
1440
+ style: {
1441
+ width: "100%",
1442
+ padding: "5px 10px",
1443
+ background: "none",
1444
+ border: "none",
1445
+ borderTop: "1px solid #f1f5f9",
1446
+ cursor: "pointer",
1447
+ fontSize: 10,
1448
+ color: "#dc2626",
1449
+ textAlign: "center"
1450
+ },
1451
+ children: "Clear filters"
1452
+ }
1453
+ )
1454
+ ] })
1455
+ ] }),
1456
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PencilButton, { onClick: () => setManualInputOpen(!manualInputOpen), active: manualInputOpen })
1457
+ ] }),
1458
+ manualInputOpen && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { marginBottom: 8, display: "flex", gap: 6 }, children: [
1459
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1460
+ "input",
1461
+ {
1462
+ type: "text",
1463
+ value: manualInputValue,
1464
+ onChange: (e) => setManualInputValue(e.target.value),
1465
+ onKeyDown: (e) => {
1466
+ if (e.key === "Enter") handleAddManual();
1467
+ },
1468
+ placeholder: "Add a keyword manually",
1469
+ autoFocus: true,
1470
+ style: { flex: 1, padding: "5px 8px", fontSize: 12, borderRadius: 8, border: "1px solid #e5e7eb", outline: "none", fontFamily: "inherit" }
1471
+ }
1472
+ ),
1473
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1474
+ "button",
1475
+ {
1476
+ onClick: handleAddManual,
1477
+ disabled: !manualInputValue.trim(),
1478
+ style: {
1479
+ padding: "5px 10px",
1480
+ fontSize: 11,
1481
+ fontWeight: 600,
1482
+ borderRadius: 6,
1483
+ border: "none",
1484
+ background: manualInputValue.trim() ? MJH_BLUE : "#e5e7eb",
1485
+ color: manualInputValue.trim() ? "#ffffff" : "#9ca3af",
1486
+ cursor: manualInputValue.trim() ? "pointer" : "not-allowed"
1487
+ },
1488
+ children: "Add"
1489
+ }
1490
+ )
1491
+ ] }),
1492
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { children: visible.map((kw, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { children: [
1493
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: {
1494
+ display: "flex",
1495
+ alignItems: "center",
1496
+ gap: 8,
1497
+ padding: "5px 0",
1498
+ borderBottom: i < visible.length - 1 ? "1px solid #f3f4f6" : "none",
1499
+ minHeight: 28
1500
+ }, children: [
1501
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: {
1502
+ width: 8,
1503
+ height: 8,
1504
+ borderRadius: "50%",
1505
+ flexShrink: 0,
1506
+ background: kw.inContent ? "#16a34a" : "#d1d5db"
1507
+ } }),
1508
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 12, fontWeight: 500, flexGrow: 1, color: kw.inContent ? "#9ca3af" : "#374151" }, children: kw.term }),
1509
+ enrichingTerms.has(kw.term) && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: "#9ca3af" }),
1510
+ !enrichingTerms.has(kw.term) && kw.volume != null && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 10, color: "#d1d5db", flexShrink: 0 }, children: abbrevVol(kw.volume) }),
1511
+ kw.placement && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PlacementIndicators, { placement: kw.placement }),
1512
+ !kw.inContent && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LightbulbButton, { onClick: () => fetchHelp(kw.term), isLoading: loadingTerm === kw.term })
1513
+ ] }),
1514
+ helpData[kw.term] && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1515
+ KeywordHelpDisplay,
1516
+ {
1517
+ suggestions: helpData[kw.term],
1518
+ onRegenerate: () => fetchHelp(kw.term, true),
1519
+ isRegenerating: regeneratingTerm === kw.term,
1520
+ term: kw.term,
1521
+ publication
1522
+ }
1523
+ )
1524
+ ] }, kw.term)) }),
1525
+ !expanded && hiddenCount > 0 && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
1526
+ "button",
1527
+ {
1528
+ onClick: () => setExpanded(true),
1529
+ style: {
1530
+ marginTop: 6,
1531
+ width: "100%",
1532
+ padding: "4px 0",
1533
+ background: "none",
1534
+ border: "none",
1535
+ cursor: "pointer",
1536
+ fontSize: 10,
1537
+ color: "#9ca3af",
1538
+ display: "flex",
1539
+ alignItems: "center",
1540
+ justifyContent: "center",
1541
+ gap: 4
1542
+ },
1543
+ children: [
1544
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "10", height: "10", viewBox: "0 0 20 20", fill: "#9ca3af", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { fillRule: "evenodd", d: "M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z", clipRule: "evenodd" }) }),
1545
+ "Show more"
1546
+ ]
1547
+ }
1548
+ ),
1549
+ expanded && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1550
+ "button",
1551
+ {
1552
+ onClick: generateMoreKeywords,
1553
+ disabled: isGeneratingMore,
1554
+ style: {
1555
+ marginTop: 6,
1556
+ width: "100%",
1557
+ padding: "5px 0",
1558
+ background: "none",
1559
+ border: "none",
1560
+ cursor: isGeneratingMore ? "wait" : "pointer",
1561
+ fontSize: 10,
1562
+ color: isGeneratingMore ? "#9ca3af" : MJH_BLUE,
1563
+ display: "flex",
1564
+ alignItems: "center",
1565
+ justifyContent: "center",
1566
+ gap: 4,
1567
+ fontWeight: 500
1568
+ },
1569
+ children: isGeneratingMore ? /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
1570
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: MJH_BLUE }),
1571
+ "Generating..."
1572
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
1573
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("svg", { width: "10", height: "10", viewBox: "0 0 20 20", fill: MJH_BLUE, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("path", { fillRule: "evenodd", d: "M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H4.598a.75.75 0 00-.75.75v3.634a.75.75 0 001.5 0v-2.033l.312.311a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm-10.624-2.85a5.5 5.5 0 019.201-2.466l.312.311h-2.433a.75.75 0 000 1.5h3.634a.75.75 0 00.75-.75V3.535a.75.75 0 00-1.5 0v2.033l-.312-.311A7 7 0 002.628 8.395a.75.75 0 001.449.39z", clipRule: "evenodd" }) }),
1574
+ "Generate more"
1575
+ ] })
1576
+ }
1577
+ )
1578
+ ] });
1579
+ }
1580
+ function KeywordPanel({
1581
+ text,
1582
+ publication,
1583
+ seedKeywords = [],
1584
+ onSeedsChange,
1585
+ onSelectPrimary,
1586
+ documentFields,
1587
+ externalAnalysis
1588
+ }) {
1589
+ const { apiUrl } = useConfig();
1590
+ const internalAnalysis = useKeywordAnalysis(text, apiUrl, publication, seedKeywords, documentFields);
1591
+ const activeAnalysis = externalAnalysis || internalAnalysis;
1592
+ const { analysis, isLoading, error, deepAnalysis, isDeepLoading, runDeepAnalysis } = activeAnalysis;
1593
+ const isVolumesLoading = activeAnalysis.isVolumesLoading ?? false;
1594
+ const refreshVolumes = activeAnalysis.refreshVolumes;
1595
+ const aeoContentChanged = activeAnalysis.aeoContentChanged ?? false;
1596
+ const [aeoTriggered, setAeoTriggered] = (0, import_react7.useState)(false);
1597
+ (0, import_react7.useEffect)(() => {
1598
+ const hasV2Aeo = deepAnalysis?.aeo?.questionHeadings?.length || deepAnalysis?.aeo?.faqSuggestions?.length;
1599
+ if (analysis && !hasV2Aeo && !isDeepLoading && !aeoTriggered) {
1600
+ setAeoTriggered(true);
1601
+ runDeepAnalysis();
1602
+ }
1603
+ }, [analysis, deepAnalysis, isDeepLoading, aeoTriggered, runDeepAnalysis]);
1604
+ (0, import_react7.useEffect)(() => {
1605
+ setAeoTriggered(false);
1606
+ }, [text]);
1607
+ const [moreFaqs, setMoreFaqs] = (0, import_react7.useState)([]);
1608
+ const [isMoreFaqsLoading, setIsMoreFaqsLoading] = (0, import_react7.useState)(false);
1609
+ const loadMoreFaqs = (0, import_react7.useCallback)(async () => {
1610
+ const aeo = deepAnalysis?.aeo;
1611
+ const existingQs = [...(aeo?.faqSuggestions ?? aeo?.faqItems ?? []).map((f) => f.question), ...moreFaqs.map((f) => f.question)];
1612
+ setIsMoreFaqsLoading(true);
1613
+ try {
1614
+ const res = await fetch(`${apiUrl}/api/seo-copilot/more-faqs`, {
1615
+ method: "POST",
1616
+ headers: { "Content-Type": "application/json" },
1617
+ body: JSON.stringify({ content: text, existingQuestions: existingQs, publication })
1618
+ });
1619
+ if (res.ok) {
1620
+ const data = await res.json();
1621
+ setMoreFaqs((prev) => [...prev, ...data.faqSuggestions || []]);
1622
+ }
1623
+ } catch {
1624
+ }
1625
+ setIsMoreFaqsLoading(false);
1626
+ }, [deepAnalysis, moreFaqs, apiUrl, text, publication]);
1627
+ const defaultSelectedTerm = analysis?.primaryKeyword?.term ?? "";
1628
+ const [selectedPrimaryTerm, setSelectedPrimaryTerm] = (0, import_react7.useState)(defaultSelectedTerm);
1629
+ const prevDefaultRef = (0, import_react7.useRef)(defaultSelectedTerm);
1630
+ (0, import_react7.useEffect)(() => {
1631
+ if (defaultSelectedTerm && defaultSelectedTerm !== prevDefaultRef.current) {
1632
+ setSelectedPrimaryTerm((prev) => prev === prevDefaultRef.current ? defaultSelectedTerm : prev);
1633
+ prevDefaultRef.current = defaultSelectedTerm;
1634
+ }
1635
+ }, [defaultSelectedTerm]);
1636
+ if ((!text || text.trim().length < 20) && (!seedKeywords || seedKeywords.length === 0)) {
1637
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { padding: 24, textAlign: "center" }, children: [
1638
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 14, color: "#9ca3af", fontWeight: 500 }, children: "Start writing to see keyword suggestions" }),
1639
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 12, color: "#d1d5db", marginTop: 4 }, children: "Need at least a few sentences" })
1640
+ ] });
1641
+ }
1642
+ if (isLoading && !analysis) {
1643
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { padding: 24, display: "flex", alignItems: "center", justifyContent: "center", gap: 8 }, children: [
1644
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, {}),
1645
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 12, color: "#9ca3af" }, children: "Analyzing content..." })
1646
+ ] });
1647
+ }
1648
+ if (error && !analysis) {
1649
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { ...cardStyle, margin: 12, background: "#fef2f2", color: "#dc2626" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("span", { style: { fontSize: 12 }, children: [
1650
+ "Error: ",
1651
+ error
1652
+ ] }) });
1653
+ }
1654
+ if (!analysis) return null;
1655
+ const suggestedKeywords = buildSuggestedKeywords(
1656
+ analysis.supportingKeywords || [],
1657
+ analysis.missingKeywords,
1658
+ text
1659
+ );
1660
+ const candidates = analysis.primaryKeywordCandidates && analysis.primaryKeywordCandidates.length > 0 ? analysis.primaryKeywordCandidates : [analysis.primaryKeyword];
1661
+ const activeTerm = candidates.find((c) => c.term === selectedPrimaryTerm) ? selectedPrimaryTerm : candidates[0]?.term ?? "";
1662
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { padding: 12, display: "flex", flexDirection: "column", gap: 12 }, children: [
1663
+ onSeedsChange && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(SeedKeywordInput, { seeds: seedKeywords, onSeedsChange }),
1664
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1665
+ PrimaryKeywordSelector,
1666
+ {
1667
+ candidates,
1668
+ selectedTerm: activeTerm,
1669
+ onSelect: (term) => {
1670
+ setSelectedPrimaryTerm(term);
1671
+ onSelectPrimary?.(term);
1672
+ },
1673
+ publication
1674
+ }
1675
+ ),
1676
+ suggestedKeywords.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1677
+ SuggestedKeywordsCard,
1678
+ {
1679
+ keywords: suggestedKeywords,
1680
+ text,
1681
+ apiUrl,
1682
+ publication,
1683
+ documentFields,
1684
+ refreshVolumes,
1685
+ isVolumesLoading
1686
+ }
1687
+ ),
1688
+ !deepAnalysis && isDeepLoading ? /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", gap: 8, padding: "12px 0" }, children: [
1689
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: AEO_PURPLE }),
1690
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 12, color: "#9ca3af" }, children: "Generating AEO insights..." })
1691
+ ] }) : deepAnalysis ? /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", flexDirection: "column", gap: 12 }, children: [
1692
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { padding: "6px 10px", background: "#fefce8", borderRadius: 8, border: "1px solid #fde68a" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 10, color: "#92400e", lineHeight: 1.5 }, children: "Answers generated by AI may contain inaccuracies. Always verify facts, drug information, and clinical details before publishing." }) }),
1693
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { ...cardStyle, padding: "8px 14px", background: "rgba(124,58,237,0.03)", borderLeft: "3px solid " + AEO_PURPLE }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 7 }, children: [
1694
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { width: 7, height: 7, borderRadius: "50%", background: AEO_PURPLE, flexShrink: 0 } }),
1695
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 10.5, textTransform: "uppercase", letterSpacing: "0.06em", color: "#94a3b8", fontWeight: 600 }, children: "AEO Insights" })
1696
+ ] }) }),
1697
+ !deepAnalysis.aeo && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { ...cardStyle, textAlign: "center" }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 12, color: "#9ca3af" }, children: "No AEO data was returned. Try again - the analysis may have timed out." }) }),
1698
+ deepAnalysis.aeo && (deepAnalysis.aeo.questionHeadings ?? deepAnalysis.aeo.headingSuggestions ?? []).length > 0 && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: cardStyle, children: [
1699
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(SectionHeader, { label: "Question Headline Recommendations", iconColor: AEO_PURPLE }),
1700
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 10, color: "#9ca3af", marginBottom: 8, marginTop: -4 }, children: "Rewrite your headings as questions. Google targets these for featured snippets." }),
1701
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { display: "flex", flexDirection: "column", gap: 8 }, children: (deepAnalysis.aeo.questionHeadings?.map((qh) => ({ heading: qh.suggestedHeading, originalHeading: qh.originalHeading, rationale: qh.rationale })) ?? deepAnalysis.aeo.headingSuggestions?.map((h) => ({ heading: h.heading, originalHeading: void 0, rationale: h.rationale })) ?? []).map((h, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { padding: "8px 10px", background: "rgba(124,58,237,0.03)", borderRadius: 10, borderLeft: "3px solid " + AEO_PURPLE }, children: [
1702
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 8 }, children: [
1703
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { flex: 1 }, children: [
1704
+ h.originalHeading && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 11, color: "#9ca3af", textDecoration: "line-through", marginBottom: 2 }, children: h.originalHeading }),
1705
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 12, fontWeight: 700, color: AEO_PURPLE, lineHeight: 1.5 }, children: h.heading })
1706
+ ] }),
1707
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1708
+ FeedbackButton,
1709
+ {
1710
+ suggestionType: "aeo_heading",
1711
+ suggestionText: h.heading,
1712
+ publication
1713
+ }
1714
+ )
1715
+ ] }),
1716
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 10, color: "#9ca3af", marginTop: 4, fontStyle: "italic" }, children: h.rationale })
1717
+ ] }, i)) })
1718
+ ] }),
1719
+ (() => {
1720
+ const faqs = deepAnalysis.aeo ? [...deepAnalysis.aeo.faqSuggestions ?? deepAnalysis.aeo.faqItems ?? [], ...moreFaqs] : [];
1721
+ return faqs.length > 0 ? /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: cardStyle, children: [
1722
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(SectionHeader, { label: "FAQ Suggestions", iconColor: AEO_PURPLE }),
1723
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: 10, color: "#9ca3af", marginBottom: 8, marginTop: -4 }, children: "Add these Q&As at the bottom of your article. Answers are derived from your article content. Can be wrapped with FAQ schema markup." }),
1724
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { display: "flex", flexDirection: "column", gap: 8 }, children: faqs.map((faq, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { padding: "8px 10px", background: "rgba(124,58,237,0.03)", borderRadius: 10, borderLeft: "3px solid " + AEO_PURPLE }, children: [
1725
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 8 }, children: [
1726
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("span", { style: { fontSize: 12, fontWeight: 700, color: "#1f2937", lineHeight: 1.5 }, children: [
1727
+ "Q: ",
1728
+ faq.question
1729
+ ] }),
1730
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1731
+ FeedbackButton,
1732
+ {
1733
+ suggestionType: "aeo_faq",
1734
+ suggestionText: faq.question,
1735
+ publication
1736
+ }
1737
+ )
1738
+ ] }),
1739
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { fontSize: 12, color: "#374151", lineHeight: 1.6, marginTop: 4 }, children: [
1740
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontWeight: 600, color: "#6b7280" }, children: "A: " }),
1741
+ faq.answer
1742
+ ] })
1743
+ ] }, i)) }),
1744
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1745
+ "button",
1746
+ {
1747
+ onClick: loadMoreFaqs,
1748
+ disabled: isMoreFaqsLoading,
1749
+ style: {
1750
+ marginTop: 8,
1751
+ width: "100%",
1752
+ padding: "6px 12px",
1753
+ fontSize: 11,
1754
+ fontWeight: 600,
1755
+ color: isMoreFaqsLoading ? "#9ca3af" : AEO_PURPLE,
1756
+ background: isMoreFaqsLoading ? "#f3f4f6" : "rgba(124,58,237,0.06)",
1757
+ border: "1px solid rgba(124,58,237,0.15)",
1758
+ borderRadius: 6,
1759
+ cursor: isMoreFaqsLoading ? "wait" : "pointer",
1760
+ display: "flex",
1761
+ alignItems: "center",
1762
+ justifyContent: "center",
1763
+ gap: 6,
1764
+ transition: "all 150ms"
1765
+ },
1766
+ children: isMoreFaqsLoading ? /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
1767
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: AEO_PURPLE }),
1768
+ " Generating..."
1769
+ ] }) : "More FAQ suggestions"
1770
+ }
1771
+ )
1772
+ ] }) : null;
1773
+ })(),
1774
+ aeoContentChanged && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
1775
+ "div",
1776
+ {
1777
+ onClick: runDeepAnalysis,
1778
+ style: {
1779
+ padding: "8px 12px",
1780
+ background: "#fffbeb",
1781
+ border: "1px solid #fde68a",
1782
+ borderRadius: 8,
1783
+ display: "flex",
1784
+ alignItems: "center",
1785
+ justifyContent: "space-between",
1786
+ cursor: "pointer"
1787
+ },
1788
+ children: [
1789
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 11, color: "#92400e" }, children: "Content has changed since this was generated." }),
1790
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 11, fontWeight: 600, color: AEO_PURPLE, textDecoration: "underline" }, children: "Update" })
1791
+ ]
1792
+ }
1793
+ )
1794
+ ] }) : null,
1795
+ isVolumesLoading && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 6, justifyContent: "center", padding: "4px 0" }, children: [
1796
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: MJH_GOLD }),
1797
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 10, color: "#9ca3af" }, children: "Checking search volumes..." })
1798
+ ] }),
1799
+ isLoading && analysis && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 6, justifyContent: "center" }, children: [
1800
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LoadingBars, { size: "xs", color: "#9ca3af" }),
1801
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { style: { fontSize: 10, color: "#9ca3af" }, children: "Updating insights..." })
1802
+ ] })
1803
+ ] });
1804
+ }
1805
+
1806
+ // src/components/ChatPanel.tsx
1807
+ var import_react9 = require("react");
1808
+ var import_react_markdown = __toESM(require("react-markdown"));
1809
+ var import_remark_gfm = __toESM(require("remark-gfm"));
1810
+ var import_structure2 = require("sanity/structure");
1811
+
1812
+ // src/hooks/useSEOChat.ts
1813
+ var import_react8 = require("react");
1814
+ function getUserId() {
1815
+ const key = "seo-copilot-user-id";
1816
+ try {
1817
+ let id = localStorage.getItem(key);
1818
+ if (!id) {
1819
+ id = `user-${Math.random().toString(36).slice(2)}-${Date.now()}`;
1820
+ localStorage.setItem(key, id);
1821
+ }
1822
+ return id;
1823
+ } catch {
1824
+ return "anonymous";
1825
+ }
1826
+ }
1827
+ function useSEOChat(apiUrl, documentText, publication, keywordPanelData, documentId) {
1828
+ const chatCacheKey = documentId ? `seo-copilot-chat-${documentId}` : "seo-copilot-chat-default";
1829
+ const [messages, setMessagesRaw] = (0, import_react8.useState)(() => {
1830
+ try {
1831
+ const cached = typeof window !== "undefined" ? sessionStorage.getItem(chatCacheKey) : null;
1832
+ return cached ? JSON.parse(cached) : [];
1833
+ } catch {
1834
+ return [];
1835
+ }
1836
+ });
1837
+ const setMessages = (0, import_react8.useCallback)((msgs) => {
1838
+ setMessagesRaw((prev) => {
1839
+ const next = typeof msgs === "function" ? msgs(prev) : msgs;
1840
+ try {
1841
+ sessionStorage.setItem(chatCacheKey, JSON.stringify(next));
1842
+ } catch {
1843
+ }
1844
+ return next;
1845
+ });
1846
+ }, [chatCacheKey]);
1847
+ const [isStreaming, setIsStreaming] = (0, import_react8.useState)(false);
1848
+ const [historyLoaded, setHistoryLoaded] = (0, import_react8.useState)(false);
1849
+ const userId = (0, import_react8.useRef)(getUserId());
1850
+ const saveTimerRef = (0, import_react8.useRef)(null);
1851
+ (0, import_react8.useEffect)(() => {
1852
+ if (!documentId || historyLoaded) return;
1853
+ let cancelled = false;
1854
+ loadChatHistory(apiUrl, userId.current, documentId).then((loaded) => {
1855
+ if (!cancelled && loaded.length > 0 && messages.length === 0) {
1856
+ setMessages(loaded);
1857
+ }
1858
+ if (!cancelled) setHistoryLoaded(true);
1859
+ });
1860
+ return () => {
1861
+ cancelled = true;
1862
+ };
1863
+ }, [apiUrl, documentId, historyLoaded]);
1864
+ const scheduleSave = (0, import_react8.useCallback)((msgs) => {
1865
+ if (!documentId) return;
1866
+ if (saveTimerRef.current) clearTimeout(saveTimerRef.current);
1867
+ saveTimerRef.current = setTimeout(() => {
1868
+ saveChatHistory(apiUrl, userId.current, documentId, msgs, publication);
1869
+ }, 800);
1870
+ }, [apiUrl, documentId, publication]);
1871
+ const sendMessage = (0, import_react8.useCallback)(
1872
+ async (text) => {
1873
+ const userMessage = { role: "user", content: text };
1874
+ const updatedMessages = [...messages, userMessage];
1875
+ setMessages(updatedMessages);
1876
+ setIsStreaming(true);
1877
+ try {
1878
+ const stream = await fetchChatStream(
1879
+ apiUrl,
1880
+ updatedMessages,
1881
+ documentText,
1882
+ publication,
1883
+ keywordPanelData
1884
+ );
1885
+ if (!stream) {
1886
+ setIsStreaming(false);
1887
+ return;
1888
+ }
1889
+ const reader = stream.getReader();
1890
+ const decoder = new TextDecoder();
1891
+ let assistantContent = "";
1892
+ setMessages((prev) => [...prev, { role: "assistant", content: "" }]);
1893
+ while (true) {
1894
+ const { done, value } = await reader.read();
1895
+ if (done) break;
1896
+ const chunk = decoder.decode(value, { stream: true });
1897
+ const lines = chunk.split("\n");
1898
+ for (const line of lines) {
1899
+ if (line.startsWith("data: ")) {
1900
+ const data = line.slice(6);
1901
+ if (data === "[DONE]") continue;
1902
+ try {
1903
+ const parsed = JSON.parse(data);
1904
+ if (parsed.text) {
1905
+ assistantContent += parsed.text;
1906
+ setMessages((prev) => {
1907
+ const updated = [...prev];
1908
+ updated[updated.length - 1] = {
1909
+ role: "assistant",
1910
+ content: assistantContent
1911
+ };
1912
+ return updated;
1913
+ });
1914
+ }
1915
+ } catch {
1916
+ }
1917
+ }
1918
+ }
1919
+ }
1920
+ const finalMessages = [
1921
+ ...updatedMessages,
1922
+ { role: "assistant", content: assistantContent }
1923
+ ];
1924
+ scheduleSave(finalMessages);
1925
+ } catch (err) {
1926
+ const errorMsg = err instanceof Error ? err.message : "Chat failed";
1927
+ setMessages((prev) => [
1928
+ ...prev,
1929
+ { role: "assistant", content: `Error: ${errorMsg}` }
1930
+ ]);
1931
+ }
1932
+ setIsStreaming(false);
1933
+ },
1934
+ [messages, apiUrl, documentText, publication, keywordPanelData, scheduleSave]
1935
+ );
1936
+ const clearChat = (0, import_react8.useCallback)(() => {
1937
+ setMessages([]);
1938
+ if (documentId) {
1939
+ saveChatHistory(apiUrl, userId.current, documentId, [], publication);
1940
+ }
1941
+ }, [apiUrl, documentId, publication]);
1942
+ return { messages, isStreaming, sendMessage, clearChat };
1943
+ }
1944
+
1945
+ // src/components/ChatPanel.tsx
1946
+ var import_jsx_runtime6 = require("react/jsx-runtime");
1947
+ var SUGGESTIONS = [
1948
+ "What keywords am I missing?",
1949
+ "How do I optimize for AEO?",
1950
+ "Chat about primary and supporting keywords",
1951
+ "What semantically related terms should I include?"
1952
+ ];
1953
+ function LogoIcon() {
1954
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", children: [
1955
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("rect", { x: "1", y: "3", width: "4", height: "10", rx: "1", fill: MJH_GOLD, opacity: "1" }),
1956
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("rect", { x: "6", y: "1", width: "4", height: "14", rx: "1", fill: MJH_GOLD, opacity: "0.85" }),
1957
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("rect", { x: "11", y: "5", width: "4", height: "8", rx: "1", fill: MJH_GOLD, opacity: "0.6" })
1958
+ ] });
1959
+ }
1960
+ function ChatPanel({ text, publication, keywordPanelData }) {
1961
+ const { apiUrl } = useConfig();
1962
+ const { value } = (0, import_structure2.useDocumentPane)();
1963
+ const documentId = value?._id || void 0;
1964
+ const { messages, isStreaming, sendMessage, clearChat } = useSEOChat(apiUrl, text, publication, keywordPanelData, documentId);
1965
+ const [input, setInput] = (0, import_react9.useState)("");
1966
+ const [inputFocused, setInputFocused] = (0, import_react9.useState)(false);
1967
+ const [hoveredSuggestion, setHoveredSuggestion] = (0, import_react9.useState)(null);
1968
+ const [clearHovered, setClearHovered] = (0, import_react9.useState)(false);
1969
+ const messagesEndRef = (0, import_react9.useRef)(null);
1970
+ (0, import_react9.useEffect)(() => {
1971
+ messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
1972
+ }, [messages]);
1973
+ const handleSend = () => {
1974
+ const trimmed = input.trim();
1975
+ if (!trimmed || isStreaming) return;
1976
+ setInput("");
1977
+ sendMessage(trimmed);
1978
+ };
1979
+ const handleKeyDown = (e) => {
1980
+ if (e.key === "Enter" && !e.shiftKey) {
1981
+ e.preventDefault();
1982
+ handleSend();
1983
+ }
1984
+ };
1985
+ const showTypingBars = isStreaming && (messages.length === 0 || messages[messages.length - 1].role === "user" || messages[messages.length - 1].role === "assistant" && !messages[messages.length - 1].content);
1986
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { style: { position: "relative", flex: 1, minHeight: 0 }, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { style: { position: "absolute", top: 0, left: 0, right: 0, bottom: 0, display: "flex", flexDirection: "column" }, children: [
1987
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { style: { flex: 1, overflowY: "auto", padding: 12, minHeight: 0 }, children: messages.length === 0 && !isStreaming ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { style: { display: "flex", flexDirection: "column", gap: 12 }, children: [
1988
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 8, marginBottom: 4 }, children: [
1989
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(LogoIcon, {}),
1990
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { style: { fontSize: 12, fontWeight: 600, color: "#6b7280" }, children: "Welcome to SEO Copilot! Ask anything about your content." })
1991
+ ] }),
1992
+ SUGGESTIONS.map((s, i) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1993
+ "button",
1994
+ {
1995
+ onClick: () => sendMessage(s),
1996
+ onMouseEnter: () => setHoveredSuggestion(i),
1997
+ onMouseLeave: () => setHoveredSuggestion(null),
1998
+ style: {
1999
+ width: "100%",
2000
+ textAlign: "left",
2001
+ padding: "10px 14px",
2002
+ borderRadius: 12,
2003
+ fontSize: 12.5,
2004
+ color: "#475569",
2005
+ background: hoveredSuggestion === i ? USER_BLUE : "#ffffff",
2006
+ border: `1px solid ${hoveredSuggestion === i ? USER_BLUE_BORDER : "#f1f5f9"}`,
2007
+ boxShadow: "0 1px 2px rgba(0,0,0,0.03)",
2008
+ cursor: "pointer",
2009
+ transition: "all 200ms ease"
2010
+ },
2011
+ children: s
2012
+ },
2013
+ i
2014
+ ))
2015
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { style: { display: "flex", flexDirection: "column", gap: 12 }, children: [
2016
+ messages.map((msg, i) => {
2017
+ if (msg.role === "assistant" && !msg.content) return null;
2018
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2019
+ "div",
2020
+ {
2021
+ style: {
2022
+ display: "flex",
2023
+ justifyContent: msg.role === "user" ? "flex-end" : "flex-start",
2024
+ animation: "fade-up 0.2s ease-out forwards"
2025
+ },
2026
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
2027
+ "div",
2028
+ {
2029
+ style: {
2030
+ maxWidth: "88%",
2031
+ padding: "10px 14px",
2032
+ fontSize: 12.5,
2033
+ lineHeight: 1.65,
2034
+ borderRadius: 18,
2035
+ ...msg.role === "user" ? {
2036
+ background: USER_BLUE,
2037
+ border: `1px solid ${USER_BLUE_BORDER}`,
2038
+ boxShadow: "0 1px 3px rgba(0,93,172,0.08)",
2039
+ borderBottomRightRadius: 6,
2040
+ color: "#1e293b"
2041
+ } : {
2042
+ background: "#ffffff",
2043
+ boxShadow: "0 1px 2px rgba(0,0,0,0.04)",
2044
+ borderBottomLeftRadius: 6,
2045
+ color: "#475569"
2046
+ }
2047
+ },
2048
+ children: [
2049
+ msg.role === "assistant" ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "prose-chat", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_react_markdown.default, { remarkPlugins: [import_remark_gfm.default], children: msg.content }) }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { style: { whiteSpace: "pre-wrap" }, children: msg.content }),
2050
+ isStreaming && i === messages.length - 1 && msg.role === "assistant" && msg.content && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { style: { display: "inline-flex", alignItems: "flex-end", gap: 2, height: 10, marginLeft: 4, verticalAlign: "middle" }, children: [1, 2, 3].map((n) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2051
+ "span",
2052
+ {
2053
+ style: {
2054
+ width: 2,
2055
+ height: 6,
2056
+ borderRadius: 1,
2057
+ background: MJH_GOLD,
2058
+ animation: `bar-shift-${n} 1.4s ease-in-out infinite`
2059
+ }
2060
+ },
2061
+ n
2062
+ )) })
2063
+ ]
2064
+ }
2065
+ )
2066
+ },
2067
+ i
2068
+ );
2069
+ }),
2070
+ showTypingBars && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { style: { display: "flex", justifyContent: "flex-start" }, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2071
+ "div",
2072
+ {
2073
+ style: {
2074
+ padding: 12,
2075
+ borderRadius: 16,
2076
+ borderBottomLeftRadius: 4,
2077
+ background: "#ffffff",
2078
+ boxShadow: "0 1px 3px rgba(0,0,0,0.06)"
2079
+ },
2080
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(LoadingBars, { size: "xs" })
2081
+ }
2082
+ ) }),
2083
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { ref: messagesEndRef })
2084
+ ] }) }),
2085
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
2086
+ "div",
2087
+ {
2088
+ style: {
2089
+ display: "flex",
2090
+ gap: 8,
2091
+ padding: 12,
2092
+ borderTop: "1px solid #f3f4f6",
2093
+ background: "rgba(255,255,255,0.9)",
2094
+ backdropFilter: "blur(8px)",
2095
+ flexShrink: 0
2096
+ },
2097
+ children: [
2098
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2099
+ "input",
2100
+ {
2101
+ type: "text",
2102
+ value: input,
2103
+ onChange: (e) => setInput(e.target.value),
2104
+ onKeyDown: handleKeyDown,
2105
+ onFocus: () => setInputFocused(true),
2106
+ onBlur: () => setInputFocused(false),
2107
+ placeholder: "Ask about SEO...",
2108
+ disabled: isStreaming,
2109
+ style: {
2110
+ flex: 1,
2111
+ padding: "8px 12px",
2112
+ fontSize: 12,
2113
+ borderRadius: 12,
2114
+ border: `1px solid ${inputFocused ? MJH_BLUE : "#e5e7eb"}`,
2115
+ background: "#ffffff",
2116
+ boxShadow: inputFocused ? "0 0 0 3px rgba(0,93,172,0.1)" : "0 1px 2px rgba(0,0,0,0.04)",
2117
+ outline: "none",
2118
+ opacity: isStreaming ? 0.4 : 1,
2119
+ transition: "border-color 150ms, box-shadow 150ms"
2120
+ }
2121
+ }
2122
+ ),
2123
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
2124
+ "button",
2125
+ {
2126
+ onClick: handleSend,
2127
+ disabled: isStreaming || !input.trim(),
2128
+ style: {
2129
+ padding: "8px 12px",
2130
+ borderRadius: 12,
2131
+ border: "none",
2132
+ color: "#ffffff",
2133
+ background: `linear-gradient(135deg, ${MJH_BLUE}, #004A8A)`,
2134
+ boxShadow: "0 2px 6px rgba(0,93,172,0.3)",
2135
+ cursor: isStreaming || !input.trim() ? "not-allowed" : "pointer",
2136
+ opacity: isStreaming || !input.trim() ? 0.4 : 1,
2137
+ transition: "all 150ms",
2138
+ display: "flex",
2139
+ alignItems: "center"
2140
+ },
2141
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("svg", { width: "16", height: "16", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("path", { d: "M3.105 2.289a.75.75 0 00-.826.95l1.414 4.925A1.5 1.5 0 005.135 9.25h6.115a.75.75 0 010 1.5H5.135a1.5 1.5 0 00-1.442 1.086l-1.414 4.926a.75.75 0 00.826.95 28.896 28.896 0 0015.293-7.154.75.75 0 000-1.115A28.897 28.897 0 003.105 2.289z" }) })
2142
+ }
2143
+ )
2144
+ ]
2145
+ }
2146
+ ),
2147
+ messages.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { style: { display: "flex", justifyContent: "flex-end", padding: "0 12px 8px", flexShrink: 0 }, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
2148
+ "button",
2149
+ {
2150
+ onClick: clearChat,
2151
+ onMouseEnter: () => setClearHovered(true),
2152
+ onMouseLeave: () => setClearHovered(false),
2153
+ style: {
2154
+ background: "none",
2155
+ border: "none",
2156
+ fontSize: 10,
2157
+ color: clearHovered ? "#ef4444" : "#d1d5db",
2158
+ cursor: "pointer",
2159
+ display: "flex",
2160
+ alignItems: "center",
2161
+ gap: 4,
2162
+ transition: "color 150ms"
2163
+ },
2164
+ children: [
2165
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("path", { fillRule: "evenodd", d: "M8.75 1A2.75 2.75 0 006 3.75v.443c-.795.077-1.584.176-2.365.298a.75.75 0 10.23 1.482l.149-.022.841 10.518A2.75 2.75 0 007.596 19h4.807a2.75 2.75 0 002.742-2.53l.841-10.519.149.023a.75.75 0 00.23-1.482A41.03 41.03 0 0014 4.193V3.75A2.75 2.75 0 0011.25 1h-2.5zM10 4c.84 0 1.673.025 2.5.075V3.75c0-.69-.56-1.25-1.25-1.25h-2.5c-.69 0-1.25.56-1.25 1.25v.325C8.327 4.025 9.16 4 10 4zM8.58 7.72a.75.75 0 00-1.5.06l.3 7.5a.75.75 0 101.5-.06l-.3-7.5zm4.34.06a.75.75 0 10-1.5-.06l-.3 7.5a.75.75 0 101.5.06l.3-7.5z", clipRule: "evenodd" }) }),
2166
+ "Clear"
2167
+ ]
2168
+ }
2169
+ ) })
2170
+ ] }) });
2171
+ }
2172
+
2173
+ // src/components/SEOCopilotPanel.tsx
2174
+ var import_jsx_runtime7 = require("react/jsx-runtime");
2175
+ function LogoIcon2() {
2176
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", children: [
2177
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("rect", { x: "1", y: "3", width: "4", height: "10", rx: "1", fill: MJH_GOLD, opacity: "1" }),
2178
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("rect", { x: "6", y: "1", width: "4", height: "14", rx: "1", fill: MJH_GOLD, opacity: "0.85" }),
2179
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("rect", { x: "11", y: "5", width: "4", height: "8", rx: "1", fill: MJH_GOLD, opacity: "0.6" })
2180
+ ] });
2181
+ }
2182
+ function PanelContent({ initialPublication, isFloating, isFullscreen, onToggleFloat, onToggleFullscreen, onClose, floatPos: _floatPos, onDragStart, seedKeywords, onSeedsChange, text, documentFields, onSelectPrimary, externalAnalysis }) {
2183
+ const publication = initialPublication;
2184
+ const [keywordsOpen, setKeywordsOpen] = (0, import_react10.useState)(true);
2185
+ const [chatOpen, setChatOpen] = (0, import_react10.useState)(true);
2186
+ const isLoading = externalAnalysis.isLoading;
2187
+ const keywordAnalysisState = externalAnalysis;
2188
+ const keywordPanelData = (0, import_react10.useMemo)(() => {
2189
+ const { analysis } = keywordAnalysisState;
2190
+ if (!analysis) return void 0;
2191
+ return {
2192
+ primaryKeyword: { term: analysis.primaryKeyword.term, volume: analysis.primaryKeyword.volume },
2193
+ supportingKeywords: (analysis.supportingKeywords || []).map((kw) => ({ term: kw.term, volume: kw.volume })),
2194
+ missingKeywords: analysis.missingKeywords.map((kw) => ({ term: kw.term, volume: kw.volume })),
2195
+ aeoData: keywordAnalysisState.deepAnalysis?.aeo ? {
2196
+ questionHeadings: (keywordAnalysisState.deepAnalysis.aeo.questionHeadings || []).map((h) => ({
2197
+ suggestedHeading: h.suggestedHeading,
2198
+ rationale: h.rationale
2199
+ })),
2200
+ faqSuggestions: (keywordAnalysisState.deepAnalysis.aeo.faqSuggestions || []).map((f) => ({
2201
+ question: f.question,
2202
+ answer: f.answer
2203
+ }))
2204
+ } : void 0
2205
+ };
2206
+ }, [keywordAnalysisState.analysis, keywordAnalysisState.deepAnalysis]);
2207
+ const [splitPercent, setSplitPercent] = (0, import_react10.useState)(75);
2208
+ const containerRef = (0, import_react10.useRef)(null);
2209
+ const dragging = (0, import_react10.useRef)(false);
2210
+ const onMouseDown = (0, import_react10.useCallback)(() => {
2211
+ dragging.current = true;
2212
+ document.body.style.cursor = "row-resize";
2213
+ document.body.style.userSelect = "none";
2214
+ }, []);
2215
+ (0, import_react10.useEffect)(() => {
2216
+ const onMouseMove = (e) => {
2217
+ if (!dragging.current || !containerRef.current) return;
2218
+ const rect = containerRef.current.getBoundingClientRect();
2219
+ const pct = (e.clientY - rect.top) / rect.height * 100;
2220
+ setSplitPercent(Math.min(85, Math.max(15, pct)));
2221
+ };
2222
+ const onMouseUp = () => {
2223
+ dragging.current = false;
2224
+ document.body.style.cursor = "";
2225
+ document.body.style.userSelect = "";
2226
+ };
2227
+ window.addEventListener("mousemove", onMouseMove);
2228
+ window.addEventListener("mouseup", onMouseUp);
2229
+ return () => {
2230
+ window.removeEventListener("mousemove", onMouseMove);
2231
+ window.removeEventListener("mouseup", onMouseUp);
2232
+ };
2233
+ }, []);
2234
+ const headerCursor = isFloating ? "grab" : "default";
2235
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "seo-copilot-panel", style: { height: "100%", display: "flex", flexDirection: "column", background: "#FBFAF7", fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif", opacity: 1 }, children: [
2236
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("style", { dangerouslySetInnerHTML: { __html: INJECTED_CSS } }),
2237
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
2238
+ "div",
2239
+ {
2240
+ onMouseDown: isFloating ? onDragStart : void 0,
2241
+ style: {
2242
+ position: "relative",
2243
+ display: "flex",
2244
+ alignItems: "center",
2245
+ justifyContent: "space-between",
2246
+ padding: "14px 16px",
2247
+ background: "linear-gradient(180deg, #FFFFFF 0%, #FBFAF7 100%)",
2248
+ boxShadow: "0 1px 0 rgba(0,0,0,0.04)",
2249
+ zIndex: 10,
2250
+ cursor: headerCursor,
2251
+ userSelect: "none",
2252
+ flexShrink: 0
2253
+ },
2254
+ children: [
2255
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: 6 }, children: [
2256
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(LogoIcon2, {}),
2257
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("span", { style: { fontWeight: 700, fontSize: 15, letterSpacing: "-0.01em" }, children: [
2258
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { style: { color: MJH_GOLD }, children: "SEO" }),
2259
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { style: { color: MJH_SLATE }, children: " copilot" })
2260
+ ] })
2261
+ ] }),
2262
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { display: "flex", alignItems: "center", gap: isFloating ? 4 : 6 }, children: [
2263
+ isFloating && onToggleFullscreen && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2264
+ "button",
2265
+ {
2266
+ onClick: (e) => {
2267
+ e.stopPropagation();
2268
+ onToggleFullscreen();
2269
+ },
2270
+ onMouseDown: (e) => e.stopPropagation(),
2271
+ style: {
2272
+ background: isFullscreen ? "rgba(230,192,27,0.15)" : "rgba(0,0,0,0.04)",
2273
+ border: "none",
2274
+ borderRadius: 6,
2275
+ cursor: "pointer",
2276
+ padding: "4px 8px",
2277
+ display: "flex",
2278
+ alignItems: "center",
2279
+ gap: 4,
2280
+ color: isFullscreen ? "#8B7310" : "#6b7280",
2281
+ fontSize: 10,
2282
+ fontWeight: 600,
2283
+ transition: "all 150ms"
2284
+ },
2285
+ children: isFullscreen ? /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_jsx_runtime7.Fragment, { children: [
2286
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { d: "M3.28 2.22a.75.75 0 00-1.06 1.06L5.44 6.5H3.75a.75.75 0 000 1.5h4.5A.75.75 0 009 7.25v-4.5a.75.75 0 00-1.5 0v1.69L4.28 1.22a.75.75 0 00-1 1zM16.72 17.78a.75.75 0 001.06-1.06L14.56 13.5h1.69a.75.75 0 000-1.5h-4.5a.75.75 0 00-.75.75v4.5a.75.75 0 001.5 0v-1.69l3.22 3.22z" }) }),
2287
+ "Exit fullscreen"
2288
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_jsx_runtime7.Fragment, { children: [
2289
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { d: "M13.28 7.78l3.22-3.22v1.69a.75.75 0 001.5 0v-4.5a.75.75 0 00-.75-.75h-4.5a.75.75 0 000 1.5h1.69l-3.22 3.22a.75.75 0 001.06 1.06zM2 17.25a.75.75 0 001.5 0v-1.69l3.22 3.22a.75.75 0 001.06-1.06L4.56 14.5h1.69a.75.75 0 000-1.5h-4.5a.75.75 0 00-.75.75v4.5z" }) }),
2290
+ "Fullscreen"
2291
+ ] })
2292
+ }
2293
+ ),
2294
+ isFloating && onToggleFloat && /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
2295
+ "button",
2296
+ {
2297
+ onClick: (e) => {
2298
+ e.stopPropagation();
2299
+ onToggleFloat();
2300
+ },
2301
+ onMouseDown: (e) => e.stopPropagation(),
2302
+ style: {
2303
+ background: "rgba(0,93,172,0.08)",
2304
+ border: "none",
2305
+ borderRadius: 6,
2306
+ cursor: "pointer",
2307
+ padding: "4px 8px",
2308
+ display: "flex",
2309
+ alignItems: "center",
2310
+ gap: 4,
2311
+ color: MJH_BLUE,
2312
+ fontSize: 10,
2313
+ fontWeight: 600,
2314
+ transition: "all 150ms"
2315
+ },
2316
+ children: [
2317
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { fillRule: "evenodd", d: "M4.25 2A2.25 2.25 0 002 4.25v11.5A2.25 2.25 0 004.25 18h11.5A2.25 2.25 0 0018 15.75V4.25A2.25 2.25 0 0015.75 2H4.25zM3.5 4.25a.75.75 0 01.75-.75h11.5a.75.75 0 01.75.75v11.5a.75.75 0 01-.75.75H4.25a.75.75 0 01-.75-.75V4.25z", clipRule: "evenodd" }) }),
2318
+ "Dock"
2319
+ ]
2320
+ }
2321
+ ),
2322
+ !isFloating && onToggleFloat && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2323
+ "button",
2324
+ {
2325
+ onClick: (e) => {
2326
+ e.stopPropagation();
2327
+ onToggleFloat();
2328
+ },
2329
+ onMouseDown: (e) => e.stopPropagation(),
2330
+ style: {
2331
+ background: "none",
2332
+ border: "none",
2333
+ cursor: "pointer",
2334
+ padding: 2,
2335
+ display: "flex",
2336
+ alignItems: "center",
2337
+ color: "#9ca3af",
2338
+ transition: "color 150ms"
2339
+ },
2340
+ title: "Pop out",
2341
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("svg", { width: "14", height: "14", viewBox: "0 0 20 20", fill: "currentColor", children: [
2342
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { d: "M4.25 5.5a.75.75 0 00-.75.75v8.5c0 .414.336.75.75.75h8.5a.75.75 0 00.75-.75v-4a.75.75 0 011.5 0v4A2.25 2.25 0 0112.75 17h-8.5A2.25 2.25 0 012 14.75v-8.5A2.25 2.25 0 014.25 4h5a.75.75 0 010 1.5h-5z" }),
2343
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { d: "M6.194 12.753a.75.75 0 001.06.053L16.5 4.44v2.81a.75.75 0 001.5 0v-4.5a.75.75 0 00-.75-.75h-4.5a.75.75 0 000 1.5h2.553l-9.056 8.194a.75.75 0 00-.053 1.06z" })
2344
+ ] })
2345
+ }
2346
+ ),
2347
+ isLoading && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: { width: 8, height: 8, borderRadius: "50%", background: MJH_GOLD, animation: "content-pulse 2s ease-in-out infinite" } }),
2348
+ onClose && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2349
+ "button",
2350
+ {
2351
+ onClick: (e) => {
2352
+ e.stopPropagation();
2353
+ onClose();
2354
+ },
2355
+ onMouseDown: (e) => e.stopPropagation(),
2356
+ style: {
2357
+ background: "none",
2358
+ border: "none",
2359
+ cursor: "pointer",
2360
+ padding: 2,
2361
+ display: "flex",
2362
+ alignItems: "center",
2363
+ color: "#9ca3af",
2364
+ transition: "color 150ms"
2365
+ },
2366
+ title: "Close",
2367
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("svg", { width: "14", height: "14", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { d: "M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" }) })
2368
+ }
2369
+ )
2370
+ ] }),
2371
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: {
2372
+ position: "absolute",
2373
+ bottom: 3,
2374
+ left: 38,
2375
+ fontSize: 9,
2376
+ color: "#b0b8c4",
2377
+ fontWeight: 400,
2378
+ fontStyle: "italic",
2379
+ letterSpacing: "0.02em"
2380
+ }, children: "More eyes on your expertise" })
2381
+ ]
2382
+ }
2383
+ ),
2384
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { ref: containerRef, style: { flex: 1, display: "flex", flexDirection: "column", overflow: "hidden" }, children: [
2385
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { height: keywordsOpen ? chatOpen ? `${splitPercent}%` : "100%" : "auto", overflow: keywordsOpen ? "auto" : "hidden", flexShrink: keywordsOpen ? void 0 : 0 }, children: [
2386
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
2387
+ "button",
2388
+ {
2389
+ onClick: () => setKeywordsOpen(!keywordsOpen),
2390
+ style: { width: "100%", padding: "4px 12px", display: "flex", alignItems: "center", justifyContent: "space-between", background: "transparent", border: "none", borderBottom: "1px solid rgba(0,0,0,0.06)", cursor: "pointer" },
2391
+ children: [
2392
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("span", { style: { display: "flex", alignItems: "center", gap: 6, flex: 1 }, children: [
2393
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { style: { fontSize: 10, fontWeight: 600, textTransform: "uppercase", letterSpacing: "0.05em", color: "#9ca3af" }, children: "Keywords & AEO Insights" }),
2394
+ (keywordAnalysisState.isLoading || keywordAnalysisState.isVolumesLoading || keywordAnalysisState.isDeepLoading) && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(LoadingBars, { size: "xs", color: "#9ca3af" })
2395
+ ] }),
2396
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "#9ca3af", style: { transform: keywordsOpen ? "rotate(0deg)" : "rotate(-90deg)", transition: "transform 200ms" }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { fillRule: "evenodd", d: "M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z", clipRule: "evenodd" }) })
2397
+ ]
2398
+ }
2399
+ ),
2400
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: { display: keywordsOpen ? "block" : "none" }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2401
+ KeywordPanel,
2402
+ {
2403
+ text,
2404
+ publication,
2405
+ seedKeywords,
2406
+ onSeedsChange,
2407
+ onSelectPrimary,
2408
+ documentFields,
2409
+ externalAnalysis: keywordAnalysisState
2410
+ }
2411
+ ) })
2412
+ ] }),
2413
+ keywordsOpen && chatOpen && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2414
+ "div",
2415
+ {
2416
+ onMouseDown,
2417
+ style: {
2418
+ height: 6,
2419
+ cursor: "row-resize",
2420
+ background: "linear-gradient(180deg, rgba(0,0,0,0.04) 0%, rgba(0,0,0,0.08) 50%, rgba(0,0,0,0.04) 100%)",
2421
+ display: "flex",
2422
+ alignItems: "center",
2423
+ justifyContent: "center",
2424
+ flexShrink: 0
2425
+ },
2426
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: { width: 32, height: 2, borderRadius: 1, background: "#d1d5db" } })
2427
+ }
2428
+ ),
2429
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: {
2430
+ flex: chatOpen && !keywordsOpen ? 1 : void 0,
2431
+ height: chatOpen ? keywordsOpen ? `${100 - splitPercent}%` : void 0 : "auto",
2432
+ overflow: "hidden",
2433
+ display: "flex",
2434
+ flexDirection: "column",
2435
+ flexShrink: chatOpen ? void 0 : 0,
2436
+ minHeight: 0
2437
+ }, children: [
2438
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
2439
+ "button",
2440
+ {
2441
+ onClick: () => setChatOpen(!chatOpen),
2442
+ style: { width: "100%", padding: "4px 12px", display: "flex", alignItems: "center", justifyContent: "space-between", background: "transparent", border: "none", borderBottom: "1px solid rgba(0,0,0,0.06)", cursor: "pointer" },
2443
+ children: [
2444
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { style: { fontSize: 10, fontWeight: 600, textTransform: "uppercase", letterSpacing: "0.05em", color: "#9ca3af" }, children: "Chat" }),
2445
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "#9ca3af", style: { transform: chatOpen ? "rotate(0deg)" : "rotate(-90deg)", transition: "transform 200ms" }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { fillRule: "evenodd", d: "M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z", clipRule: "evenodd" }) })
2446
+ ]
2447
+ }
2448
+ ),
2449
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: { flex: chatOpen ? 1 : 0, overflow: "hidden", display: chatOpen ? "flex" : "none", flexDirection: "column", minHeight: 0 }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ChatPanel, { text, publication, keywordPanelData }) })
2450
+ ] })
2451
+ ] })
2452
+ ] });
2453
+ }
2454
+ function SEOCopilotInner({ config, onClose }) {
2455
+ const [isFloating, setIsFloating] = (0, import_react10.useState)(false);
2456
+ const [isFullscreen, setIsFullscreen] = (0, import_react10.useState)(false);
2457
+ const [floatPos, setFloatPos] = (0, import_react10.useState)({ x: typeof window !== "undefined" ? window.innerWidth - 420 : 400, y: 80 });
2458
+ const [floatSize, setFloatSize] = (0, import_react10.useState)({ width: 380, height: typeof window !== "undefined" ? Math.round(window.innerHeight * 0.8) : 600 });
2459
+ const dragOffset = (0, import_react10.useRef)(null);
2460
+ const resizeStart = (0, import_react10.useRef)(null);
2461
+ const { text, isLoading: docLoading, documentFields, documentId } = useDocumentContent();
2462
+ const { apiUrl } = useConfig();
2463
+ const seedCacheKey = documentId ? `seo-copilot-cache-${documentId}` : "seo-copilot-cache-v2";
2464
+ const [seedKeywords, setSeedKeywordsRaw] = (0, import_react10.useState)(() => {
2465
+ try {
2466
+ const c = sessionStorage.getItem(seedCacheKey);
2467
+ if (c) return JSON.parse(c).seeds || [];
2468
+ } catch {
2469
+ }
2470
+ return [];
2471
+ });
2472
+ const setSeedKeywords = (0, import_react10.useCallback)((seeds) => {
2473
+ setSeedKeywordsRaw(seeds);
2474
+ try {
2475
+ const c = JSON.parse(sessionStorage.getItem(seedCacheKey) || "{}");
2476
+ sessionStorage.setItem(seedCacheKey, JSON.stringify({ ...c, seeds }));
2477
+ } catch {
2478
+ }
2479
+ }, [seedCacheKey]);
2480
+ const [selectedPrimary, setSelectedPrimary] = (0, import_react10.useState)();
2481
+ const keywordAnalysisState = useKeywordAnalysis(text, apiUrl, config.publication, seedKeywords, documentFields, selectedPrimary, documentId);
2482
+ const handleDragStart = (0, import_react10.useCallback)((e) => {
2483
+ dragOffset.current = {
2484
+ dx: e.clientX - floatPos.x,
2485
+ dy: e.clientY - floatPos.y
2486
+ };
2487
+ const onMouseMove = (ev) => {
2488
+ if (!dragOffset.current) return;
2489
+ setFloatPos({
2490
+ x: ev.clientX - dragOffset.current.dx,
2491
+ y: ev.clientY - dragOffset.current.dy
2492
+ });
2493
+ };
2494
+ const onMouseUp = () => {
2495
+ dragOffset.current = null;
2496
+ window.removeEventListener("mousemove", onMouseMove);
2497
+ window.removeEventListener("mouseup", onMouseUp);
2498
+ document.body.style.cursor = "";
2499
+ document.body.style.userSelect = "";
2500
+ };
2501
+ document.body.style.cursor = "grabbing";
2502
+ document.body.style.userSelect = "none";
2503
+ window.addEventListener("mousemove", onMouseMove);
2504
+ window.addEventListener("mouseup", onMouseUp);
2505
+ }, [floatPos]);
2506
+ const handleResizeStart = (0, import_react10.useCallback)((e) => {
2507
+ e.preventDefault();
2508
+ e.stopPropagation();
2509
+ resizeStart.current = { startX: e.clientX, startY: e.clientY, startW: floatSize.width, startH: floatSize.height };
2510
+ document.body.style.cursor = "nwse-resize";
2511
+ document.body.style.userSelect = "none";
2512
+ const onMouseMove = (ev) => {
2513
+ if (!resizeStart.current) return;
2514
+ const newW = Math.max(320, resizeStart.current.startW + (ev.clientX - resizeStart.current.startX));
2515
+ const newH = Math.max(300, resizeStart.current.startH + (ev.clientY - resizeStart.current.startY));
2516
+ setFloatSize({ width: newW, height: newH });
2517
+ };
2518
+ const onMouseUp = () => {
2519
+ resizeStart.current = null;
2520
+ document.body.style.cursor = "";
2521
+ document.body.style.userSelect = "";
2522
+ window.removeEventListener("mousemove", onMouseMove);
2523
+ window.removeEventListener("mouseup", onMouseUp);
2524
+ };
2525
+ window.addEventListener("mousemove", onMouseMove);
2526
+ window.addEventListener("mouseup", onMouseUp);
2527
+ }, [floatSize]);
2528
+ void docLoading;
2529
+ const panelContent = /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2530
+ PanelContent,
2531
+ {
2532
+ initialPublication: config.publication,
2533
+ isFloating,
2534
+ isFullscreen,
2535
+ onToggleFloat: () => {
2536
+ setIsFloating((v) => !v);
2537
+ setIsFullscreen(false);
2538
+ },
2539
+ onToggleFullscreen: () => setIsFullscreen((v) => !v),
2540
+ onClose,
2541
+ floatPos,
2542
+ onDragStart: handleDragStart,
2543
+ seedKeywords,
2544
+ onSeedsChange: setSeedKeywords,
2545
+ text,
2546
+ documentFields,
2547
+ onSelectPrimary: setSelectedPrimary,
2548
+ externalAnalysis: {
2549
+ analysis: keywordAnalysisState.analysis,
2550
+ isLoading: keywordAnalysisState.isLoading,
2551
+ isVolumesLoading: keywordAnalysisState.isVolumesLoading,
2552
+ error: keywordAnalysisState.error,
2553
+ deepAnalysis: keywordAnalysisState.deepAnalysis,
2554
+ isDeepLoading: keywordAnalysisState.isDeepLoading,
2555
+ runDeepAnalysis: keywordAnalysisState.runDeepAnalysis,
2556
+ refreshVolumes: keywordAnalysisState.refreshVolumes,
2557
+ aeoContentChanged: keywordAnalysisState.aeoContentChanged
2558
+ }
2559
+ }
2560
+ );
2561
+ const floatingPanel = /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
2562
+ "div",
2563
+ {
2564
+ style: {
2565
+ position: "fixed",
2566
+ left: isFullscreen ? 0 : floatPos.x,
2567
+ top: isFullscreen ? 0 : floatPos.y,
2568
+ width: isFullscreen ? "100vw" : floatSize.width,
2569
+ height: isFullscreen ? "100vh" : floatSize.height,
2570
+ zIndex: 2147483647,
2571
+ borderRadius: isFullscreen ? 0 : 12,
2572
+ overflow: "hidden",
2573
+ boxShadow: isFullscreen ? "none" : "0 8px 32px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.10)",
2574
+ border: isFullscreen ? "none" : "1px solid rgba(0,0,0,0.08)",
2575
+ background: "#FBFAF7"
2576
+ },
2577
+ children: [
2578
+ panelContent,
2579
+ !isFullscreen && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2580
+ "div",
2581
+ {
2582
+ onMouseDown: handleResizeStart,
2583
+ style: {
2584
+ position: "absolute",
2585
+ bottom: 0,
2586
+ right: 0,
2587
+ width: 16,
2588
+ height: 16,
2589
+ cursor: "nwse-resize",
2590
+ display: "flex",
2591
+ alignItems: "center",
2592
+ justifyContent: "center"
2593
+ },
2594
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("svg", { width: "10", height: "10", viewBox: "0 0 10 10", fill: "#9ca3af", children: [
2595
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { d: "M9 1v8H1", fill: "none", stroke: "#9ca3af", strokeWidth: "1.5", strokeLinecap: "round" }),
2596
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("path", { d: "M9 5v4H5", fill: "none", stroke: "#9ca3af", strokeWidth: "1.5", strokeLinecap: "round" })
2597
+ ] })
2598
+ }
2599
+ )
2600
+ ]
2601
+ }
2602
+ );
2603
+ if (isFloating) {
2604
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_jsx_runtime7.Fragment, { children: [
2605
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: { height: "100%", background: "#FBFAF7", display: "flex", alignItems: "center", justifyContent: "center" }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { style: { fontSize: 12, color: "#9ca3af" }, children: "SEO Copilot is floating" }) }),
2606
+ typeof document !== "undefined" && (0, import_react_dom.createPortal)(floatingPanel, document.body)
2607
+ ] });
2608
+ }
2609
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: { height: "100%", background: "#FBFAF7", position: "relative", zIndex: 1 }, children: panelContent });
2610
+ }
2611
+ function SEOCopilotPanel({ config, onClose }) {
2612
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2613
+ import_react10.Suspense,
2614
+ {
2615
+ fallback: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { padding: 16, display: "flex", alignItems: "center", gap: 12 }, children: [
2616
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(LoadingBars, {}),
2617
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { style: { fontSize: 12, color: "#9ca3af" }, children: "Loading SEO Copilot..." })
2618
+ ] }),
2619
+ children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ConfigProvider, { config, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(SEOCopilotInner, { config, onClose }) })
2620
+ }
2621
+ );
2622
+ }
2623
+ function LoadingBars({ size = "sm", color = MJH_GOLD }) {
2624
+ const barWidth = size === "xs" ? 4 : 6;
2625
+ const containerHeight = size === "xs" ? 10 : 14;
2626
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: { display: "inline-flex", alignItems: "flex-end", gap: 2, height: containerHeight }, children: [1, 2, 3].map((i) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2627
+ "div",
2628
+ {
2629
+ style: {
2630
+ width: barWidth,
2631
+ height: 8,
2632
+ borderRadius: 2,
2633
+ background: color,
2634
+ animation: `bar-shift-${i} 1.4s ease-in-out infinite`
2635
+ }
2636
+ },
2637
+ i
2638
+ )) });
2639
+ }
2640
+
2641
+ // src/inspector.ts
2642
+ function SEOIcon() {
2643
+ return (0, import_react11.createElement)(
2644
+ "svg",
2645
+ { viewBox: "0 0 25 25", width: "1em", height: "1em", fill: "none" },
2646
+ (0, import_react11.createElement)("circle", { cx: "12.5", cy: "12.5", r: "11.5", fill: "#E6C01B" }),
2647
+ (0, import_react11.createElement)("text", {
2648
+ x: "12.5",
2649
+ y: "7.5",
2650
+ textAnchor: "middle",
2651
+ dominantBaseline: "central",
2652
+ fill: "white",
2653
+ fontSize: "7",
2654
+ fontWeight: "900",
2655
+ fontFamily: "Arial Black, Impact, sans-serif"
2656
+ }, "S"),
2657
+ (0, import_react11.createElement)("text", {
2658
+ x: "12.5",
2659
+ y: "12.8",
2660
+ textAnchor: "middle",
2661
+ dominantBaseline: "central",
2662
+ fill: "white",
2663
+ fontSize: "7",
2664
+ fontWeight: "900",
2665
+ fontFamily: "Arial Black, Impact, sans-serif"
2666
+ }, "E"),
2667
+ (0, import_react11.createElement)("text", {
2668
+ x: "12.5",
2669
+ y: "18",
2670
+ textAnchor: "middle",
2671
+ dominantBaseline: "central",
2672
+ fill: "white",
2673
+ fontSize: "7",
2674
+ fontWeight: "900",
2675
+ fontFamily: "Arial Black, Impact, sans-serif"
2676
+ }, "O")
2677
+ );
2678
+ }
2679
+ function seoCopilotInspector(config) {
2680
+ return {
2681
+ name: "seo-copilot",
2682
+ useMenuItem: () => ({
2683
+ title: "SEO Copilot",
2684
+ icon: SEOIcon,
2685
+ showAsAction: true
2686
+ }),
2687
+ component: function SEOCopilotInspector(props) {
2688
+ return (0, import_react11.createElement)(SEOCopilotPanel, { ...props, config });
2689
+ }
2690
+ };
2691
+ }
2692
+
2693
+ // src/index.ts
2694
+ var seoCopilot = (0, import_sanity.definePlugin)((config) => ({
2695
+ name: "seo-copilot",
2696
+ document: {
2697
+ inspectors: (prev) => {
2698
+ return [...prev, seoCopilotInspector(config)];
2699
+ }
2700
+ }
2701
+ }));
2702
+ // Annotate the CommonJS export names for ESM import in node:
2703
+ 0 && (module.exports = {
2704
+ seoCopilot
2705
+ });
2706
+ //# sourceMappingURL=index.js.map