var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React from 'react';
import { clamp, maxProgressValue, minProgressValue } from '../Loader/Loader';
import { StyledProgressBar, StyledProgressBarLoader, } from './ProgressBar.styles';
var finalAnimationDuration = 900;
/**

 Progress bars visually show system loading progress, such as uploading a file
 or loading a page, as well as user progress, like filling out a form.

 @since 10.19.0

 @see [Storybook](https://procore.github.io/core/latest/?path=/story/demos-progressbar--demo)

 @see [Design Guidelines](https://design.procore.com/progress-bar)

 */
export var ProgressBar = React.forwardRef(function ProgressBar(_a, ref) {
    var _b = _a.animated, animated = _b === void 0 ? false : _b, value = _a.value, props = __rest(_a, ["animated", "value"]);
    var isProgressValueComplete = value >= maxProgressValue;
    var _c = React.useState(false), isFinalized = _c[0], setFinalized = _c[1];
    var _d = React.useState(isProgressValueComplete), isProgressComplete = _d[0], setProgressComplete = _d[1];
    var didMountRef = React.useRef(false);
    React.useEffect(function () {
        var isFinalized = didMountRef.current && isProgressValueComplete;
        var timerId = isFinalized
            ? window.setTimeout(function () {
                setFinalized(false);
            }, finalAnimationDuration)
            : null;
        setProgressComplete(isProgressValueComplete);
        setFinalized(isFinalized);
        if (!didMountRef.current) {
            didMountRef.current = true;
        }
        return function () {
            timerId && clearTimeout(timerId);
        };
    }, [isProgressValueComplete]);
    return (React.createElement(StyledProgressBar, __assign({ "$complete": isProgressComplete, "$finalized": isFinalized, ref: ref, role: "progressbar", "aria-valuenow": clamp(value), "aria-valuemin": minProgressValue, "aria-valuemax": maxProgressValue }, props),
        React.createElement(StyledProgressBarLoader, { value: value, animated: animated })));
});
ProgressBar.displayName = 'ProgressBar';
//# sourceMappingURL=ProgressBar.js.map