const DEG_TO_RAD = Math.PI / 180;

function finiteNumber(value) {
  const number = Number(value);
  return Number.isFinite(number) ? number : 0;
}

export function proceduralPlacementRotation(config = {}) {
  return {
    x: finiteNumber(config.rotationX),
    y: finiteNumber(config.rotationY),
    z: finiteNumber(config.rotationZ),
  };
}

export function applyProceduralPlacementRotation(config, target) {
  const rotation = proceduralPlacementRotation(config);
  const x = rotation.x * DEG_TO_RAD;
  const y = rotation.y * DEG_TO_RAD;
  const z = rotation.z * DEG_TO_RAD;
  if (target?.rotation?.set) {
    target.rotation.set(x, y, z);
  } else if (target?.rotation) {
    target.rotation.x = x;
    target.rotation.y = y;
    target.rotation.z = z;
  }
  return rotation;
}
