import { addTo } from './addTo';
import { getGroup } from './getGroup';
import { getMesh } from './getMesh';
import { cardinalSplineCurve3D, getPointOnCardinalSplineCurve, getRotationBetweenPoints } from './utils/SplineCurve';
/**
 * Function to place objects along a path.
 * @example
 * ```ts
  addTo(
    root,
    getObjectsAlongPath({
      numberOfItems: 8,
      showPoints: false,
      points: [
        [-3, 6, 2],
        [-10, 8, 3],
        [-15, 12, 14],
        [-27, 20, 10],
      ],
      onEach: (props: any) => {
        const item = getPrefab({
          p: props.p,
          id: 'rounded_cube_01',
          s: [1.5, 0.75, 1.5],
          r: [0, props.r[1], 0],
        });
        return item; // <-- Important to return what's been added.
      },
    })
  );
 * ```
 */
export const getObjectsAlongPath = (options = {}) => {
    const { showPoints = false, numberOfItems = 10, points = [
        [0, 0, 0],
        [0, 5, 5],
    ], close = false, onEach = () => { }, } = options;
    const wrapper = getGroup(options);
    if (showPoints) {
        for (let i = 0; i < points.length; i++) {
            const p = points[i];
            const point = getMesh({
                id: 'sphere',
                p: p,
                s: [0.5, 2, 0.5],
                material: 'palette_01_red',
                physical: false,
            });
            addTo(wrapper, point);
        }
    }
    const tension = 1;
    const curve = cardinalSplineCurve3D(points, tension, 20, close);
    const points2 = [];
    for (let i = 0; i < numberOfItems; i++) {
        points2.push(getPointOnCardinalSplineCurve(curve, i / (numberOfItems - 1)));
    }
    let lastRotation;
    for (let i = 0; i < points2.length; i++) {
        const p1 = points2[i];
        let p2;
        if (points2[i + 1]) {
            p2 = points2[i + 1];
            lastRotation = [0, 0, 0];
            const rr = getRotationBetweenPoints(p1, p2);
            lastRotation = rr;
        }
        const progress = i / (numberOfItems - 1);
        const r = lastRotation;
        const props = {
            progress,
            index: i,
            p: p1,
            r,
        };
        const onEachRes = onEach(props);
        if (onEachRes) {
            addTo(wrapper, onEachRes);
        }
    }
    return wrapper;
};
//# sourceMappingURL=getObjectsAlongPath.js.map