import React, { useState } from 'react'; import { ExcelRow, COLUMNS } from '../types'; import { X, Sparkles, Save, Loader2 } from 'lucide-react'; import { generateDescription } from '../services/anthropic'; import { cn } from '../lib/utils'; interface EditPanelProps { row: ExcelRow; rowIndex: number; onSave: (rowIndex: number, updatedRow: ExcelRow) => void; onClose: () => void; } export function EditPanel({ row, rowIndex, onSave, onClose }: EditPanelProps) { const [formData, setFormData] = useState({ longDe: row[COLUMNS.LONG_DE] || '', longEn: row[COLUMNS.LONG_EN] || '', shortDe: row[COLUMNS.SHORT_DE] || '', shortEn: row[COLUMNS.SHORT_EN] || '', }); const [loadingField, setLoadingField] = useState(null); const [error, setError] = useState(null); const isModified = (field: keyof typeof formData) => { const colMap = { longDe: COLUMNS.LONG_DE, longEn: COLUMNS.LONG_EN, shortDe: COLUMNS.SHORT_DE, shortEn: COLUMNS.SHORT_EN, }; return formData[field] !== (row[colMap[field]] || ''); }; const handleGenerate = async (field: keyof typeof formData) => { setLoadingField(field); setError(null); try { let prompt = ''; const baseContext = `Article Name: ${row[COLUMNS.ARTICLE_NAME]}\nArticle Details (EN): ${row[COLUMNS.DETAILS_EN] || 'N/A'}\nArticle Details (DE): ${row[COLUMNS.DETAILS_DE] || 'N/A'}`; const systemPrompt = "You are a professional copywriter for CRAZE GmbH, a German toy company. Write commercial product descriptions for B2B buyers (retailers, distributors). Use clear, engaging, professional language. Never invent features not mentioned in the source material."; if (field === 'longDe') { prompt = `Based on the following product details, generate a long commercial description in German. It should be fluent, oriented towards B2B toy buyers, 3-5 paragraphs long. Include features, benefits, and material if available.\n\n${baseContext}`; } else if (field === 'longEn') { if (formData.longDe) { prompt = `Translate the following German product description into perfect English. Keep the same tone and length.\n\nGerman Description:\n${formData.longDe}`; } else { prompt = `Based on the following product details, generate a long commercial description in English. It should be fluent, oriented towards B2B toy buyers, 3-5 paragraphs long. Include features, benefits, and material if available.\n\n${baseContext}`; } } else if (field === 'shortDe') { if (formData.longDe) { prompt = `Create a short version (2-4 sentences max) of the following German product description. Include only the most relevant info. Use a direct and commercial tone.\n\nGerman Description:\n${formData.longDe}`; } else { prompt = `Based on the following product details, generate a short commercial description in German (2-4 sentences max). Include only the most relevant info. Use a direct and commercial tone.\n\n${baseContext}`; } } else if (field === 'shortEn') { if (formData.shortDe) { prompt = `Translate the following short German product description into perfect English.\n\nGerman Description:\n${formData.shortDe}`; } else { prompt = `Based on the following product details, generate a short commercial description in English (2-4 sentences max). Include only the most relevant info. Use a direct and commercial tone.\n\n${baseContext}`; } } const generatedText = await generateDescription(prompt, systemPrompt); setFormData(prev => ({ ...prev, [field]: generatedText.trim() })); } catch (err: any) { setError(err.message || 'An error occurred during generation.'); } finally { setLoadingField(null); } }; const handleSave = () => { const newRow = [...row]; newRow[COLUMNS.LONG_DE] = formData.longDe; newRow[COLUMNS.LONG_EN] = formData.longEn; newRow[COLUMNS.SHORT_DE] = formData.shortDe; newRow[COLUMNS.SHORT_EN] = formData.shortEn; onSave(rowIndex, newRow); }; const FieldEditor = ({ title, field }: { title: string, field: keyof typeof formData }) => (