import Command from '../command';
import { COMMAND_TYPE } from '../constants';

export default class MultiCommand extends Command {
  constructor(service, commands = [], options = {}) {
    super(service);
    this.type = COMMAND_TYPE.MULTI;
    this.name = options.name || '批量修改';
    this.commands = commands;
  }

  execute() {
    this.commands.forEach(command => {
      command.execute();
    });
  }

  undo() {
    for (let index = this.commands.length - 1; index >= 0; index -= 1) {
      this.commands[index].undo();
    }
  }

  getChangedKeys() {
    return this.commands.reduce((result, command) => {
      return result.concat(command.getChangedKeys());
    }, []);
  }
}
