Загрузка данных


import type { Anchor, Bounds, ContainerSize, Point, SeriesApi } from './types';

import type { Coordinate, IChartApi, Time } from 'lightweight-charts';

interface SeriesTimeItem {
  time: Time;
}

interface TimePoint {
  time: number;
  coordinate: number;
}

export function getPriceFromYCoordinate(series: SeriesApi, yCoordinate: number): number | null {
  return series.coordinateToPrice(yCoordinate as Coordinate);
}

export function getYCoordinateFromPrice(series: SeriesApi, price: number): Coordinate | null {
  return series.priceToCoordinate(price);
}

export function getTimeFromXCoordinate(chart: IChartApi, xCoordinate: number): Time | null {
  return chart.timeScale().coordinateToTime(xCoordinate as Coordinate) ?? null;
}

export function getXCoordinateFromTime(chart: IChartApi, time: Time): Coordinate | null {
  return chart.timeScale().timeToCoordinate(time);
}

export function getProjectedXCoordinateFromTime(chart: IChartApi, series: SeriesApi, time: Time): Coordinate | null {
  const coordinate = getXCoordinateFromTime(chart, time);

  if (coordinate !== null) {
    return coordinate;
  }

  const targetTime = getNumericTime(time);

  if (targetTime === null) {
    return null;
  }

  const points = getSeriesTimeCoordinates(chart, series);

  if (points.length < 2) {
    return null;
  }

  const lastIndex = points.length - 1;

  if (targetTime <= points[0].time) {
    return interpolateCoordinate(points[0], points[1], targetTime);
  }

  if (targetTime >= points[lastIndex].time) {
    return interpolateCoordinate(points[lastIndex - 1], points[lastIndex], targetTime);
  }

  let left = 0;
  let right = lastIndex;

  while (left <= right) {
    const middle = Math.floor((left + right) / 2);
    const middleTime = points[middle].time;

    if (middleTime === targetTime) {
      return points[middle].coordinate as Coordinate;
    }

    if (middleTime < targetTime) {
      left = middle + 1;
    } else {
      right = middle - 1;
    }
  }

  return interpolateCoordinate(points[right], points[left], targetTime);
}

export function clamp(value: number, min: number, max: number): number {
  return Math.max(min, Math.min(value, max));
}

export function getContainerSize(container: HTMLElement): ContainerSize {
  const rect = container.getBoundingClientRect();

  return {
    width: rect.width,
    height: rect.height,
  };
}

export function clampPointToContainer(point: Point, container: HTMLElement): Point {
  const { width, height } = getContainerSize(container);

  return {
    x: clamp(point.x, 0, width),
    y: clamp(point.y, 0, height),
  };
}

export function getPointerPoint(container: HTMLElement, event: PointerEvent): Point {
  const rect = container.getBoundingClientRect();

  return clampPointToContainer(
    {
      x: event.clientX - rect.left,
      y: event.clientY - rect.top,
    },
    container,
  );
}

export function isNearPoint(point: Point, x: number, y: number, tolerance: number): boolean {
  return Math.abs(point.x - x) <= tolerance && Math.abs(point.y - y) <= tolerance;
}

export function isPointInBounds(point: Point, bounds: Bounds, tolerance = 0): boolean {
  return (
    point.x >= bounds.left - tolerance &&
    point.x <= bounds.right + tolerance &&
    point.y >= bounds.top - tolerance &&
    point.y <= bounds.bottom + tolerance
  );
}

export function normalizeBounds(
  left: number,
  right: number,
  top: number,
  bottom: number,
  container: HTMLElement,
): Bounds {
  const { width, height } = getContainerSize(container);

  return {
    left: clamp(Math.min(left, right), 0, width),
    right: clamp(Math.max(left, right), 0, width),
    top: clamp(Math.min(top, bottom), 0, height),
    bottom: clamp(Math.max(top, bottom), 0, height),
  };
}

export function shiftTimeByPixels(chart: IChartApi, time: Time, offsetX: number): Time | null {
  const coordinate = getXCoordinateFromTime(chart, time);

  if (coordinate === null) {
    return null;
  }

  return getTimeFromXCoordinate(chart, Number(coordinate) + offsetX);
}

export function getPriceDelta(series: SeriesApi, fromY: number, toY: number): number {
  const fromPrice = getPriceFromYCoordinate(series, fromY);
  const toPrice = getPriceFromYCoordinate(series, toY);

  if (fromPrice === null || toPrice === null) {
    return 0;
  }

  return toPrice - fromPrice;
}

export function getPriceRangeInContainer(
  series: SeriesApi,
  container: HTMLElement,
): { min: number; max: number } | null {
  const { height } = getContainerSize(container);

  if (!height) {
    return null;
  }

  const topPrice = getPriceFromYCoordinate(series, 0);
  const bottomPrice = getPriceFromYCoordinate(series, height);

  if (topPrice === null || bottomPrice === null) {
    return null;
  }

  return {
    min: Math.min(topPrice, bottomPrice),
    max: Math.max(topPrice, bottomPrice),
  };
}

export function getAnchorFromPoint(chart: IChartApi, series: SeriesApi, point: Point): Anchor | null {
  const time = getTimeFromXCoordinate(chart, point.x);
  const price = getPriceFromYCoordinate(series, point.y);

  if (time === null || price === null) {
    return null;
  }

  return {
    time,
    price,
  };
}

function getSeriesTimeCoordinates(chart: IChartApi, series: SeriesApi): TimePoint[] {
  const data = series.data() as readonly SeriesTimeItem[];

  const points = data.reduce<TimePoint[]>((result, item) => {
    const time = getNumericTime(item.time);

    if (time === null) {
      return result;
    }

    const coordinate = chart.timeScale().timeToCoordinate(item.time);

    if (coordinate === null) {
      return result;
    }

    result.push({
      time,
      coordinate: Number(coordinate),
    });

    return result;
  }, []);

  points.sort((a, b) => a.time - b.time);

  return points.filter((point, index, array) => index === 0 || point.time !== array[index - 1].time);
}

function interpolateCoordinate(leftPoint: TimePoint, rightPoint: TimePoint, targetTime: number): Coordinate | null {
  const timeRange = rightPoint.time - leftPoint.time;

  if (timeRange === 0) {
    return null;
  }

  const ratio = (targetTime - leftPoint.time) / timeRange;
  const coordinate = leftPoint.coordinate + (rightPoint.coordinate - leftPoint.coordinate) * ratio;

  if (!Number.isFinite(coordinate)) {
    return null;
  }

  return coordinate as Coordinate;
}

function getNumericTime(time: Time): number | null {
  if (typeof time === 'number') {
    return Number.isFinite(time) ? time : null;
  }

  if (typeof time === 'string') {
    const parsed = Date.parse(time);

    return Number.isFinite(parsed) ? Math.floor(parsed / 1000) : null;
  }

  if (typeof time === 'object' && time !== null && 'year' in time && 'month' in time && 'day' in time) {
    const businessDay = time as { year: number; month: number; day: number };

    return Math.floor(Date.UTC(businessDay.year, businessDay.month - 1, businessDay.day) / 1000);
  }

  return null;
}