"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toClipboard = void 0;
var libs_1 = require("./libs");
function toClipboard(args) {
    var grid = args.grid, action = args.action;
    var cells = grid.selectionValues;
    var selection = grid.selection;
    var items = Object.keys(cells).map(function (key) {
        var item = cells[key];
        item = typeof item !== 'object' ? { value: item } : item;
        if (typeof item.value === 'string') {
            item.value = libs_1.util.removeMarkdownEncoding(item.value);
        }
        return { key: key, item: item };
    });
    var text = libs_1.coord.table.toString({
        delimiter: '\t',
        items: items.map(function (_a) {
            var key = _a.key, item = _a.item;
            var value = item.value ? item.value.toString() : '';
            return { key: key, value: value };
        }),
    });
    var data = grid.data;
    var columns = getAxisData('COLUMN', selection, data.columns);
    var rows = getAxisData('ROW', selection, data.rows);
    var payload = {
        action: action,
        selection: selection,
        text: text,
        cells: cells,
        columns: columns,
        rows: rows,
    };
    return payload;
}
exports.toClipboard = toClipboard;
function getAxisData(axis, selection, data) {
    return selection.ranges.reduce(function (acc, next) {
        if (libs_1.coord.cell.isAxisRangeKey(next, axis)) {
            var range = libs_1.coord.range.fromKey(next);
            range.axis(axis).keys.forEach(function (key) {
                if (data[key]) {
                    acc[key] = data[key];
                }
            });
        }
        return acc;
    }, {});
}
