"use strict";

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

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

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

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

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

var _plot = require("../plot");

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

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

var _InputBase = _interopRequireDefault(require("@material-ui/core/InputBase"));

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

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 styles = function styles(theme) {
  return {
    input: {
      fontFamily: theme.typography.fontFamily,
      fontSize: theme.typography.fontSize,
      borderRadius: '8px',
      background: theme.palette.common.white,
      color: _renderUi.color.defaults.BLACK
    },
    inputLabel: {
      padding: 0
    }
  };
};

var getLabelPosition = function getLabelPosition(graphProps, x, y, labelLength) {
  var scale = graphProps.scale,
      domain = graphProps.domain,
      range = graphProps.range; // treat corner cases for maximum and minimum

  var topShift = y === range.min ? 16 : y === range.max ? 0 : 8;
  var leftShift = 10;
  var rightEdge = scale.x(x) + labelLength + leftShift;

  if (rightEdge >= scale.x(domain.max)) {
    return {
      left: scale.x(x) - leftShift - labelLength,
      top: scale.y(y) - topShift
    };
  }

  return {
    left: scale.x(x) + leftShift,
    top: scale.y(y) - topShift
  };
};

exports.getLabelPosition = getLabelPosition;

var CoordinatesLabel = function CoordinatesLabel(_ref) {
  var x = _ref.x,
      y = _ref.y,
      graphProps = _ref.graphProps,
      classes = _ref.classes;
  var label = "(".concat((0, _utils.roundNumber)(x), ", ").concat((0, _utils.roundNumber)(y), ")");
  var labelLength = (label.length || 0) * 6;
  var labelPosition = getLabelPosition(graphProps, x, y, labelLength);

  var style = _objectSpread({
    position: 'absolute',
    pointerEvents: 'auto',
    width: labelLength,
    padding: 0
  }, labelPosition);

  return /*#__PURE__*/_react["default"].createElement(_InputBase["default"], {
    style: style,
    classes: {
      input: classes.inputLabel
    },
    className: classes.input,
    value: label,
    inputProps: {
      ariaLabel: 'naked'
    }
  });
};

exports.CoordinatesLabel = CoordinatesLabel;
CoordinatesLabel.propTypes = {
  graphProps: _plot.types.GraphPropsType,
  classes: _propTypes["default"].object,
  x: _propTypes["default"].number,
  y: _propTypes["default"].number
};

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

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