import * as Types from "../../../../types/world/worldTypes";

import { toast } from "react-toastify";

import RemoveConnectedLines from "../lines/removeConnectedLines";
// import { deletePointApi } from "../../../../services/factoryBuilder/lines/deletePointApi";
import { Socket } from "socket.io-client";
import { getUserData } from "../../../../functions/getUserData";

function deletePoint(
  hoveredDeletablePoint: Types.RefMesh,
  onlyFloorlines: Types.RefOnlyFloorLines,
  floorPlanGroupPoint: Types.RefGroup,
  floorPlanGroupLine: Types.RefGroup,
  lines: Types.RefLines,
  setDeletedLines: any,
  socket: Socket<any>,
  projectId?: string,
  versionId?: string,
): void {
  //////////  Deleting a Point and the lines that are connected to it  //////////

  if (!hoveredDeletablePoint.current) {
    return;
  }
  const { userId, organization, email } = getUserData();
  (<any>hoveredDeletablePoint.current.material).dispose();
  (<any>hoveredDeletablePoint.current.geometry).dispose();
  floorPlanGroupPoint.current.remove(hoveredDeletablePoint.current);
  const DeletedPointUUID = hoveredDeletablePoint.current.uuid;

  //REST

  // deletePointApi(organization, DeletedPointUUID);

  //SOCKET

  const data = {
    organization,
    uuid: DeletedPointUUID,
    socketId: socket.id,
    versionId,
    projectId,
    userId,
  };

  // console.log('data: ', data);
  socket.emit("v1:Line:delete:point", data);

  //////////  Update onlyFloorlines.current to remove references to the deleted point  //////////

  onlyFloorlines.current = onlyFloorlines.current
    .map((floorline) =>
      floorline.filter(
        (line) =>
          line[0][1] !== DeletedPointUUID && line[1][1] !== DeletedPointUUID
      )
    )
    .filter((floorline) => floorline.length > 0);

  RemoveConnectedLines(
    DeletedPointUUID,
    floorPlanGroupLine,
    floorPlanGroupPoint,
    setDeletedLines,
    lines
  );

  echo.success("Point Removed!");
}

export default deletePoint;
