"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toSelectionValues = void 0;
var tslib_1 = require("tslib");
var libs_1 = require("./libs");
function toSelectionValues(args) {
    var cells = args.cells, selection = args.selection;
    if (selection.all) {
        return cells;
    }
    var res = {};
    if (selection.cell) {
        res[selection.cell] = cells[selection.cell];
    }
    var filterAxisRange = function (field, start, end, coords) {
        return coords
            .filter(function (cell) { return cell[field] >= start && cell[field] <= end; })
            .map(function (cell) { return cell.key; });
    };
    var keys = [];
    var all = Object.keys(cells).map(function (key) { return libs_1.coord.cell.toCell(key); });
    libs_1.coord.range.union(selection.ranges).ranges.forEach(function (range) {
        if (libs_1.coord.cell.isColumnRangeKey(range.key)) {
            keys = tslib_1.__spreadArrays(keys, filterAxisRange('column', range.left.column, range.right.column, all));
        }
        else if (libs_1.coord.cell.isRowRangeKey(range.key)) {
            keys = tslib_1.__spreadArrays(keys, filterAxisRange('row', range.left.row, range.right.row, all));
        }
        else {
            keys = tslib_1.__spreadArrays(keys, range.keys);
        }
    });
    return libs_1.R.uniq(keys).reduce(function (acc, key) {
        var value = cells[key] || { value: undefined };
        acc[key] = value;
        return acc;
    }, res);
}
exports.toSelectionValues = toSelectionValues;
