import * as THREE from 'three';

function snapToFixedPoint(
    position: [number, number, number],
    snapInterval: number = 0.1
): [number, number, number] {
    return [
        Math.round(position[0] / snapInterval) * snapInterval,
        Math.round(position[1] / snapInterval) * snapInterval,
        Math.round(position[2] / snapInterval) * snapInterval,
    ];
}

function handleDecalPositionSnap(
    point: THREE.Vector3,
    offset: THREE.Vector3,
    parent: Wall | Floor,
    decal: Decal,
    snapInterval: number = 0.1
): THREE.Vector3 {
    let rawPos: [number, number, number];

    rawPos = [
        point.x + offset.x,
        point.y + offset.y,
        decal.decalPosition[2],
    ];

    const snapped = snapToFixedPoint(rawPos, snapInterval);
    return new THREE.Vector3(snapped[0], snapped[1], snapped[2]);
}

export default handleDecalPositionSnap;