"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const components_1 = require("../themes/components");
const addZero = (n) => Math.trunc(n).toString().padStart(2, '0');
const Clock = () => {
    const startedAt = react_redux_1.useSelector((state) => state.room.startedAt);
    const requestRef = react_1.default.useRef();
    const [timeDiff, setTimeDiff] = react_1.default.useState(0);
    const timeoutRef = react_1.default.useRef();
    react_1.useEffect(() => {
        if (startedAt) {
            const animate = () => {
                const diff = (new Date().getTime() - new Date(startedAt).getTime()) / 1000;
                setTimeDiff(diff);
                timeoutRef.current = setTimeout(() => {
                    if (requestRef.current) {
                        requestRef.current = requestAnimationFrame(animate);
                    }
                }, 500);
            };
            requestRef.current = requestAnimationFrame(animate);
        }
        return () => { clearTimeout(timeoutRef.current); cancelAnimationFrame(requestRef.current); };
    }, [startedAt]);
    return (react_1.default.createElement(components_1.Text, { color: "textPrimary.400" },
        timeDiff >= 3600 && (react_1.default.createElement(react_1.default.Fragment, null,
            (addZero((timeDiff / 3600))),
            "h",
            ' ')),
        (addZero((timeDiff % 3600) / 60)),
        "m",
        ' ',
        (addZero(timeDiff % 60)),
        "s"));
};
exports.default = Clock;
//# sourceMappingURL=Clock.js.map