import type { WallOpening } from '@house-plan-maker/shared';

/**
 * Check if a new opening at the given position would overlap with existing openings
 * on the same wall.
 */
export function hasOverlap(
  wallId: string,
  position: number,
  width: number,
  existingOpenings: readonly WallOpening[],
): boolean {
  const halfWidth = width / 2;
  const newStart = position - halfWidth;
  const newEnd = position + halfWidth;

  for (const opening of existingOpenings) {
    if (opening.wallId !== wallId) continue;

    const existingStart = opening.positionAlongWall - opening.width / 2;
    const existingEnd = opening.positionAlongWall + opening.width / 2;

    if (newStart < existingEnd && newEnd > existingStart) {
      return true;
    }
  }

  return false;
}
