import { GROUP_NAME, defaultCircleStyle, defaultLineStyle } from './constants';
import { uid } from 'uid';
import { FenceGroup } from './types';

export const getCircleConfig = (points: number[], scale: number) => {
  const circlePoints = [];
  for (let i = 0; i < points.length - 1; i += 2) {
    circlePoints.push([points[i], points[i + 1]]);
  }
  return circlePoints.map((point, idx) => {
    return createCircleConfigItem(point as [number, number], idx, scale);
  });
};

export const createCircleConfigItem = (point: [number, number], idx: number, scale: number) => {
  return {
    ...defaultCircleStyle,
    radius: defaultCircleStyle.radius / scale,
    hitStrokeWidth: defaultCircleStyle.hitStrokeWidth / scale,
    x: point[0],
    y: point[1],
    uid: uid(),
    idx,
  };
};

export const createGroupConfig = (points: number[], scale: number): FenceGroup => {
  const lineConfig = {
    ...defaultLineStyle,
    strokeWidth: defaultLineStyle.strokeWidth / scale,
    points: points,
  };
  const circleConfigs = getCircleConfig(points, scale);
  return {
    lineConfig,
    name: GROUP_NAME,
    circleConfigs,
    uid: uid(),
    _temp: { points: [] },
  };
};
