"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Ellipsis", {
    enumerable: true,
    get: function() {
        return Ellipsis;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _ts_generator = require("@swc/helpers/_/_ts_generator");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _taro = require("@tarojs/taro");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _getrect = require("../../utils/taro/get-rect");
var _typings = require("../../utils/typings");
var _index = require("../configprovider/index");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    content: '',
    direction: 'end',
    rows: 1,
    expandText: '',
    collapseText: '',
    symbol: '...',
    lineHeight: '20',
    width: 'auto'
});
var classPrefix = "nut-ellipsis";
var Ellipsis = function Ellipsis(props) {
    var _ellipsis_current, _ellipsis_current1, _ellipsis_current2, _ellipsis_current3;
    var _ref = (0, _object_spread._)({}, defaultProps, props), children = _ref.children, content = _ref.content, direction = _ref.direction, rows = _ref.rows, className = _ref.className, expandText = _ref.expandText, collapseText = _ref.collapseText, symbol = _ref.symbol, lineHeight = _ref.lineHeight, width = _ref.width, onClick = _ref.onClick, onChange = _ref.onChange, rest = (0, _object_without_properties._)(_ref, [
        "children",
        "content",
        "direction",
        "rows",
        "className",
        "expandText",
        "collapseText",
        "symbol",
        "lineHeight",
        "width",
        "onClick",
        "onChange"
    ]);
    var maxHeight = (0, _react.useRef)(0);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), exceeded = _useState[0], setExceeded = _useState[1];
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), expanded = _useState1[0], setExpanded = _useState1[1];
    var _useState2 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), cacled = _useState2[0], setCacled = _useState2[1];
    var ellipsis = (0, _react.useRef)({
        leading: '',
        tailing: ''
    });
    var root = (0, _react.useRef)(null);
    var rootContain = (0, _react.useRef)(null);
    var symbolContain = (0, _react.useRef)(null);
    var _useState3 = (0, _sliced_to_array._)((0, _react.useState)(content), 2), contentCopy = _useState3[0], setContentCopy = _useState3[1];
    var lineH = (0, _react.useRef)(0) // 当行的最大高度
    ;
    var originHeight = (0, _react.useRef)(0) // 原始高度
    ;
    var refRandomId = (0, _react.useRef)(Math.random().toString(36).slice(-8));
    var widthRef = (0, _react.useRef)('auto');
    var widthBase = (0, _react.useRef)([
        14,
        10,
        7,
        8.4,
        10
    ]) // 中、英(大)、英(小)、数字、其他字符的基础宽度
    ;
    var symbolTextWidth = (0, _react.useRef)(widthBase.current[0] * 0.7921);
    var chineseReg = /^[\u4e00-\u9fa5]+$/ // 汉字
    ;
    var digitReg = /^[0-9]+$/ // 数字
    ;
    var letterUpperReg = /^[A-Z]+$/ // 字母
    ;
    var letterLowerReg = /^[a-z]+$/ // 字母
    ;
    var rtl = (0, _index.useRtl)();
    var rtlClasses = (0, _classnames.default)((0, _define_property._)({}, "".concat(classPrefix, "-rtl"), rtl));
    var classes = (0, _classnames.default)(classPrefix, width ? "".concat(classPrefix, "-width") : '', rtlClasses, className);
    (0, _react.useEffect)(function() {
        setContentCopy(content);
        (0, _taro.nextTick)(function() {
            getSymbolInfo();
            getReference();
        });
        return function() {
            setCacled(false);
        };
    }, [
        content
    ]);
    // 获取省略号宽度
    var getSymbolInfo = function getSymbolInfo() {
        return (0, _async_to_generator._)(function() {
            var refe;
            return (0, _ts_generator._)(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            (0, _getrect.getRectInMultiPlatform)(symbolContain === null || symbolContain === void 0 ? void 0 : symbolContain.current)
                        ];
                    case 1:
                        refe = _state.sent();
                        symbolTextWidth.current = refe.width ? Math.ceil(refe.width) : Math.ceil(widthBase.current[0] * 0.7921);
                        return [
                            2
                        ];
                }
            });
        })();
    };
    var symbolText = function symbolText() {
        if (direction === 'end' || direction === 'middle') {
            return "".concat(symbol).concat(expandText);
        }
        return "".concat(symbol).concat(expandText).concat(symbol);
    };
    var getReference = function getReference() {
        return (0, _async_to_generator._)(function() {
            var query;
            return (0, _ts_generator._)(this, function(_state) {
                query = (0, _taro.createSelectorQuery)();
                query.select("#root".concat(refRandomId.current)) && query.select("#root".concat(refRandomId.current)).fields({
                    computedStyle: [
                        'width',
                        'height',
                        'lineHeight',
                        'paddingTop',
                        'paddingBottom',
                        'fontSize'
                    ]
                }, function(res) {
                    if (!res) return;
                    lineH.current = pxToNumber(res.lineHeight === 'normal' ? lineHeight : res.lineHeight);
                    maxHeight.current = Math.floor(lineH.current * (Number(rows) + 0.5) + pxToNumber(res.paddingTop) + pxToNumber(res.paddingBottom));
                    originHeight.current = pxToNumber(res.height);
                    widthRef.current = res.width;
                    // 设置基础字符
                    var bsize = pxToNumber(res.fontSize);
                    widthBase.current = [
                        bsize,
                        bsize * 0.72,
                        bsize * 0.53,
                        bsize * 0.4,
                        bsize * 0.75
                    ];
                    if (rootContain.current && rootContain.current.style) {
                        rootContain.current.style.fontSize = "".concat(bsize, "px");
                    }
                    calcEllipse();
                }).exec();
                return [
                    2
                ];
            });
        })();
    };
    // 计算省略号的位置
    var calcEllipse = function calcEllipse() {
        return (0, _async_to_generator._)(function() {
            var refe, rowNum, end, end1, start;
            return (0, _ts_generator._)(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            (0, _getrect.getRectInMultiPlatform)(rootContain.current)
                        ];
                    case 1:
                        refe = _state.sent();
                        if (refe.height <= maxHeight.current) {
                            setExceeded(false);
                            setCacled(true);
                        } else {
                            rowNum = Math.floor(content.length / (originHeight.current / lineH.current - 1)); // 每行的字数
                            if (direction === 'middle') {
                                end = content.length;
                                ellipsis.current.leading = tailorContent(0, rowNum * (Number(rows) + 0.5), 'end');
                                ellipsis.current.tailing = tailorContent(content.length - rowNum * (Number(rows) + 0.5), end, 'start');
                            } else if (direction === 'end') {
                                end1 = rowNum * (Number(rows) + 0.5);
                                ellipsis.current.leading = tailorContent(0, end1);
                            } else {
                                start = content.length - rowNum * (Number(rows) + 0.5) - 5;
                                ellipsis.current.tailing = tailorContent(start, content.length);
                            }
                            // 进行兜底判断，是否符合要求
                            assignContent();
                            setTimeout(function() {
                                verifyEllipsis();
                            }, 100);
                        }
                        return [
                            2
                        ];
                }
            });
        })();
    };
    // 验证省略号
    var verifyEllipsis = function verifyEllipsis1() {
        return (0, _async_to_generator._)(function() {
            var refe, _ellipsis_current_leading, _ellipsis_current, _ellipsis_current_leading1, _ellipsis_current1, _ellipsis_current2, _ellipsis_current_tailing, _ellipsis_current3;
            return (0, _ts_generator._)(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            (0, _getrect.getRectInMultiPlatformWithoutCache)(rootContain.current)
                        ];
                    case 1:
                        refe = _state.sent();
                        if (refe && refe.height && refe.height > maxHeight.current) {
                            if (direction === 'end') {
                                ;
                                ellipsis.current.leading = (_ellipsis_current1 = ellipsis.current) === null || _ellipsis_current1 === void 0 ? void 0 : (_ellipsis_current_leading1 = _ellipsis_current1.leading) === null || _ellipsis_current_leading1 === void 0 ? void 0 : _ellipsis_current_leading1.slice(0, (((_ellipsis_current = ellipsis.current) === null || _ellipsis_current === void 0 ? void 0 : (_ellipsis_current_leading = _ellipsis_current.leading) === null || _ellipsis_current_leading === void 0 ? void 0 : _ellipsis_current_leading.length) || 0) - 1);
                            } else {
                                ;
                                ellipsis.current.tailing = (_ellipsis_current3 = ellipsis.current) === null || _ellipsis_current3 === void 0 ? void 0 : (_ellipsis_current_tailing = _ellipsis_current3.tailing) === null || _ellipsis_current_tailing === void 0 ? void 0 : _ellipsis_current_tailing.slice(1, (_ellipsis_current2 = ellipsis.current) === null || _ellipsis_current2 === void 0 ? void 0 : _ellipsis_current2.tailing.length);
                            }
                            assignContent();
                            setTimeout(function() {
                                verifyEllipsis();
                            }, 100);
                        }
                        return [
                            2
                        ];
                }
            });
        })();
    };
    var assignContent = function assignContent() {
        var _ellipsis_current, _ellipsis_current1, _ellipsis_current2, _ellipsis_current3;
        var newContent = "".concat(((_ellipsis_current = ellipsis.current) === null || _ellipsis_current === void 0 ? void 0 : _ellipsis_current.leading) || '').concat(((_ellipsis_current1 = ellipsis.current) === null || _ellipsis_current1 === void 0 ? void 0 : _ellipsis_current1.leading) ? symbol : '').concat(expandText || '').concat(((_ellipsis_current2 = ellipsis.current) === null || _ellipsis_current2 === void 0 ? void 0 : _ellipsis_current2.tailing) ? symbol : '').concat(((_ellipsis_current3 = ellipsis.current) === null || _ellipsis_current3 === void 0 ? void 0 : _ellipsis_current3.tailing) || '');
        setContentCopy(newContent);
    };
    // 计算省略号
    var tailorContent = function tailorContent(left, right) {
        var type = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : '';
        var threeDotWidth = symbolTextWidth.current;
        var direc = direction === 'middle' && type ? type : direction;
        setExceeded(true);
        var widthPart = -1;
        var start = left;
        var end = right;
        var cutoff = 0;
        var marking = 0;
        if (direc === 'end') {
            marking = start;
            cutoff = end;
        } else {
            marking = end;
            cutoff = start;
        }
        var contentWidth = pxToNumber(widthRef.current) * Number(rows) - threeDotWidth;
        var contentPartWidth = direction === 'middle' ? contentWidth / 2 : contentWidth;
        while(widthPart < contentPartWidth){
            var zi = content[marking];
            if (chineseReg.test(zi)) {
                widthPart = Number(widthPart + widthBase.current[0]);
            } else if (letterUpperReg.test(zi)) {
                widthPart = Number(widthPart + widthBase.current[1]);
            } else if (letterLowerReg.test(zi)) {
                widthPart = Number(widthPart + widthBase.current[2]);
            } else if (digitReg.test(zi)) {
                widthPart = Number(widthPart + widthBase.current[3]);
            } else {
                widthPart = Number(widthPart + widthBase.current[4]);
            }
            cutoff = marking;
            direc === 'end' ? marking++ : marking--;
        }
        if (direc === 'end') {
            return content.slice(0, cutoff);
        }
        return content.slice(cutoff, end);
    };
    var pxToNumber = function pxToNumber(value) {
        if (!value) return 0;
        var match = value.match(/^\d*(\.\d*)?/);
        return match ? Number(match[0]) : 0;
    };
    var clickHandle = function clickHandle(type) {
        if (type === 1) {
            setExpanded(true);
            onChange && onChange('expand');
        } else {
            setExpanded(false);
            onChange && onChange('collapse');
        }
    };
    var handleClick = function handleClick() {
        onClick && onClick();
    };
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
        className: classes,
        onClick: handleClick,
        ref: root,
        id: "root".concat(refRandomId.current)
    }, rest), /*#__PURE__*/ _react.default.createElement(_components.View, null, !exceeded ? /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-ellipsis-wordbreak",
        style: {
            width: "".concat(!Number.isNaN(Number(width)) ? "".concat(width, "px") : 'auto'),
            minHeight: '20px',
            height: cacled ? 'auto' : '20px',
            overflow: 'hidden'
        }
    }, content) : null, exceeded && !expanded ? /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-ellipsis-wordbreak",
        style: {
            width: "".concat(!Number.isNaN(Number(width)) ? "".concat(width, "px") : 'auto')
        }
    }, (_ellipsis_current = ellipsis.current) === null || _ellipsis_current === void 0 ? void 0 : _ellipsis_current.leading, ((_ellipsis_current1 = ellipsis.current) === null || _ellipsis_current1 === void 0 ? void 0 : _ellipsis_current1.leading) && symbol, expandText ? /*#__PURE__*/ _react.default.createElement("span", {
        className: "nut-ellipsis-text",
        onClick: function onClick(e) {
            e.stopPropagation();
            clickHandle(1);
        }
    }, expandText) : null, ((_ellipsis_current2 = ellipsis.current) === null || _ellipsis_current2 === void 0 ? void 0 : _ellipsis_current2.tailing) && symbol, (_ellipsis_current3 = ellipsis.current) === null || _ellipsis_current3 === void 0 ? void 0 : _ellipsis_current3.tailing) : null, exceeded && expanded ? /*#__PURE__*/ _react.default.createElement(_components.View, {
        style: {
            width: "".concat(!Number.isNaN(Number(width)) ? "".concat(width, "px") : 'auto')
        }
    }, content, expandText ? /*#__PURE__*/ _react.default.createElement("span", {
        className: "nut-ellipsis-text",
        onClick: function onClick(e) {
            e.stopPropagation();
            clickHandle(2);
        }
    }, collapseText) : null) : null)), /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-ellipsis-copy",
        ref: rootContain,
        id: "rootContain".concat(refRandomId.current),
        style: {
            width: "".concat(widthRef.current)
        }
    }, /*#__PURE__*/ _react.default.createElement(_components.View, null, contentCopy)), /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-ellipsis-copy",
        ref: symbolContain,
        id: "symbolContain".concat(refRandomId.current),
        style: {
            display: 'inline'
        }
    }, symbolText()));
};
Ellipsis.displayName = 'NutEllipsis';
