import { getBoundingBox } from './getBoundingBox';
/**
 * Distribute prefab or groups horizontally
 * Good for placing art on walls
 * @example
 * ```ts
  const decoWrapper = getGroup();
  addTo(root, decoWrapper);
  for (let index = 0; index < 6; index++) {
    addTo(decoWrapper, getPrefab({ id: 'tron_q1_blue', p: [index, -1.05, 0.75], r: [90, 0, 0], s: [1, 0.6, 1] }));
  }
  if (decoWrapper.children) distributeHorizontally(decoWrapper.children, 4);

  // Alternative ---

  const arts = [];
  arts.push(getFramedImage({ p: [0, 1, 0], src: 'https://cdn.hibervr.com/demo/img/amanda.jpg', ratio: 1000 / 560 }));
  arts.push(getFramedImage({ p: [0, 1, 0], src: 'https://cdn.hibervr.com/demo/img/earth.jpg', ratio: 662 / 1000 }));
  arts.push(getFramedImage({ p: [0, 1, 0], src: 'https://cdn.hibervr.com/demo/img/Nighthawks.jpg', ratio: 1000 / 567 }));
  distributeHorizontally(arts, 10);
  addTo(wall, arts);

 * ```
 */
export const distributeHorizontally = (toAlign, size) => {
    size = size * 2;
    const startPos = size * 0.5;
    const steps = size / (toAlign.length - 1);
    let totWidth = 0;
    const bbs = [];
    for (let index = 0; index < toAlign.length; index++) {
        const obj = toAlign[index];
        const bb = getBoundingBox(obj);
        if (bb) {
            bbs.push(bb);
            totWidth += bb.width * 0.5;
        }
    }
    const gapSize = (size - totWidth * 2) / (toAlign.length + 1);
    let xPos = startPos - gapSize;
    for (let index = 0; index < toAlign.length; index++) {
        const obj = toAlign[index];
        if (obj.transform?.pos) {
            xPos -= bbs[index].width * 0.5;
            obj.transform.pos[0] = xPos;
            xPos -= bbs[index].width * 0.5 + gapSize;
        }
    }
};
//# sourceMappingURL=distributeHorizontally.js.map