"use client" import { Bookmark, Copy, Download, Edit2, FileText, Plus, Search, Trash2, Upload, } from "lucide-react" import { useCallback, useEffect, useRef, useState } from "react" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog" import { useDictionary } from "@/hooks/use-dictionary" import { deleteTemplate, duplicateTemplate, exportTemplates, getAllTemplates, importTemplates, incrementClickCount, incrementRunCount, searchTemplates, type Template, updateTemplate, validateImportData, } from "@/lib/template-storage" import { TemplateCreateDialog } from "./TemplateCreateDialog" import { TemplateEditDialog } from "./TemplateEditDialog" interface TemplatePanelProps { setInput: (input: string) => void onSendTemplate?: (template: Template) => void currentInput?: string } function formatLastUsed(timestamp: number, neverUsedText: string): string { if (!timestamp) return neverUsedText const now = Date.now() const diffMs = now - timestamp const diffMins = Math.floor(diffMs / (1000 * 60)) const diffHours = Math.floor(diffMs / (1000 * 60 * 60)) const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)) try { const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" }) if (diffMins < 1) return rtf.format(0, "minute") if (diffMins < 60) return rtf.format(-diffMins, "minute") if (diffHours < 24) return rtf.format(-diffHours, "hour") if (diffDays < 7) return rtf.format(-diffDays, "day") } catch { // Fallback if Intl.RelativeTimeFormat is not available if (diffMins < 1) return "<1m ago" if (diffMins < 60) return `${diffMins}m ago` if (diffHours < 24) return `${diffHours}h ago` if (diffDays < 7) return `${diffDays}d ago` } return new Date(timestamp).toLocaleDateString(undefined, { month: "short", day: "numeric", }) } export function TemplatePanel({ setInput, onSendTemplate, currentInput = "", }: TemplatePanelProps) { const dict = useDictionary() const [templates, setTemplates] = useState([]) const [loading, setLoading] = useState(true) const [createDialogOpen, setCreateDialogOpen] = useState(false) const [editDialogOpen, setEditDialogOpen] = useState(false) const [templateToEdit, setTemplateToEdit] = useState