"use strict";

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

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

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

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

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

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 _plot = require("@pie-lib/plot");

var _renderUi = require("@pie-lib/render-ui");

var _editableHtml = _interopRequireDefault(require("@pie-lib/editable-html"));

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

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 rotations = {
  left: -90,
  top: 0,
  bottom: 0,
  right: 90
};

var getTransform = function getTransform(side, width, height) {
  var t = function t(x, y, rotate) {
    return "translate(".concat(x, ", ").concat(y, "), rotate(").concat(rotate, ")");
  };

  if (side === 'left') {
    return t(-20, height / 2, rotations[side]);
  }

  if (side === 'right') {
    return t(width + 30, height / 2, rotations[side]);
  }

  if (side === 'top') {
    return t(width / 2, -20, rotations[side]);
  }

  if (side === 'bottom') {
    return t(width / 2, height + 30, rotations[side]);
  }
};

exports.getTransform = getTransform;

var getY = function getY(side, height) {
  switch (side) {
    case 'left':
      return -height;

    case 'top':
      return -height;

    case 'right':
      return -height - 10;

    default:
      return -height + 10;
  }
};

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

  var _super = _createSuper(RawLabel);

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

  (0, _createClass2["default"])(RawLabel, [{
    key: "render",
    value: function render() {
      var _cn;

      var _this$props = this.props,
          disabledLabel = _this$props.disabledLabel,
          placeholder = _this$props.placeholder,
          text = _this$props.text,
          side = _this$props.side,
          graphProps = _this$props.graphProps,
          classes = _this$props.classes,
          onChange = _this$props.onChange,
          _this$props$mathMlOpt = _this$props.mathMlOptions,
          mathMlOptions = _this$props$mathMlOpt === void 0 ? {} : _this$props$mathMlOpt;
      var size = graphProps.size,
          domain = graphProps.domain,
          range = graphProps.range;
      var totalHeight = (size.height || 500) + (range.padding || 0) * 2;
      var totalWidth = (size.width || 500) + (domain.padding || 0) * 2;
      var transform = getTransform(side, totalWidth, totalHeight);
      var width = side === 'left' || side === 'right' ? totalHeight : totalWidth;
      var height = 36;
      var y = getY(side, height);
      var finalHeight = side === 'bottom' ? height + 22 : height + 18;
      var activePlugins = ['bold', 'italic', 'underline', 'strikethrough', 'math' // 'languageCharacters'
      ];
      return /*#__PURE__*/_react["default"].createElement("foreignObject", {
        x: -(width / 2),
        y: y,
        width: width,
        height: finalHeight,
        transform: transform,
        textAnchor: "middle"
      }, /*#__PURE__*/_react["default"].createElement(_renderUi.Readable, {
        "false": true
      }, /*#__PURE__*/_react["default"].createElement(_editableHtml["default"], {
        className: (0, _classnames["default"])((_cn = {}, (0, _defineProperty2["default"])(_cn, classes.bottomLabel, side === 'bottom'), (0, _defineProperty2["default"])(_cn, classes.disabledAxisLabel, disabledLabel), _cn), classes.axisLabel),
        markup: text || '',
        onChange: onChange,
        placeholder: !disabledLabel && placeholder,
        toolbarOpts: {
          position: side === 'bottom' ? 'top' : 'bottom',
          noPadding: true,
          noBorder: true
        },
        activePlugins: activePlugins,
        mathMlOptions: mathMlOptions
      })));
    }
  }]);
  return RawLabel;
}(_react["default"].Component);

