import React, { useState } from 'react'; // Custom SVG Icons to ensure zero external dependencies and perfect rendering const Icons = { Sparkles: ({ className = "w-5 h-5" }) => ( ), Loader: ({ className = "w-5 h-5 animate-spin" }) => ( ), Copy: ({ className = "w-5 h-5" }) => ( ), Check: ({ className = "w-5 h-5" }) => ( ), Word: ({ className = "w-5 h-5" }) => ( ), Link: ({ className = "w-5 h-5" }) => ( ), Settings: ({ className = "w-5 h-5" }) => ( ) }; export default function AffiliateScriptApp() { const [url, setUrl] = useState(''); const [count, setCount] = useState('3'); const [style, setStyle] = useState('Santai & Kekinian (Gaul)'); const [length, setLength] = useState('Sedang (30-60 detik)'); const [isGenerating, setIsGenerating] = useState(false); const [results, setResults] = useState([]); const [error, setError] = useState(''); const [copiedIndex, setCopiedIndex] = useState(null); const styleOptions = [ 'Santai & Kekinian (Gaul)', 'Formal & Profesional', 'Persuasif & Hard Selling (Langsung jualan)', 'Storytelling / Curhat (Soft selling)', 'FOMO / Mendesak (Bikin takut kehabisan)', 'Lucu & Menghibur (Komedi)' ]; const lengthOptions = [ 'Pendek (< 30 detik / Singkat, padat, jelas)', 'Sedang (30 - 60 detik / Pas untuk TikTok/Reels)', 'Panjang (> 60 detik / Edukatif & Detail)' ]; const handleGenerate = async () => { if (!url.trim()) { setError('Masukkan link produk terlebih dahulu!'); return; } setIsGenerating(true); setError(''); setResults([]); try { const apiKey = ""; // API Key handled by environment const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent?key=${apiKey}`; const systemPrompt = `Kamu adalah copywriter affiliate marketing profesional kelas atas untuk platform seperti TikTok, Instagram Reels, dan Shopee Video. Keahlianmu adalah membuat hook yang mematikan, isi yang engage, dan Call to Action (CTA) yang memaksa penonton untuk klik keranjang kuning atau link di bio.`; const userPrompt = `Buatkan ${count} variasi script konten video affiliate marketing untuk produk dari link berikut:\n${url}\n\nPanduan Ketat:\n1. Bahasa: Indonesia.\n2. Gaya/Nada Bahasa: ${style}.\n3. Panjang tulisan: ${length}.\n4. Format: Berikan output murni narasi/script yang siap dibaca (boleh tambah instruksi visual di dalam kurung siku seperti [Tunjuk keranjang kuning] tapi fokus utamanya adalah teks narasi).\n5. Jika link tidak bisa diakses/dibaca langsung, tebak produk dari URL-nya ATAU buatkan template super engaging yang relevan di mana kreator bisa dengan mudah mengisi detail spesifik produk mereka.`; const payload = { contents: [{ parts: [{ text: userPrompt }] }], systemInstruction: { parts: [{ text: systemPrompt }] }, generationConfig: { responseMimeType: "application/json", responseSchema: { type: "ARRAY", items: { type: "STRING" }, description: "Array berisi variasi script konten affiliate marketing" } } }; const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) { throw new Error('Gagal terhubung ke server AI.'); } const result = await response.json(); if (result.candidates && result.candidates.length > 0) { const jsonText = result.candidates[0].content.parts[0].text; const parsedResults = JSON.parse(jsonText); setResults(parsedResults); } else { throw new Error('Format respon tidak sesuai.'); } } catch (err) { console.error(err); setError('Terjadi kesalahan saat membuat script. Silakan coba lagi. Pastikan link dapat diakses atau deskripsi URL jelas.'); } finally { setIsGenerating(false); } }; const handleCopy = (text, index) => { // Creating a temporary textarea is a robust way to copy text if clipboard API fails in some iframes try { navigator.clipboard.writeText(text).then(() => { setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); }); } catch (err) { const textArea = document.createElement("textarea"); textArea.value = text; document.body.appendChild(textArea); textArea.select(); document.execCommand("copy"); document.body.removeChild(textArea); setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); } }; const handleExportWord = (text, index) => { // Wrap the text in a simple HTML structure compatible with MS Word (.doc format) const header = "
" + formattedText + "
" + footer; // Create blob and download link const blob = new Blob(['\ufeff', html], { type: 'application/msword' }); const blobUrl = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = blobUrl; link.download = `Script_Affiliate_${index + 1}.doc`; document.body.appendChild(link); link.click(); // Cleanup document.body.removeChild(link); URL.revokeObjectURL(blobUrl); }; return (Sulap link produk menjadi script video affiliate (TikTok, Reels, Shopee) yang memikat dalam hitungan detik.