import { coord, R } from './libs';
export function toSelectionValues(args) {
    const { cells, selection } = args;
    if (selection.all) {
        return cells;
    }
    const res = {};
    if (selection.cell) {
        res[selection.cell] = cells[selection.cell];
    }
    const filterAxisRange = (field, start, end, coords) => {
        return coords
            .filter((cell) => cell[field] >= start && cell[field] <= end)
            .map((cell) => cell.key);
    };
    let keys = [];
    const all = Object.keys(cells).map((key) => coord.cell.toCell(key));
    coord.range.union(selection.ranges).ranges.forEach((range) => {
        if (coord.cell.isColumnRangeKey(range.key)) {
            keys = [...keys, ...filterAxisRange('column', range.left.column, range.right.column, all)];
        }
        else if (coord.cell.isRowRangeKey(range.key)) {
            keys = [...keys, ...filterAxisRange('row', range.left.row, range.right.row, all)];
        }
        else {
            keys = [...keys, ...range.keys];
        }
    });
    return R.uniq(keys).reduce((acc, key) => {
        const value = cells[key] || { value: undefined };
        acc[key] = value;
        return acc;
    }, res);
}