(0, _defineProperty2["default"])(RawLabel, "propTypes", {
  text: _propTypes["default"].string,
  side: _propTypes["default"].string,
  classes: _propTypes["default"].object,
  disabledLabel: _propTypes["default"].bool,
  placeholder: _propTypes["default"].string,
  graphProps: _plot.types.GraphPropsType.isRequired,
  onChange: _propTypes["default"].func
});
(0, _defineProperty2["default"])(RawLabel, "defaultProps", {
  onChange: function onChange() {}
});
var Label = (0, _styles.withStyles)(function (theme) {
  return {
    label: {
      fill: _renderUi.color.defaults.SECONDARY
    },
    axisLabel: {
      fontSize: theme.typography.fontSize - 2,
      textAlign: 'center',
      padding: '0 4px'
    },
    disabledAxisLabel: {
      pointerEvents: 'none'
    },
    bottomLabel: {
      marginTop: '44px'
    }
  };
})(RawLabel);
var LabelType = {
  left: _propTypes["default"].string,
  top: _propTypes["default"].string,
  bottom: _propTypes["default"].string,
  right: _propTypes["default"].string
};
exports.LabelType = LabelType;

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

  var _super2 = _createSuper(Labels);

  function Labels() {
    var _this;

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

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

    _this = _super2.call.apply(_super2, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChangeLabel", function (newValue, side) {
      var _this$props2 = _this.props,
          value = _this$props2.value,
          onChange = _this$props2.onChange;

      var labels = _objectSpread(_objectSpread({}, value), {}, (0, _defineProperty2["default"])({}, side, newValue));

      onChange(labels);
    });
    return _this;
  }

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

      var _this$props3 = this.props,
          disabledLabels = _this$props3.disabledLabels,
          _this$props3$placehol = _this$props3.placeholders,
          placeholders = _this$props3$placehol === void 0 ? {} : _this$props3$placehol,
          _this$props3$value = _this$props3.value,
          value = _this$props3$value === void 0 ? {} : _this$props3$value,
          graphProps = _this$props3.graphProps,
          _this$props3$mathMlOp = _this$props3.mathMlOptions,
          mathMlOptions = _this$props3$mathMlOp === void 0 ? {} : _this$props3$mathMlOp;
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(Label, {
        key: "left",
        side: "left",
        text: value.left,
        disabledLabel: disabledLabels,
        placeholder: placeholders.left,
        graphProps: graphProps,
        onChange: function onChange(value) {
          return _this2.onChangeLabel(value, 'left');
        },
        mathMlOptions: mathMlOptions
      }), /*#__PURE__*/_react["default"].createElement(Label, {
        key: "top",
        side: "top",
        text: value.top,
        disabledLabel: disabledLabels,
        placeholder: placeholders.top,
        graphProps: graphProps,
        onChange: function onChange(value) {
          return _this2.onChangeLabel(value, 'top');
        },
        mathMlOptions: mathMlOptions
      }), /*#__PURE__*/_react["default"].createElement(Label, {
        key: "bottom",
        side: "bottom",
        text: value.bottom,
        disabledLabel: disabledLabels,
        placeholder: placeholders.bottom,
        graphProps: graphProps,
        onChange: function onChange(value) {
          return _this2.onChangeLabel(value, 'bottom');
        },
        mathMlOptions: mathMlOptions
      }), /*#__PURE__*/_react["default"].createElement(Label, {
        key: "right",
        side: "right",
        text: value.right,
        disabledLabel: disabledLabels,
        placeholder: placeholders.right,
        graphProps: graphProps,
        onChange: function onChange(value) {
          return _this2.onChangeLabel(value, 'right');
        },
        mathMlOptions: mathMlOptions
      }));
    }
  }]);
  return Labels;
}(_react["default"].Component);

exports.Labels = Labels;
(0, _defineProperty2["default"])(Labels, "propTypes", {
  classes: _propTypes["default"].object,
  className: _propTypes["default"].string,
  disabledLabels: _propTypes["default"].bool,
  placeholders: _propTypes["default"].object,
  value: _propTypes["default"].shape(LabelType),
  graphProps: _propTypes["default"].object,
  onChange: _propTypes["default"].object
});
(0, _defineProperty2["default"])(Labels, "defaultProps", {});
var _default = Labels;
exports["default"] = _default;
//# sourceMappingURL=labels.js.map