"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 _typings = require("../../utils/typings");
var _configprovider = require("../configprovider");
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, _configprovider.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, rest = (0, _object_without_properties._)(_ref, [
        "className",
        "style",
        "percent",
        "background",
        "color",
        "strokeWidth",
        "showText",
        "animated",
        "children",
        "lazy",
        "delay"
    ]);
    var classPrefix = 'nut-progress';
    var _obj;
    var classesInner = (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-inner"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-active"), animated), _obj));
    var stylesOuter = {
        height: "".concat(strokeWidth, "px"),
        background: background
    };
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(0), 2), displayPercent = _useState[0], setDispalyPercent = _useState[1];
    var stylesInner = {
        width: "".concat(displayPercent, "%"),
        background: color || '#FF0F23'
    };
    (0, _react.useEffect)(function() {
        setDispalyPercent(percent);
    }, [
        percent
    ]);
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), intersecting = _useState1[0], setIntersecting = _useState1[1];
    var progressRef = (0, _react.useRef)(null);
    var observer = (0, _react.useRef)(null);
    var initObserver = function initObserver() {
        var options = {
            threshold: [
                0
            ],
            rootMargin: '0px'
        };
        observer.current = new IntersectionObserver(function(entires, self) {
            entires.forEach(function(item) {
                setIntersecting(item.isIntersecting);
            });
        }, options);
        observer.current.observe(progressRef.current);
    };
    var resetObserver = function resetObserver() {
        observer.current.disconnect && observer.current.disconnect();
    };
    (0, _react.useEffect)(function() {
        if (lazy) {
            setTimeout(function() {
                if (intersecting) {
                    setDispalyPercent(percent);
                } else {
                    setDispalyPercent(0);
                }
            }, delay);
        }
    }, [
        intersecting
    ]);
    (0, _react.useEffect)(function() {
        lazy && initObserver();
        var timer = null;
        if (delay) {
            setDispalyPercent(0);
            timer = setTimeout(function() {
                setDispalyPercent(percent);
            }, delay);
        }
        return function() {
            lazy && resetObserver();
            timer && clearTimeout(timer);
        };
    }, []);
    return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
        ref: progressRef,
        className: (0, _classnames.default)(classPrefix, className),
        style: style
    }, rest), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-outer"),
        style: stylesOuter
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: classesInner,
        style: stylesInner
    }, showText && /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-text"),
        style: rtl ? {
            right: "".concat(displayPercent, "%")
        } : {
            left: "".concat(displayPercent, "%")
        }
    }, children || /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-text-inner"),
        style: {
            background: color
        }
    }, percent, "%")))));
};
Progress.displayName = 'NutProgress';
