export const getColumns = refs => {
    let count = 0;

    if (refs && refs.length && refs[0].current && refs[0].current.getBoundingClientRect) {
        const firstRect = refs[0].current.getBoundingClientRect();
        const top = firstRect.y;

        for (let i = 0, len = refs.length; i < len; i += 1) {
            const rect = refs[i].current.getBoundingClientRect();
            if (rect.y === top) {
                count += 1;
            } else {
                break;
            }
        }
    }
    return count;
};

export const getRow = (index, columns) => Math.floor(index / columns);

export const getRowHeights = (refs, columns) => {
    const heights = [];
    let maxHeight;
    refs.forEach((ref, idx) => {
        const row = getRow(idx, columns);
        // reset max height on a new row
        if (!(idx % columns)) {
            maxHeight = 0;
        }
        if (ref && ref.current) {
            maxHeight = Math.max(maxHeight, ref.current.offsetHeight);
        }
        heights[row] = maxHeight;
    });
    return heights;
};
