import { getActiveProperties } from "./getActiveProperties";

export const convertAutoToNumeric = (
  canvasRect: DOMRect,
  position: {
    top: number | "auto";
    left: number | "auto";
    right: number | "auto";
    bottom: number | "auto";
  }
): { top: number; left: number; right: number; bottom: number } => {
  const { width, height } = canvasRect;

  // Determine which properties are active
  const [activeProp1, activeProp2] = getActiveProperties(position);

  let top = typeof position.top !== "string" ? position.top : 0;
  let left = typeof position.left !== "string" ? position.left : 0;
  let right = typeof position.right !== "string" ? position.right : 0;
  let bottom = typeof position.bottom !== "string" ? position.bottom : 0;

  // Calculate missing properties based on active properties
  if (activeProp1 === "top") {
    bottom = height - top;
  } else if (activeProp1 === "bottom") {
    top = height - bottom;
  }

  if (activeProp2 === "left") {
    right = width - left;
  } else if (activeProp2 === "right") {
    left = width - right;
  }

  return {
    top,
    left,
    right,
    bottom,
  };
};
