"use strict";

exports.__esModule = true;
exports.shortcutsGridContext = shortcutsGridContext;
var _mixed = require("../helpers/mixed");
var _constants = require("./constants");
var _commands = require("./commands");
/**
 * The context that defines shortcut list available for selected cell or cells.
 *
 * @param {Handsontable} hot The Handsontable instance.
 */
function shortcutsGridContext(hot) {
  var context = hot.getShortcutManager().addContext('grid');
  var commandsPool = (0, _commands.createKeyboardShortcutCommandsPool)(hot);
  var config = {
    runOnlyIf: function runOnlyIf() {
      var _hot$getSettings = hot.getSettings(),
        navigableHeaders = _hot$getSettings.navigableHeaders;
      return (0, _mixed.isDefined)(hot.getSelected()) && (navigableHeaders || !navigableHeaders && hot.countRenderedRows() > 0 && hot.countRenderedCols() > 0);
    },
    group: _constants.GRID_GROUP
  };
  context.addShortcuts([{
    keys: [['F2']],
    callback: function callback(event) {
      return commandsPool.editorFastOpen(event);
    }
  }, {
    keys: [['Enter'], ['Enter', 'Shift']],
    callback: function callback(event, keys) {
      return commandsPool.editorOpen(event, keys);
    }
  }, {
    keys: [['Backspace'], ['Delete']],
    callback: function callback() {
      return commandsPool.emptySelectedCells();
    }
  }], {
    group: _constants.EDITOR_EDIT_GROUP,
    runOnlyIf: function runOnlyIf() {
      return (0, _mixed.isDefined)(hot.getSelected());
    }
  });
  context.addShortcuts([{
    keys: [['Control/Meta', 'A'], ['Control/Meta', 'Shift', 'Space']],
    callback: function callback() {
      return commandsPool.selectAll();
    }
  }, {
    keys: [['Control/Meta', 'Enter']],
    callback: function callback() {
      return commandsPool.populateSelectedCellsData();
    },
    runOnlyIf: function runOnlyIf() {
      return !hot.getSelectedRangeLast().highlight.isHeader() && hot.getSelectedRangeLast().getCellsCount() > 1;
    }
  }, {
    keys: [['Control', 'Space']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.extendCellsSelectionToColumns();
    }
  }, {
    keys: [['Shift', 'Space']],
    stopPropagation: true,
    callback: function callback() {
      return commandsPool.extendCellsSelectionToRows();
    }
  }, {
    keys: [['ArrowUp']],
    callback: function callback() {
      return commandsPool.moveCellSelectionUp();
    }
  }, {
    keys: [['ArrowUp', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostTop();
    }
  }, {
    keys: [['ArrowUp', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionUp();
    }
  }, {
    keys: [['ArrowUp', 'Shift', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.extendCellsSelectionToMostTop();
    },
    runOnlyIf: function runOnlyIf() {
      return !(hot.selection.isSelectedByCorner() || hot.selection.isSelectedByColumnHeader());
    }
  }, {
    keys: [['ArrowDown']],
    callback: function callback() {
      return commandsPool.moveCellSelectionDown();
    }
  }, {
    keys: [['ArrowDown', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostBottom();
    }
  }, {
    keys: [['ArrowDown', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionDown();
    }
  }, {
    keys: [['ArrowDown', 'Shift', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.extendCellsSelectionToMostBottom();
    },
    runOnlyIf: function runOnlyIf() {
      return !(hot.selection.isSelectedByCorner() || hot.selection.isSelectedByColumnHeader());
    }
  }, {
    keys: [['ArrowLeft']],
    callback: function callback() {
      return commandsPool.moveCellSelectionLeft();
    }
  }, {
    keys: [['ArrowLeft', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostLeft();
    }
  }, {
    keys: [['ArrowLeft', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionLeft();
    }
  }, {
    keys: [['ArrowLeft', 'Shift', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.extendCellsSelectionToMostLeft();
    },
    runOnlyIf: function runOnlyIf() {
      return !(hot.selection.isSelectedByCorner() || hot.selection.isSelectedByRowHeader());
    }
  }, {
    keys: [['ArrowRight']],
    callback: function callback() {
      return commandsPool.moveCellSelectionRight();
    }
  }, {
    keys: [['ArrowRight', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostRight();
    }
  }, {
    keys: [['ArrowRight', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionRight();
    }
  }, {
    keys: [['ArrowRight', 'Shift', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.extendCellsSelectionToMostRight();
    },
    runOnlyIf: function runOnlyIf() {
      return !(hot.selection.isSelectedByCorner() || hot.selection.isSelectedByRowHeader());
    }
  }, {
    keys: [['Home']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostInlineStart();
    },
    runOnlyIf: function runOnlyIf() {
      return hot.view.isMainTableNotFullyCoveredByOverlays();
    }
  }, {
    keys: [['Home', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionToMostInlineStart();
    }
  }, {
    keys: [['Home', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostTopInlineStart();
    },
    runOnlyIf: function runOnlyIf() {
      return hot.view.isMainTableNotFullyCoveredByOverlays();
    }
  }, {
    keys: [['End']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostInlineEnd();
    },
    runOnlyIf: function runOnlyIf() {
      return hot.view.isMainTableNotFullyCoveredByOverlays();
    }
  }, {
    keys: [['End', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionToMostInlineEnd();
    }
  }, {
    keys: [['End', 'Control/Meta']],
    captureCtrl: true,
    callback: function callback() {
      return commandsPool.moveCellSelectionToMostBottomInlineEnd();
    },
    runOnlyIf: function runOnlyIf() {
      return hot.view.isMainTableNotFullyCoveredByOverlays();
    }
  }, {
    keys: [['PageUp']],
    callback: function callback() {
      return commandsPool.moveCellSelectionUpByViewportHight();
    }
  }, {
    keys: [['PageUp', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionUpByViewportHeight();
    }
  }, {
    keys: [['PageDown']],
    callback: function callback() {
      return commandsPool.moveCellSelectionDownByViewportHeight();
    }
  }, {
    keys: [['PageDown', 'Shift']],
    callback: function callback() {
      return commandsPool.extendCellsSelectionDownByViewportHeight();
    }
  }, {
    keys: [['Tab']],
    callback: function callback() {
      return commandsPool.moveCellSelectionInlineStart();
    }
  }, {
    keys: [['Shift', 'Tab']],
    callback: function callback() {
      return commandsPool.moveCellSelectionInlineEnd();
    }
  }, {
    keys: [['Control/Meta', 'Backspace']],
    callback: function callback() {
      return commandsPool.scrollToFocusedCell();
    }
  }], config);
}