import { useState, useEffect, useRef } from 'react';
import { ResponsiveContainer, ComposedChart, Area, Bar, Line, XAxis, YAxis, Tooltip, ReferenceLine, LineChart } from 'recharts';
import { AreaChart as AreaIcon, BarChart2 as CandleIcon, Activity, Eye, EyeOff, Maximize2, Minimize2 } from 'lucide-react';
import ChartChatbot from './ChartChatbot';
interface ChartDataPoint {
time: string;
price: number;
open?: number;
high?: number;
low?: number;
close?: number;
range?: [number, number];
}
interface StockChartProps {
activeTicker: string;
chartData: ChartDataPoint[];
activeStats: any;
chartRange: string;
onRangeChange: (range: string) => void;
user?: any;
onOpenRecharge?: () => void;
}
// Custom SVG component to draw wicks and body for candlestick bars
const CandlestickBar = (props: any) => {
const { x, y, width, height, minPrice } = props;
// Extract values safely from props or nested payload
const open = props.open !== undefined ? props.open : props.payload?.open;
const close = props.close !== undefined ? props.close : props.payload?.close;
const high = props.high !== undefined ? props.high : props.payload?.high;
const low = props.low !== undefined ? props.low : props.payload?.low;
if (open === undefined || close === undefined || high === undefined || low === undefined || minPrice === undefined) return null;
const isUp = close >= open;
const color = isUp ? '#10b981' : '#ef4444'; // green or red
// Calculate scale: pixels per unit price
// The bottom of the bar corresponds to minPrice. The top of the bar (y) corresponds to close.
const priceRange = close - minPrice;
const scale = priceRange > 0 ? height / priceRange : 1;
// Project prices to SVG coordinates
const yClose = y;
const yOpen = y + (close - open) * scale;
const yHigh = y + (close - high) * scale;
const yLow = y + (close - low) * scale;
const cx = x + width / 2;
const rectY = Math.min(yClose, yOpen);
const rectHeight = Math.max(Math.abs(yClose - yOpen), 2); // Ensure at least 2px height for body
// For dense charts (like MAX range), bars can become less than 1px. We cap the minimum body width to 2px so it remains visible.
const bodyWidth = Math.max(width, 2);
const bodyX = x - (bodyWidth - width) / 2; // Center the body
return (