import { filter } from 'rxjs/operators';
import { toSelectionValues, DEFAULT, util } from '../common';
const STYLE = ['BOLD', 'ITALIC', 'UNDERLINE'];
export function init(args) {
    const { grid, command$ } = args;
    const style$ = command$.pipe(filter((e) => STYLE.includes(e.command)), filter((e) => !e.isCancelled));
    style$.subscribe((e) => {
        const command = e.command;
        const field = toField(command);
        const values = toSelectionValues({ cells: grid.data.cells, selection: e.selection });
        const defaults = DEFAULT.CELL.PROPS.style;
        const changes = Object.keys(values).reduce((acc, key) => {
            const cell = grid.cell(key);
            const value = cell.data.value;
            const error = cell.data.error;
            const props = util.cell.value.toggleCellProp({
                defaults,
                props: cell.data.props,
                section: 'style',
                field,
            });
            acc[key] = { value, props, error };
            return acc;
        }, {});
        grid.changeCells(changes, { source: 'PROPS/style' });
    });
}
const toField = (command) => {
    switch (command) {
        case 'BOLD':
            return 'bold';
        case 'ITALIC':
            return 'italic';
        case 'UNDERLINE':
            return 'underline';
        default:
            throw new Error(`Command '${command}' not supported`);
    }
};
