"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.ActionsButton = 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 _Button = _interopRequireDefault(require("@material-ui/core/Button"));

var _Popover = _interopRequireDefault(require("@material-ui/core/Popover"));

var _Paper = _interopRequireDefault(require("@material-ui/core/Paper"));

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

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

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

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

  var _super = _createSuper(ActionsButton);

  function ActionsButton(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, ActionsButton);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleActionsClick", function (event) {
      _this.setState({
        actionsAnchorEl: event.currentTarget
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleActionsClose", function () {
      _this.setState({
        actionsAnchorEl: null
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleAddCategory", function () {
      var addCategory = _this.props.addCategory;
      addCategory();

      _this.handleActionsClose();
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleDeleteCategory", function (index) {
      var deleteCategory = _this.props.deleteCategory;
      deleteCategory(index);

      _this.handleActionsClose();
    });
    _this.state = {
      actionsAnchorEl: null
    };
    return _this;
  }

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

      var _this$props = this.props,
          classes = _this$props.classes,
          categories = _this$props.categories,
          language = _this$props.language;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.actions
      }, /*#__PURE__*/_react["default"].createElement("div", {
        role: "button",
        tabIndex: 0,
        className: classes.trigger,
        onClick: this.handleActionsClick
      }, "Actions"), /*#__PURE__*/_react["default"].createElement(_Popover["default"], {
        open: Boolean(this.state.actionsAnchorEl),
        anchorEl: this.state.actionsAnchorEl,
        onClose: this.handleActionsClose,
        anchorOrigin: {
          vertical: 'bottom',
          horizontal: 'left'
        },
        transformOrigin: {
          vertical: 'top',
          horizontal: 'left'
        }
      }, /*#__PURE__*/_react["default"].createElement(_Paper["default"], {
        className: classes.actionsPaper
      }, /*#__PURE__*/_react["default"].createElement(_Button["default"], {
        onClick: function onClick() {
          return _this2.handleAddCategory();
        }
      }, "+ ", translator.t('charting.add', {
        lng: language
      })), categories.length > 0 && categories.map(function (category, index) {
        return category.deletable && !category.correctness && /*#__PURE__*/_react["default"].createElement(_Button["default"], {
          key: index,
          onClick: function onClick() {
            return _this2.handleDeleteCategory(index);
          }
        }, "".concat(translator.t('charting.delete', {
          lng: language
        }), " <").concat(category.label || translator.t('charting.newLabel', {
          lng: language
        }), ">"));
      }))));
    }
  }]);
  return ActionsButton;
}(_react["default"].Component);

exports.ActionsButton = ActionsButton;
(0, _defineProperty2["default"])(ActionsButton, "propTypes", {
  classes: _propTypes["default"].object.isRequired,
  addCategory: _propTypes["default"].func.isRequired,
  deleteCategory: _propTypes["default"].func.isRequired,
  language: _propTypes["default"].string,
  categories: _propTypes["default"].array
});

var styles = function styles(theme) {
  return {
    actions: {
      alignSelf: 'flex-end'
    },
    trigger: {
      cursor: 'pointer',
      fontSize: theme.typography.fontSize,
      color: _renderUi.color.tertiary(),
      padding: theme.spacing.unit
    },
    actionsPaper: {
      padding: theme.spacing.unit,
      display: 'flex',
      flexDirection: 'column',
      gap: theme.spacing.unit,
      '& button': {
        textTransform: 'none',
        fontSize: theme.typography.fontSize,
        color: _renderUi.color.text(),
        justifyContent: 'flex-start'
      }
    }
  };
};

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

exports["default"] = _default;
//# sourceMappingURL=actions-button.js.map