import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useEffect, useRef, useState } from "react";
import classNames from "classnames";
import { ComponentDefaults } from "../../utils/typings";
import { useRtl } from "../configprovider";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    percent: 0,
    showText: false,
    animated: false,
    lazy: false,
    delay: 0
});
export var Progress = function(props) {
    var rtl = useRtl();
    var _ref = _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 = _object_without_properties(_ref, [
        "className",
        "style",
        "percent",
        "background",
        "color",
        "strokeWidth",
        "showText",
        "animated",
        "children",
        "lazy",
        "delay"
    ]);
    var classPrefix = 'nut-progress';
    var _obj;
    var classesInner = classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-inner"), true), _define_property(_obj, "".concat(classPrefix, "-active"), animated), _obj));
    var stylesOuter = {
        height: "".concat(strokeWidth, "px"),
        background: background
    };
    var _useState = _sliced_to_array(useState(0), 2), displayPercent = _useState[0], setDispalyPercent = _useState[1];
    var stylesInner = {
        width: "".concat(displayPercent, "%"),
        background: color || '#FF0F23'
    };
    useEffect(function() {
        setDispalyPercent(percent);
    }, [
        percent
    ]);
    var _useState1 = _sliced_to_array(useState(false), 2), intersecting = _useState1[0], setIntersecting = _useState1[1];
    var progressRef = useRef(null);
    var observer = useRef(null);
    var initObserver = function() {
        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() {
        observer.current.disconnect && observer.current.disconnect();
    };
    useEffect(function() {
        if (lazy) {
            setTimeout(function() {
                if (intersecting) {
                    setDispalyPercent(percent);
                } else {
                    setDispalyPercent(0);
                }
            }, delay);
        }
    }, [
        intersecting
    ]);
    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.createElement("div", _object_spread({
        ref: progressRef,
        className: classNames(classPrefix, className),
        style: style
    }, rest), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-outer"),
        style: stylesOuter
    }, /*#__PURE__*/ React.createElement("div", {
        className: classesInner,
        style: stylesInner
    }, showText && /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-text"),
        style: rtl ? {
            right: "".concat(displayPercent, "%")
        } : {
            left: "".concat(displayPercent, "%")
        }
    }, children || /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-text-inner"),
        style: {
            background: color
        }
    }, percent, "%")))));
};
Progress.displayName = 'NutProgress';
