/**
 * PositionUtils - Pixel/position calculations for calendar grid
 *
 * RESPONSIBILITY: Convert between time and pixel positions
 * NOTE: Date formatting belongs in DateService, not here
 */

import { IGridConfig } from '../core/IGridConfig';

export interface EventPosition {
  top: number;    // pixels from day start
  height: number; // pixels
}

/**
 * Calculate pixel position for an event based on its times
 */
export function calculateEventPosition(
  start: Date,
  end: Date,
  config: IGridConfig
): EventPosition {
  const startMinutes = start.getHours() * 60 + start.getMinutes();
  const endMinutes = end.getHours() * 60 + end.getMinutes();

  const dayStartMinutes = config.dayStartHour * 60;
  const minuteHeight = config.hourHeight / 60;

  const top = (startMinutes - dayStartMinutes) * minuteHeight;
  const height = (endMinutes - startMinutes) * minuteHeight;

  return { top, height };
}

/**
 * Convert minutes to pixels
 */
export function minutesToPixels(minutes: number, config: IGridConfig): number {
  return (minutes / 60) * config.hourHeight;
}

/**
 * Convert pixels to minutes
 */
export function pixelsToMinutes(pixels: number, config: IGridConfig): number {
  return (pixels / config.hourHeight) * 60;
}

/**
 * Snap pixel position to grid interval
 */
export function snapToGrid(pixels: number, config: IGridConfig): number {
  const snapPixels = minutesToPixels(config.snapInterval, config);
  return Math.round(pixels / snapPixels) * snapPixels;
}
