"use strict";

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

var _typeof = require("@babel/runtime/helpers/typeof");

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

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

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

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

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

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

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

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

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

var _utils = require("./utils");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var LabelComponent = function LabelComponent(props) {
  var _cn;

  var classes = props.classes,
      disabledLabel = props.disabledLabel,
      graphHeight = props.graphHeight,
      graphWidth = props.graphWidth,
      isChartBottomLabel = props.isChartBottomLabel,
      isDefineChartBottomLabel = props.isDefineChartBottomLabel,
      isChartLeftLabel = props.isChartLeftLabel,
      isDefineChartLeftLabel = props.isDefineChartLeftLabel,
      placeholder = props.placeholder,
      text = props.text,
      side = props.side,
      onChange = props.onChange,
      _props$mathMlOptions = props.mathMlOptions,
      mathMlOptions = _props$mathMlOptions === void 0 ? {} : _props$mathMlOptions,
      charactersLimit = props.charactersLimit,
      titleHeight = props.titleHeight;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      rotatedToHorizontal = _useState2[0],
      setRotatedToHorizontal = _useState2[1];

  var activePlugins = ['bold', 'italic', 'underline', 'strikethrough', 'math' // 'languageCharacters'
  ];
  var isChart = isChartBottomLabel || isChartLeftLabel || isDefineChartBottomLabel || isDefineChartLeftLabel;
  var chartValue = side === 'left' && isDefineChartLeftLabel && graphHeight - 220;
  var defaultStyle = {
    width: chartValue || (side === 'left' || side === 'right' ? graphHeight - 8 : graphWidth - 8),
    top: chartValue || isChartLeftLabel && "".concat(graphHeight - 70, "px") || side === 'left' && "".concat(graphHeight - 8, "px") || isChartBottomLabel && "".concat(graphHeight - 60 + titleHeight, "px") || side === 'bottom' && "".concat(graphHeight - 120 + titleHeight, "px") || 0,
    left: side === 'right' && "".concat(graphWidth - 8, "px") || (isDefineChartLeftLabel || isDefineChartBottomLabel) && '40px' || isChartBottomLabel && '-10px' || 0
  };
  var rotatedStyle = {
    width: graphWidth - 8,
    top: side === 'right' && "".concat(graphHeight - 22, "px") || 0,
    left: 0
  };

  var rotateLabel = function rotateLabel() {
    return !disabledLabel && (side === 'left' || side === 'right') && setRotatedToHorizontal(true);
  };

  return /*#__PURE__*/_react["default"].createElement(_renderUi.Readable, {
    "false": true
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(isChart ? classes.chartLabel : classes.axisLabel, (_cn = {}, (0, _defineProperty2["default"])(_cn, classes.rotateLeftLabel, side === 'left' && !rotatedToHorizontal), (0, _defineProperty2["default"])(_cn, classes.rotateRightLabel, side === 'right' && !rotatedToHorizontal), (0, _defineProperty2["default"])(_cn, classes.editLabel, rotatedToHorizontal), (0, _defineProperty2["default"])(_cn, classes.customBottom, isChartBottomLabel || isDefineChartBottomLabel), (0, _defineProperty2["default"])(_cn, classes.displayNone, disabledLabel && !isChart && (0, _utils.isEmptyString)((0, _utils.extractTextFromHTML)(text))), _cn)),
    style: rotatedToHorizontal ? rotatedStyle : defaultStyle,
    onClick: rotateLabel
  }, disabledLabel ? /*#__PURE__*/_react["default"].createElement("div", {
    className: classes.disabledLabel,
    dangerouslySetInnerHTML: {
      __html: text || ''
    }
  }) : /*#__PURE__*/_react["default"].createElement(_editableHtml["default"], {
    markup: text || '',
    onChange: onChange,
    placeholder: !disabledLabel && placeholder,
    toolbarOpts: {
      position: side === 'bottom' ? 'top' : 'bottom',
      noPadding: true,
      noBorder: true
    },
    disableScrollbar: true,
    activePlugins: activePlugins,
    onDone: function onDone() {
      return setRotatedToHorizontal(false);
    },
    mathMlOptions: mathMlOptions,
    charactersLimit: charactersLimit
  })));
};

LabelComponent.propTypes = {
  classes: _propTypes["default"].object,
  disabledLabel: _propTypes["default"].bool,
  graphHeight: _propTypes["default"].number,
  graphWidth: _propTypes["default"].number,
  isChartBottomLabel: _propTypes["default"].bool,
  isDefineChartBottomLabel: _propTypes["default"].bool,
  isChartLeftLabel: _propTypes["default"].bool,
  isDefineChartLeftLabel: _propTypes["default"].bool,
  placeholder: _propTypes["default"].string,
  text: _propTypes["default"].string,
  side: _propTypes["default"].string,
  onChange: _propTypes["default"].func,
  mathMlOptions: _propTypes["default"].object,
  charactersLimit: _propTypes["default"].number,
  titleHeight: _propTypes["default"].number
};

var _default = (0, _styles.withStyles)(function (theme) {
  return {
    label: {
      fill: _renderUi.color.secondary()
    },
    axisLabel: {
      fontSize: theme.typography.fontSize - 2,
      textAlign: 'center',
      margin: theme.spacing.unit / 2,
      padding: "".concat(theme.spacing.unit / 2, "px 0")
    },
    chartLabel: {
      fontSize: theme.typography.fontSize + 2,
      textAlign: 'center',
      margin: theme.spacing.unit / 2,
      padding: "".concat(theme.spacing.unit / 2, "px 0")
    },
    disabledLabel: {
      pointerEvents: 'none',
      width: '100%'
    },
    editLabel: {
      position: 'absolute',
      backgroundColor: 'white',
      borderRadius: '4px',
      boxShadow: '0px 5px 8px rgba(0, 0, 0, 0.15)',
      zIndex: 10
    },
    rotateLeftLabel: {
      '-webkit-transform': 'rotate(-90deg)',
      transformOrigin: '0 0',
      transformStyle: 'preserve-3d',
      position: 'absolute'
    },
    rotateRightLabel: {
      '-webkit-transform': 'rotate(90deg)',
      transformOrigin: '0 0',
      transformStyle: 'preserve-3d',
      position: 'absolute'
    },
    customBottom: {
      position: 'absolute'
    },
    displayNone: {
      display: 'none'
    }
  };
})(LabelComponent);

exports["default"] = _default;
//# sourceMappingURL=label.js.map