'use client' import { useState, useMemo, memo, type KeyboardEvent } from 'react' import type { MapLegendProps, LegendItem } from '../types' const positionStyles: Record = { 'top-left': { top: 10, left: 10 }, 'top-right': { top: 10, right: 10 }, 'bottom-left': { bottom: 10, left: 10 }, 'bottom-right': { bottom: 10, right: 10 }, } function LegendIcon({ type, color, }: { type: LegendItem['type'] color?: string }) { const fill = color || '#888' switch (type) { case 'circle': return ( ) case 'line': return ( ) case 'fill': return ( ) case 'symbol': default: return ( ) } } function MapLegendComponent({ items, position = 'bottom-right', title, collapsible = false, defaultCollapsed = false, className = '', style, onItemClick, }: MapLegendProps) { const [collapsed, setCollapsed] = useState(defaultCollapsed) const containerStyle = useMemo( () => ({ position: 'absolute', ...positionStyles[position], backgroundColor: 'var(--color-popover)', color: 'var(--color-popover-foreground)', border: '1px solid var(--color-border)', borderRadius: 8, boxShadow: '0 2px 8px rgba(0,0,0,0.15)', padding: collapsed ? 8 : 12, minWidth: collapsed ? 'auto' : 120, zIndex: 1, ...style, }), [position, collapsed, style] ) const headerStyle = useMemo( () => ({ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, cursor: collapsible ? 'pointer' : 'default', fontWeight: 600, fontSize: 12, color: 'var(--color-foreground)', marginBottom: collapsed ? 0 : 8, }), [collapsible, collapsed] ) const itemStyle = useMemo( () => ({ display: 'flex', alignItems: 'center', gap: 8, padding: '4px 0', fontSize: 12, color: 'var(--color-muted-foreground)', cursor: onItemClick ? 'pointer' : 'default', }), [onItemClick] ) const handleHeaderClick = collapsible ? () => setCollapsed((c) => !c) : undefined return (
{(title || collapsible) && (
{ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() setCollapsed((c) => !c) } }, } : {})} > {title && {title}} {collapsible && ( )}
)} {!collapsed && (
    {items.map((item) => (
  • onItemClick(item) : undefined} {...(onItemClick ? { role: 'button' as const, tabIndex: 0, onKeyDown: (e: KeyboardEvent) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() onItemClick(item) } }, } : {})} > {item.icon || } {item.label}
  • ))}
)}
) } export const MapLegend = memo(MapLegendComponent)