import { Coordinate } from 'ol/coordinate';
import { radiansToDegrees } from './radiansToDegrees';

export const calculateLongestEdge = (coordinates: Coordinate[][]) => {
  const flatCoords = coordinates[0];
  let maxLength = 0;
  let longestEdgeAngle = 0;

  for (let i = 0; i < flatCoords.length - 1; i++) {
    const start = flatCoords[i];
    const end = flatCoords[i + 1];

    const dx = end[0] - start[0];
    const dy = end[1] - start[1];
    const length = Math.sqrt(dx * dx + dy * dy);

    if (length > maxLength) {
      maxLength = length;
      longestEdgeAngle = Math.atan2(dy, dx);
    }
  }

  return {
    longestEdgeAngle: radiansToDegrees(longestEdgeAngle),
  };
};
