"use strict";

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

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

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 _propTypes = _interopRequireDefault(require("prop-types"));

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

var _classnames = _interopRequireDefault(require("classnames"));

var _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _renderUi = require("../render-ui");

var _tools = require("./tools");

var _drag = require("../drag");

var _translator = _interopRequireDefault(require("../translator"));

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; } }

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; }

var translator = _translator["default"].translator;

var buttonStyles = function buttonStyles() {
  return {
    root: {
      color: _renderUi.color.text()
    },
    selected: {
      backgroundColor: _renderUi.color.background(),
      border: "1px solid ".concat(_renderUi.color.secondary())
    },
    notSelected: {
      '& span': {
        color: _renderUi.color.primary()
      },
      backgroundColor: _renderUi.color.background()
    },
    disabled: {
      '& span': {
        color: _renderUi.color.primary()
      },
      backgroundColor: _renderUi.color.disabled()
    }
  };
};

var MiniButton = (0, _styles.withStyles)(buttonStyles)(function (props) {
  var disabled = props.disabled,
      classes = props.classes,
      className = props.className,
      selected = props.selected,
      value = props.value,
      _onClick = props.onClick,
      language = props.language;
  var translatorKey = value.toLowerCase();
  return /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    size: "small",
    disabled: disabled,
    className: (0, _classnames["default"])(classes.root, selected && classes.selected, className),
    classes: {
      disabled: (0, _classnames["default"])(disabled && classes.disabled)
    },
    value: value,
    key: value,
    variant: "outlined",
    onClick: function onClick(e) {
      return _onClick(_objectSpread(_objectSpread({}, e), {}, {
        buttonValue: value
      }));
    }
  }, translator.t("graphing.".concat(translatorKey), {
    lng: language
  }));
});
exports.MiniButton = MiniButton;
MiniButton.propTypes = {
  disabled: _propTypes["default"].bool,
  classes: _propTypes["default"].object,
  className: _propTypes["default"].string,
  disabledClassName: _propTypes["default"].string,
  selected: _propTypes["default"].bool,
  value: _propTypes["default"].string,
  onClick: _propTypes["default"].func
};

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

  var _super = _createSuper(ToggleBar);

  function ToggleBar() {
    var _this;

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

    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), "select", function (e) {
      return _this.props.onChange(e.buttonValue || e.target.textContent);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "moveTool", function (dragIndex, hoverIndex) {
      var _this$props = _this.props,
          options = _this$props.options,
          onChangeToolsOrder = _this$props.onChangeToolsOrder;
      var dragged = options[dragIndex];
      options.splice(dragIndex, 1);
      options.splice(hoverIndex, 0, dragged);
      onChangeToolsOrder(options);
    });
    return _this;
  }

  (0, _createClass2["default"])(ToggleBar, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          classes = _this$props2.classes,
          className = _this$props2.className,
          disabled = _this$props2.disabled,
          options = _this$props2.options,
          selectedToolType = _this$props2.selectedToolType,
          draggableTools = _this$props2.draggableTools,
          language = _this$props2.language;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])(className, classes.toolsContainer)
      }, (options || []).map(function (option, index) {
        if ((_tools.allTools || []).includes(option)) {
          var isSelected = option === selectedToolType;

          var toolRef = /*#__PURE__*/_react["default"].createRef();

          return /*#__PURE__*/_react["default"].createElement(DragTool, {
            key: option,
            index: index,
            draggable: draggableTools,
            moveTool: _this2.moveTool,
            classes: classes,
            toolRef: toolRef
          }, /*#__PURE__*/_react["default"].createElement(MiniButton, {
            className: (0, _classnames["default"])(classes.button, isSelected && classes.selected),
            disabled: disabled,
            disableRipple: true,
            onClick: _this2.select,
            value: option,
            selected: isSelected,
            language: language
          }));
        }
      }));
    }
  }]);
  return ToggleBar;
}(_react["default"].Component);

exports.ToggleBar = ToggleBar;
(0, _defineProperty2["default"])(ToggleBar, "propTypes", {
  classes: _propTypes["default"].object.isRequired,
  className: _propTypes["default"].string,
  options: _propTypes["default"].arrayOf(_propTypes["default"].string),
  selectedToolType: _propTypes["default"].string,
  disabled: _propTypes["default"].bool,
  draggableTools: _propTypes["default"].bool,
  onChange: _propTypes["default"].func,
  onChangeToolsOrder: _propTypes["default"].func,
  language: _propTypes["default"].string
});
(0, _defineProperty2["default"])(ToggleBar, "defaultProps", {});

