var _excluded = ["value", "tableProps", "minWidth", "maxWidth"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useRef, useEffect, useState } from 'react';
import { Popover } from 'antd';
import widthMap from "./letterWidth";
function getStrlen() {
  var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var needWidth = 0;
  for (var i = 0; i < str.length; i += 1) {
    var c = str.charCodeAt(i);
    // 单字节加1
    if (c >= 0x0001 && c <= 0x007e || c >= 0xff60 && c <= 0xff9f) {
      needWidth += widthMap[str[i]] || 9;
    } else {
      needWidth += 14;
    }
  }
  return needWidth;
}
function getLeftValue() {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var width = arguments.length > 1 ? arguments[1] : undefined;
  var curWidth = 0;
  var str = '';
  for (var i = 0; curWidth < width; i += 1) {
    // 单字节加1
    var c = value.charCodeAt(i);
    if (c >= 0x0001 && c <= 0x007e || c >= 0xff60 && c <= 0xff9f) {
      curWidth += widthMap[value[i]] || 9;
    } else {
      curWidth += 14;
    }
    if (curWidth > width) {
      break;
    }
    str += value[i];
  }
  return str;
}
function getRightValue() {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var width = arguments.length > 1 ? arguments[1] : undefined;
  var curWidth = 0;
  var str = '';
  for (var i = value.length - 1; curWidth < width; i -= 1) {
    // 单字节加1
    var c = value.charCodeAt(i);
    if (c >= 0x0001 && c <= 0x007e || c >= 0xff60 && c <= 0xff9f) {
      curWidth += widthMap[value[i]] || 9;
    } else {
      curWidth += 14;
    }
    if (curWidth > width) {
      break;
    }
    str += value[i];
  }
  return str.split('').reverse().join('');
}
export default function TableCellText(_ref) {
  var _ref$value = _ref.value,
    value = _ref$value === void 0 ? '-' : _ref$value,
    tableProps = _ref.tableProps,
    _ref$minWidth = _ref.minWidth,
    minWidth = _ref$minWidth === void 0 ? 70 : _ref$minWidth,
    _ref$maxWidth = _ref.maxWidth,
    maxWidth = _ref$maxWidth === void 0 ? 280 : _ref$maxWidth,
    others = _objectWithoutProperties(_ref, _excluded);
  var textRef = useRef(null);
  var _useState = useState(0),
    _useState2 = _slicedToArray(_useState, 2),
    showOverFlow = _useState2[0],
    setShowOverFlow = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    overFlowValue = _useState4[0],
    setOverFlowValue = _useState4[1];
  useEffect(function () {
    if (value) {
      var _textRef$current;
      var curValue = value.toString();
      var needWidth = getStrlen(curValue);
      var textWidth = textRef === null || textRef === void 0 || (_textRef$current = textRef.current) === null || _textRef$current === void 0 ? void 0 : _textRef$current.clientWidth;
      // 经测试中文字符在字体大小14px下，占用14px宽度，但是英文字符占用宽度却不尽相同
      if (needWidth > textWidth) {
        var resWidth = textWidth - 11.67; // ... 三个点占用11.67px宽度。
        var letf = Math.ceil(resWidth / 2);
        setOverFlowValue("".concat(getLeftValue(curValue, letf), "...").concat(getRightValue(curValue, resWidth - letf)));
        setShowOverFlow(true);
      } else {
        setShowOverFlow(false);
      }
    } else {
      setShowOverFlow(false);
    }
  }, [value]);
  var content = null;
  if (value instanceof Array) {
    content = /*#__PURE__*/React.createElement("span", {
      className: "popoverText"
    }, value.map(function (item) {
      return /*#__PURE__*/React.createElement("div", null, item);
    }));
  } else {
    content = /*#__PURE__*/React.createElement("span", {
      className: "popoverText"
    }, value);
  }
  var overlayStyle = {
    maxWidth: '20em',
    wordBreak: 'break-all',
    fontSize: '12px',
    color: '#666',
    overflow: 'auto',
    backgroud: '#fff',
    maxHeight: '400px',
    boxShadow: '5px 5px 10px rgba(129, 133, 167, 0.2)'
  };
  var text = {
    display: 'block',
    minWidth: minWidth,
    maxWidth: maxWidth,
    whiteSpace: 'nowrap',
    overflow: 'hidden',
    wordBreak: 'break-all'
    // textOverflow: 'ellipsis',
  };
  return /*#__PURE__*/React.createElement(Popover, {
    content: content,
    placement: "bottomLeft",
    overlayStyle: overlayStyle
  }, /*#__PURE__*/React.createElement("span", _extends({
    ref: textRef
  }, others, {
    style: text
  }), showOverFlow ? overFlowValue : value || '-'));
}