"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Progress", {
    enumerable: true,
    get: function() {
        return Progress;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
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 _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _taro = /*#__PURE__*/ _interop_require_default._(require("@tarojs/taro"));
var _components = require("@tarojs/components");
var _pxtransform = require("../../utils/taro/px-transform");
var _typings = require("../../utils/typings");
var _index = require("../configprovider/index");
var _useuuid = require("../../hooks/use-uuid");
var _platform = require("../../utils/taro/platform");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    percent: 0,
    showText: false,
    animated: false,
    lazy: false,
    delay: 0
});
var Progress = function Progress(props) {
    var rtl = (0, _index.useRtl)();
    var _ref = (0, _object_spread._)({}, defaultProps, props), className = _ref.className, style = _ref.style, percent = _ref.percent, background = _ref.background, color = _ref.color, strokeWidth = _ref.strokeWidth, showText = _ref.showText, animated = _ref.animated, children = _ref.children, lazy = _ref.lazy, delay = _ref.delay, showInfo = _ref.showInfo, borderRadius = _ref.borderRadius, fontSize = _ref.fontSize, activeColor = _ref.activeColor, backgroundColor = _ref.backgroundColor, active = _ref.active, activeMode = _ref.activeMode, duration = _ref.duration, onActiveEnd = _ref.onActiveEnd, rest = (0, _object_without_properties._)(_ref, [
        "className",
        "style",
        "percent",
        "background",
        "color",
        "strokeWidth",
        "showText",
        "animated",
        "children",
        "lazy",
        "delay",
        "showInfo",
        "borderRadius",
        "fontSize",
        "activeColor",
        "backgroundColor",
        "active",
        "activeMode",
        "duration",
        "onActiveEnd"
    ]);
    var classPrefix = 'nut-progress';
    var _props_showText, _ref1;
    var effectiveShowText = (_ref1 = (_props_showText = props.showText) !== null && _props_showText !== void 0 ? _props_showText : showInfo) !== null && _ref1 !== void 0 ? _ref1 : defaultProps.showText;
    var _props_color, _ref2;
    var effectiveColor = (_ref2 = (_props_color = props.color) !== null && _props_color !== void 0 ? _props_color : activeColor) !== null && _ref2 !== void 0 ? _ref2 : defaultProps.color;
    var _props_background, _ref3;
    var effectiveBgColor = (_ref3 = (_props_background = props.background) !== null && _props_background !== void 0 ? _props_background : backgroundColor) !== null && _ref3 !== void 0 ? _ref3 : defaultProps.background;
    var _props_animated, _ref4;
    var effectiveAnimated = (_ref4 = (_props_animated = props.animated) !== null && _props_animated !== void 0 ? _props_animated : active) !== null && _ref4 !== void 0 ? _ref4 : defaultProps.animated;
    var _obj;
    var classesInner = (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-inner"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-active"), effectiveAnimated), _obj));
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(percent), 2), displayPercent = _useState[0], setDispalyPercent = _useState[1];
    var getStyles = function getStyles() {
        // 基础样式
        var baseStyles = {
            height: strokeWidth && (0, _pxtransform.pxTransform)(Number(strokeWidth)),
            borderRadius: borderRadius && (0, _pxtransform.pxTransform)(parseInt(borderRadius.toString()))
        };
        var transitionStyle = {
            transition: "width ".concat(duration || 300, "ms ease-in-out")
        };
        return {
            outer: (0, _object_spread._)({
                width: '100%',
                backgroundColor: effectiveBgColor
            }, baseStyles),
            inner: (0, _object_spread._)({
                width: "".concat(displayPercent, "%"),
                background: effectiveColor || 'var(--nutui-color-primary, #ff0f23)'
            }, baseStyles, transitionStyle)
        };
    };
    var _getStyles = getStyles(), stylesOuter = _getStyles.outer, stylesInner = _getStyles.inner;
    var handlePercent = function handlePercent() {
        var timer = null;
        if (delay) {
            setDispalyPercent(0);
            timer = setTimeout(function() {
                setDispalyPercent(percent);
            }, delay);
        }
        return function() {
            lazy && resetObserver();
            timer && clearTimeout(timer);
        };
    };
    (0, _react.useEffect)(function() {
        var timer = null;
        if (activeMode === 'backwards') {
            setDispalyPercent(0);
            timer = setTimeout(function() {
                setDispalyPercent(percent);
            }, duration || 300);
        } else {
            setDispalyPercent(percent);
        }
        return function() {
            if (timer) {
                clearTimeout(timer);
            }
        };
    }, [
        percent,
        activeMode,
        duration
    ]);
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), intersecting = _useState1[0], setIntersecting = _useState1[1];
    var progressRef = (0, _react.useRef)(null);
    var webObserver = (0, _react.useRef)(null);
    var uuid = (0, _useuuid.useUuid)();
    var selector = "".concat(classPrefix, "-lazy-").concat(uuid);
    var resetObserver = function resetObserver() {
        var observer = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null;
        if ((0, _platform.web)()) {
            webObserver.current.disconnect && webObserver.current.disconnect();
        } else {
            observer && observer.disconnect();
        }
    };
    (0, _react.useEffect)(function() {
        if (lazy) {
            setTimeout(function() {
                if (intersecting) {
                    setDispalyPercent(percent);
                } else {
                    setDispalyPercent(0.01);
                }
            }, delay);
        }
    }, [
        intersecting
    ]);
    var handleWebObserver = function handleWebObserver() {
        /// web环境
        if (lazy) {
            webObserver.current = new IntersectionObserver(function(entires, self) {
                entires.forEach(function(item) {
                    setIntersecting(item.isIntersecting);
                });
            }, {
                threshold: [
                    0
                ],
                rootMargin: '0px'
            });
            webObserver.current.observe(progressRef.current);
        }
        handlePercent();
    };
    var handleOtherObserver = function handleOtherObserver() {
        // 非web环境
        var observer = null;
        if (lazy) {
            observer = _taro.default.createIntersectionObserver(_taro.default.getCurrentInstance().page, {
                thresholds: [
                    0
                ],
                observeAll: true
            });
            observer.relativeToViewport({
                top: 0
            }).observe("#".concat(selector), function(res) {
                setIntersecting(res.intersectionRatio > 0);
            });
        }
        handlePercent();
    };
    (0, _react.useEffect)(function() {
        if ((0, _platform.web)()) {
            handleWebObserver();
        } else if (!(0, _platform.harmony)()) {
            handleOtherObserver();
        }
    }, []);
    var getTextStyle = function getTextStyle() {
        return rtl ? {
            right: '100%'
        } : {
            left: '100%'
        };
    };
    var computeRight = function computeRight() {
        if (children) {
            return 0;
        }
        if (!(0, _platform.harmony)()) {
            return Math.floor("".concat(percent, "%").length * 9 / 2);
        }
        return Math.floor(("".concat(percent, "%").length * 9 + 4) / 2);
    };
    var computeInnerStyle = function computeInnerStyle() {
        var style = {
            backgroundColor: effectiveColor || 'var(--nutui-color-primary, #ff0f23)',
            fontSize: fontSize && (0, _pxtransform.pxTransform)(parseInt(fontSize.toString()))
        };
        if ((0, _platform.harmony)()) {
            style.width = (0, _platform.harmony)() ? (0, _pxtransform.pxTransform)("".concat(percent, "%").length * 9 + 4) : "".concat(percent, "%").length * 9 + 4;
        }
        return style;
    };
    return /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread._)({
        ref: progressRef,
        id: selector,
        className: (0, _classnames.default)(classPrefix, className),
        style: style
    }, rest), /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-outer"),
        style: stylesOuter
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: classesInner,
        style: (0, _object_spread_props._)((0, _object_spread._)({}, stylesInner), {
            position: 'relative'
        }),
        onTransitionEnd: onActiveEnd
    })), effectiveShowText && /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-text"),
        style: {
            fontSize: fontSize && parseInt(fontSize.toString())
        }
    }, children || "".concat(percent, "%")));
};
Progress.displayName = 'NutProgress';
