"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.TableToolbar = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _styles = require("@material-ui/core/styles");

var _toolbarButtons = require("../toolbar/toolbar-buttons");

var _doneButton = require("../toolbar/done-button");

var _BorderAll = _interopRequireDefault(require("@material-ui/icons/BorderAll"));

var _defaultToolbar = require("../toolbar/default-toolbar");

var _icons = require("./icons");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _debug = _interopRequireDefault(require("debug"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var log = (0, _debug["default"])('@pie-lib:editable-html:plugins:table-toolbar');

var TableToolbar = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(TableToolbar, _React$Component);

  var _super = _createSuper(TableToolbar);

  function TableToolbar() {
    var _this;

    (0, _classCallCheck2["default"])(this, TableToolbar);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onDone", function (e) {
      var onDone = _this.props.onDone;
      e.preventDefault();
      onDone();
    });
    return _this;
  }

  (0, _createClass2["default"])(TableToolbar, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          getFocusedValue = _this$props.getFocusedValue,
          plugins = _this$props.plugins,
          value = _this$props.value,
          onChange = _this$props.onChange,
          onAddRow = _this$props.onAddRow,
          onRemoveRow = _this$props.onRemoveRow,
          onAddColumn = _this$props.onAddColumn,
          onRemoveColumn = _this$props.onRemoveColumn,
          onRemoveTable = _this$props.onRemoveTable,
          onToggleBorder = _this$props.onToggleBorder,
          hasBorder = _this$props.hasBorder,
          classes = _this$props.classes;
      log('[render] hasBorder:', hasBorder); // we're separating the response area plugin because we want to display it next to the done button

      var filteredPlugins = (plugins || []).reduce(function (acc, plugin) {
        if (plugin.name === 'response_area') {
          return _objectSpread(_objectSpread({}, acc), {}, {
            respAreaPlugin: plugin
          });
        }

        return _objectSpread(_objectSpread({}, acc), {}, {
          otherPlugins: [].concat((0, _toConsumableArray2["default"])(acc.otherPlugins), [plugin])
        });
      }, {
        respAreaPlugin: null,
        otherPlugins: []
      });
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.tableToolbar
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.toolbarButtons
      }, /*#__PURE__*/_react["default"].createElement(_toolbarButtons.Button, {
        onClick: onAddRow
      }, /*#__PURE__*/_react["default"].createElement(_icons.AddRow, null)), /*#__PURE__*/_react["default"].createElement(_toolbarButtons.Button, {
        onClick: onRemoveRow
      }, /*#__PURE__*/_react["default"].createElement(_icons.RemoveRow, null)), /*#__PURE__*/_react["default"].createElement(_toolbarButtons.Button, {
        onClick: onAddColumn
      }, /*#__PURE__*/_react["default"].createElement(_icons.AddColumn, null)), /*#__PURE__*/_react["default"].createElement(_toolbarButtons.Button, {
        onClick: onRemoveColumn
      }, /*#__PURE__*/_react["default"].createElement(_icons.RemoveColumn, null)), /*#__PURE__*/_react["default"].createElement(_toolbarButtons.Button, {
        onClick: onRemoveTable
      }, /*#__PURE__*/_react["default"].createElement(_icons.RemoveTable, null)), (filteredPlugins.otherPlugins || []).map(function (p, index) {
        return /*#__PURE__*/_react["default"].createElement(_defaultToolbar.ToolbarButton, (0, _extends2["default"])({
          key: "plugin-".concat(index)
        }, p.toolbar, {
          value: value,
          onChange: onChange,
          getFocusedValue: getFocusedValue
        }));
      }), /*#__PURE__*/_react["default"].createElement(_toolbarButtons.Button, {
        onClick: onToggleBorder,
        active: hasBorder
      }, /*#__PURE__*/_react["default"].createElement(_BorderAll["default"], null))), filteredPlugins.respAreaPlugin && /*#__PURE__*/_react["default"].createElement(_defaultToolbar.ToolbarButton, (0, _extends2["default"])({
        key: 'plugin-response-area'
      }, filteredPlugins.respAreaPlugin.toolbar, {
        value: value,
        onChange: onChange,
        getFocusedValue: getFocusedValue
      })), /*#__PURE__*/_react["default"].createElement(_doneButton.DoneButton, {
        onClick: this.onDone
      }));
    }
  }]);
  return TableToolbar;
}(_react["default"].Component);

exports.TableToolbar = TableToolbar;
(0, _defineProperty2["default"])(TableToolbar, "propTypes", {
  plugins: _propTypes["default"].array.isRequired,
  value: _propTypes["default"].object.isRequired,
  onChange: _propTypes["default"].func.isRequired,
  onAddRow: _propTypes["default"].func.isRequired,
  onRemoveRow: _propTypes["default"].func.isRequired,
  onAddColumn: _propTypes["default"].func.isRequired,
  onRemoveColumn: _propTypes["default"].func.isRequired,
  onRemoveTable: _propTypes["default"].func.isRequired,
  onToggleBorder: _propTypes["default"].func.isRequired,
  hasBorder: _propTypes["default"].bool,
  onDone: _propTypes["default"].func.isRequired,
  classes: _propTypes["default"].object.isRequired
});
(0, _defineProperty2["default"])(TableToolbar, "defaultProps", {
  plugins: [],
  value: {},
  onChange: function onChange() {}
});

var styles = function styles() {
  return {
    tableToolbar: {
      width: '100%',
      display: 'flex',
      justifyContent: 'space-between'
    },
    toolbarButtons: {
      display: 'flex'
    }
  };
};

var _default = (0, _styles.withStyles)(styles)(TableToolbar);

exports["default"] = _default;
//# sourceMappingURL=table-toolbar.js.map