import { coord, util } from './libs';
export function toClipboard(args) {
    const { grid, action } = args;
    const cells = grid.selectionValues;
    const selection = grid.selection;
    const items = Object.keys(cells).map((key) => {
        let item = cells[key];
        item = typeof item !== 'object' ? { value: item } : item;
        if (typeof item.value === 'string') {
            item.value = util.removeMarkdownEncoding(item.value);
        }
        return { key, item };
    });
    const text = coord.table.toString({
        delimiter: '\t',
        items: items.map(({ key, item }) => {
            const value = item.value ? item.value.toString() : '';
            return { key, value };
        }),
    });
    const data = grid.data;
    const columns = getAxisData('COLUMN', selection, data.columns);
    const rows = getAxisData('ROW', selection, data.rows);
    const payload = {
        action,
        selection,
        text,
        cells,
        columns,
        rows,
    };
    return payload;
}
function getAxisData(axis, selection, data) {
    return selection.ranges.reduce((acc, next) => {
        if (coord.cell.isAxisRangeKey(next, axis)) {
            const range = coord.range.fromKey(next);
            range.axis(axis).keys.forEach((key) => {
                if (data[key]) {
                    acc[key] = data[key];
                }
            });
        }
        return acc;
    }, {});
}
