import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

/**
 * hover 显示全称
 */
import React, { Component } from 'react';
import { computed } from 'mobx';

var MouserOverWrapper = /*#__PURE__*/function (_Component) {
  _inherits(MouserOverWrapper, _Component);

  var _super = _createSuper(MouserOverWrapper);

  function MouserOverWrapper(props) {
    var _this;

    _classCallCheck(this, MouserOverWrapper);

    _this = _super.call(this, props);

    _this.strLength = function (str) {
      var len = {
        cL: 0,
        nL: 0,
        uL: 0,
        lL: 0,
        ol: 0,
        dL: 0,
        xL: 0,
        gL: 0
      };

      for (var i = 0; i < str.length; i += 1) {
        if (str.charCodeAt(i) >= 19968) {
          len.cL += 1; // 中文
        } else if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {
          len.nL += 1; // 0-9
        } else if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
          len.uL += 1; // A-Z
        } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
          len.lL += 1; // a-z
        } else if (str.charCodeAt(i) === 46) {
          len.dL += 1; // .
        } else if (str.charCodeAt(i) === 45) {
          len.gL += 1; // -
        } else if (str.charCodeAt(i) === 47 || str.charCodeAt(i) === 92) {
          len.xL += 1; // / \
        } else {
          len.ol += 1;
        }
      }

      return len.cL * 13 + len.nL * 7.09 + len.uL * 8.7 + len.lL * 6.8 + len.ol * 8 + len.dL * 3.78 + len.gL * 6.05 + len.xL * 4.58;
    };

    var ret = _this.calcWidth();

    _this.state = _objectSpread({
      domWidthMistake: 16 / 13,
      // 根据字符串计算宽度时 校正误差的乘法因子
      domRealWidth: 0,
      textStyle: null,
      domWidth: 0,
      maxWidth: 0
    }, ret);
    return _this;
  }

  _createClass(MouserOverWrapper, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$state = this.state,
          domWidth = _this$state.domWidth,
          domWidthMistake = _this$state.domWidthMistake,
          maxWidth = _this$state.maxWidth,
          domRealWidth = _this$state.domRealWidth;

      if (this.domTest && domRealWidth === 0) {
        var width = this.getDomRealWidth(this.domTest); // 这里用width×domWidthMistake的原因是：
        // domWidth的计算存在较大误差，这里实验性的将 真实的width放大（16/13）倍 来处理误差

        if (width && width !== 0 && domWidth > width * domWidthMistake && domWidth < maxWidth) {
          this.setState({
            domRealWidth: width
          });
        }
      }
    }
  }, {
    key: "getDomRealWidth",
    value: function getDomRealWidth(dom) {
      if (!dom) {
        return 0;
      }

      var val = dom.clientWidth;

      if (!val) {
        return 0;
      }

      var computedStyle = getComputedStyle(dom);
      var paddingWidth = parseFloat(computedStyle.paddingLeft) + parseFloat(computedStyle.paddingRight);
      return val - paddingWidth;
    }
  }, {
    key: "calcWidth",
    value: function calcWidth() {
      var _this$props = this.props,
          text = _this$props.text,
          width = _this$props.width,
          className = _this$props.className,
          style = _this$props.style;
      var menuWidth = document.getElementsByClassName('common-menu')[0] ? document.getElementsByClassName('common-menu')[0].offsetWidth : 250;
      var iWidth = window.innerWidth - 48 - menuWidth;
      var maxWidth = typeof width === 'number' ? iWidth * width : width.slice(0, -2);
      var textStyle = {
        maxWidth: "".concat(maxWidth, "px"),
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        display: 'inline-block'
      };
      Object.assign(textStyle, style);
      var domWidth = 0;

      if (text) {
        domWidth = this.strLength(text.toString());
      }

      return {
        textStyle: textStyle,
        domWidth: domWidth,
        maxWidth: maxWidth
      };
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$state2 = this.state,
          domRealWidth = _this$state2.domRealWidth,
          textStyle = _this$state2.textStyle,
          domWidth = _this$state2.domWidth,
          maxWidth = _this$state2.maxWidth,
          domWidthMistake = _this$state2.domWidthMistake;
      var _this$props2 = this.props,
          text = _this$props2.text,
          className = _this$props2.className; // 新增domRealWidth属性 来表示真实的dom元素宽度
      // 大体上按照原来的估算逻辑判断，加入逻辑：在挂载时计算真实的元素宽度
      // 如果 domWidth <= maxWidth 成立 但是 domWidth > domRealWidth
      // 表明 估算出现误差 此时 改变domRealWidth  来让组件重绘制
      // const minDomRealWidth = domRealWidth === 0 ? 0:domRealWidth - domWidthMistake;
      // const maxDomRealWidth =

      if ((domWidth < domRealWidth || domRealWidth === 0) && text && domWidth <= maxWidth) {
        return /*#__PURE__*/React.createElement("span", {
          ref: function ref(test) {
            _this2.domTest = test;
          },
          style: textStyle,
          className: className
        }, this.props.children);
      } else {
        return /*#__PURE__*/React.createElement(_Tooltip, {
          title: text,
          placement: "topLeft"
        }, /*#__PURE__*/React.createElement("span", {
          ref: function ref(test) {
            _this2.domTest = test;
          },
          style: textStyle,
          className: className
        }, this.props.children));
      }
    }
  }]);

  return MouserOverWrapper;
}(Component);

MouserOverWrapper.defaultProps = {
  text: ''
};
export { MouserOverWrapper as default };