/**
 * LWC 4.x → 5.x 호환 shim
 *
 * LWC 4.x API를 내부적으로 LWC 5.x로 실행하도록 래핑합니다.
 * CandlestickChart / IndicatorChart 에서 import 경로만 교체해서 사용합니다.
 *
 * 주요 변환:
 *  - chart.addCandlestickSeries / addLineSeries / addHistogramSeries / addAreaSeries / addBarSeries
 *      → chart.addSeries(SeriesClass, options)
 *  - series.setMarkers(markers)
 *      → createSeriesMarkers(series, markers) / markersPlugin.setMarkers(markers)
 */

import {
  createChart as _createChart,
  CandlestickSeries,
  LineSeries,
  HistogramSeries,
  AreaSeries,
  BarSeries,
  BaselineSeries,
  createSeriesMarkers,
  ColorType,
  CrosshairMode,
  LineStyle,
  LineType,
  PriceScaleMode,
  LastPriceAnimationMode,
  MismatchDirection,
  PriceLineSource,
  TickMarkType,
  TrackingModeExitMode,
  isBusinessDay,
  isUTCTimestamp,
  version,
} from 'lightweight-charts-v5';

import type {
  IChartApi as _IChartApi,
  ISeriesApi as _ISeriesApi,
  IRange,
  SeriesType,
  Time,
  DeepPartial,
  ChartOptions,
  CandlestickSeriesOptions,
  LineSeriesOptions,
  HistogramSeriesOptions,
  AreaSeriesOptions,
  BarSeriesOptions,
  BaselineSeriesOptions,
} from 'lightweight-charts-v5';

// ─── 타입 재내보내기 ─────────────────────────────────────────────────────────

// LWC 5.x에서 제거된 LineWidth 타입 — 타입으로만 유지
export type LineWidth = 1 | 2 | 3 | 4;

export type {
  Time,
  BusinessDay,
  UTCTimestamp,
  IRange,
  IRange as Range,
  LogicalRange,
  SeriesType,
  SeriesOptionsMap,
  SeriesDataItemTypeMap,
  MouseEventParams,
  MouseEventHandler,
  DeepPartial,
  ChartOptions,
  CandlestickSeriesOptions,
  LineSeriesOptions,
  HistogramSeriesOptions,
  AreaSeriesOptions,
  BarSeriesOptions,
  BaselineSeriesOptions,
  BaselineSeriesOptions as BaselineOptions,
  IPriceLine,
  PriceLineOptions,
  SeriesMarker,
  SeriesMarkerPosition,
  SeriesMarkerShape,
  CrosshairOptions,
  LayoutOptions,
  GridOptions,
  PriceScaleOptions,
  TimeScaleOptions,
  AutoscaleInfo,
  WhitespaceData,
  OhlcData,
  LineData,
  HistogramData,
  BarData,
  AreaData,
  BaselineData,
  CandlestickData,
  IPriceFormatter,
} from 'lightweight-charts-v5';

// LWC 4.x 타입 호환 (v5에서 제거/변경된 것들)
export type TimeRange<T = Time> = IRange<T>;

// ─── 확장된 ISeriesApi (setMarkers 포함) ────────────────────────────────────
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface ISeriesApi<T extends SeriesType = SeriesType> extends _ISeriesApi<T> {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  setMarkers(markers: any[]): void;
}

// ─── 확장된 IChartApi (addXxxSeries 포함) ────────────────────────────────────
export interface IChartApi extends _IChartApi {
  addCandlestickSeries(options?: DeepPartial<CandlestickSeriesOptions>): ISeriesApi<'Candlestick'>;
  addLineSeries(options?: DeepPartial<LineSeriesOptions>): ISeriesApi<'Line'>;
  addHistogramSeries(options?: DeepPartial<HistogramSeriesOptions>): ISeriesApi<'Histogram'>;
  addAreaSeries(options?: DeepPartial<AreaSeriesOptions>): ISeriesApi<'Area'>;
  addBarSeries(options?: DeepPartial<BarSeriesOptions>): ISeriesApi<'Bar'>;
  addBaselineSeries(options?: DeepPartial<BaselineSeriesOptions>): ISeriesApi<'Baseline'>;
}

// ─── 값 재내보내기 ───────────────────────────────────────────────────────────
export {
  ColorType,
  CrosshairMode,
  LineStyle,
  LineType,
  PriceScaleMode,
  LastPriceAnimationMode,
  MismatchDirection,
  PriceLineSource,
  TickMarkType,
  TrackingModeExitMode,
  CandlestickSeries,
  LineSeries,
  HistogramSeries,
  AreaSeries,
  BarSeries,
  BaselineSeries,
  createSeriesMarkers,
  isBusinessDay,
  isUTCTimestamp,
  version,
};

// ─── series.setMarkers 지원 ─────────────────────────────────────────────────
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const markersPluginMap = new WeakMap<object, any>();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function wrapSeriesMarkers<T extends SeriesType>(series: _ISeriesApi<T>): ISeriesApi<T> {
  const s = series as ISeriesApi<T>;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  (s as any).setMarkers = (markers: any[]) => {
    if (markersPluginMap.has(series)) {
      markersPluginMap.get(series).setMarkers(markers);
    } else {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const plugin = (createSeriesMarkers as any)(series, markers);
      markersPluginMap.set(series, plugin);
    }
  };
  return s;
}

// ─── createChart 래퍼 ───────────────────────────────────────────────────────
export function createChart(
  container: HTMLElement | string,
  options?: DeepPartial<ChartOptions>,
): IChartApi {
  const chart = _createChart(container, options) as IChartApi;

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const wrapAdd = (SeriesClass: any) => (opts?: any) =>
    wrapSeriesMarkers(chart.addSeries(SeriesClass, opts ?? {}));

  (chart as IChartApi).addCandlestickSeries = wrapAdd(CandlestickSeries) as IChartApi['addCandlestickSeries'];
  (chart as IChartApi).addLineSeries = wrapAdd(LineSeries) as IChartApi['addLineSeries'];
  (chart as IChartApi).addHistogramSeries = wrapAdd(HistogramSeries) as IChartApi['addHistogramSeries'];
  (chart as IChartApi).addAreaSeries = wrapAdd(AreaSeries) as IChartApi['addAreaSeries'];
  (chart as IChartApi).addBarSeries = wrapAdd(BarSeries) as IChartApi['addBarSeries'];
  (chart as IChartApi).addBaselineSeries = wrapAdd(BaselineSeries) as IChartApi['addBaselineSeries'];

  return chart;
}
