import { scaleToVec3 } from '@hiberworld/code-kit';
import { getClone, addTo, getGroup } from '.';
const clones = [];
let globalPosOffset = [0, 0, 0];
const stackingFn = (target, options) => {
    const { repeats = 3, direction = 'UP' } = options ?? {};
    let axisPosition = 0;
    let axisIndex = 1;
    let axisDir = 1;
    if (direction === 'DOWN') {
        axisIndex = 1;
        axisDir = -1;
    }
    else if (direction === 'RIGHT') {
        axisIndex = 0;
        axisDir = -1;
    }
    else if (direction === 'LEFT') {
        axisIndex = 0;
        axisDir = 1;
    }
    else if (direction === 'IN') {
        axisIndex = 2;
        axisDir = 1;
    }
    else if (direction === 'OUT') {
        axisIndex = 2;
        axisDir = -1;
    }
    const scale3 = scaleToVec3(target?.transform?.scale);
    let scale = scale3?.[axisIndex] || 1;
    let posSteps = scale * 2;
    for (let index = 0; index < repeats; index++) {
        axisPosition += posSteps * axisDir;
        const currentPosition = [0, 0, 0];
        currentPosition[axisIndex] = axisPosition;
        clones.push(getClone(target, {
            p: [
                currentPosition[0] + globalPosOffset[0],
                currentPosition[1] + globalPosOffset[1],
                currentPosition[2] + globalPosOffset[2],
            ],
        }));
    }
    if (clones.length) {
        globalPosOffset = clones[clones.length - 1].transform?.pos;
    }
};
/**
 *
 * Stack prefab or a group on each other
 * @example
 * ```
  const sCube = getPrefab({ id: 'scaffoldingcube_01' });
  const rig = getStack(sCube, {
    p: [5, 0, 0],
    repeats: [
      {
        repeats: 3,
        direction: 'UP',
      },
      {
        repeats: 5,
        direction: 'RIGHT',
      },
      {
        repeats: 3,
        direction: 'DOWN',
      },
    ],
  });
  addTo(root, rig);
 * ```
 */
export const getStack = (toStack, opt = {}) => {
    clones.length = 0;
    clones.push(toStack);
    globalPosOffset = [0, 0, 0];
    const val = {
        repeats: [
            {
                repeats: 3,
                direction: 'UP',
            },
        ],
        p: [0, 0, 0],
        r: [0, 0, 0],
        s: [1, 1, 1],
        ...opt,
    };
    const wrapper = getGroup(val);
    const repeatInfoArr = Array.isArray(val.repeats) ? val.repeats : [val.repeats];
    for (let index = 0; index < repeatInfoArr.length; index++) {
        const stackData = repeatInfoArr[index];
        stackingFn(toStack, stackData);
    }
    addTo(wrapper, clones);
    return wrapper;
};
//# sourceMappingURL=getStack.js.map