var styles = function styles(theme) {
  return {
    toolsContainer: {
      display: 'flex',
      flexWrap: 'wrap'
    },
    button: {
      marginRight: theme.spacing.unit / 2,
      marginBottom: theme.spacing.unit / 2,
      color: _renderUi.color.text()
    },
    under: {
      position: 'absolute',
      top: 0,
      left: 0,
      zIndex: -1,
      pointerEvents: 'none'
    },
    wrapper: {
      position: 'relative'
    },
    hidden: {
      opacity: 0
    }
  };
};

var _default = (0, _drag.withDragContext)((0, _styles.withStyles)(styles)(ToggleBar));

exports["default"] = _default;
var DRAG_TYPE = 'tool';

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

  var _super2 = _createSuper(Item);

  function Item() {
    (0, _classCallCheck2["default"])(this, Item);
    return _super2.apply(this, arguments);
  }

  (0, _createClass2["default"])(Item, [{
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          classes = _this$props3.classes,
          children = _this$props3.children,
          connectDragSource = _this$props3.connectDragSource,
          connectDropTarget = _this$props3.connectDropTarget,
          connectDragPreview = _this$props3.connectDragPreview,
          isDragging = _this$props3.isDragging,
          toolRef = _this$props3.toolRef;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.wrapper,
        ref: toolRef
      }, connectDragSource(connectDropTarget( /*#__PURE__*/_react["default"].createElement("div", {
        className: isDragging && classes.hidden
      }, children))), connectDragPreview( /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.under
      }, children)));
    }
  }]);
  return Item;
}(_react["default"].Component);

exports.Item = Item;
(0, _defineProperty2["default"])(Item, "propTypes", {
  classes: _propTypes["default"].object.isRequired,
  className: _propTypes["default"].string,
  children: _propTypes["default"].oneOfType([_propTypes["default"].arrayOf(_propTypes["default"].node), _propTypes["default"].node]),
  connectDragSource: _propTypes["default"].func.isRequired,
  connectDragPreview: _propTypes["default"].func.isRequired,
  connectDropTarget: _propTypes["default"].func.isRequired,
  isDragging: _propTypes["default"].bool,
  toolRef: _propTypes["default"].any
});
(0, _defineProperty2["default"])(Item, "defaultProps", {});
var itemSource = {
  canDrag: function canDrag(props) {
    return props.draggable;
  },
  beginDrag: function beginDrag(props) {
    return {
      index: props.index
    };
  }
};
var itemTarget = {
  hover: function hover(props, monitor) {
    var _toolRef$current;

    var dragIndex = monitor.getItem().index;
    var toolRef = props.toolRef,
        hoverIndex = props.index;

    if (dragIndex === hoverIndex || !toolRef.current) {
      return;
    }

    var hoverBoundingRect = (_toolRef$current = toolRef.current) === null || _toolRef$current === void 0 ? void 0 : _toolRef$current.getBoundingClientRect();
    var hoverMiddleX = (hoverBoundingRect.right - hoverBoundingRect.left) / 2;
    var clientOffset = monitor.getClientOffset();
    var hoverClientX = clientOffset.x - hoverBoundingRect.left;

    if (dragIndex < hoverIndex && hoverClientX < hoverMiddleX) {
      return;
    }

    if (dragIndex > hoverIndex && hoverClientX > hoverMiddleX) {
      return;
    }

    props.moveTool(dragIndex, hoverIndex);
    monitor.getItem().index = hoverIndex;
  }
};

var collectTarget = function collectTarget(connect) {
  return {
    connectDropTarget: connect.dropTarget()
  };
};

var collectSource = function collectSource(connect, monitor) {
  return {
    connectDragSource: connect.dragSource(),
    connectDragPreview: connect.dragPreview(),
    isDragging: monitor.isDragging()
  };
};

var DragTool = (0, _drag.DropTarget)(DRAG_TYPE, itemTarget, collectTarget)((0, _drag.DragSource)(DRAG_TYPE, itemSource, collectSource)(Item));
//# sourceMappingURL=toggle-bar.js.map