"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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DurationTimer = exports.renderDurationStringFromMillis = void 0;
const React = __importStar(require("react"));
const luxon_1 = require("luxon");
const material_1 = require("@mui/material");
const addPadding = (num, addColon = true) => {
    const res = num.length === 1 ? `0${num}` : num;
    if (res === '') {
        return '';
    }
    if (addColon) {
        return `${res}:`;
    }
    return res;
};
const renderDurationStringFromMillis = (millis, showSign = false) => {
    const absMillis = Math.abs(millis);
    const sign = millis < 0 ? '-' : '+';
    const obj = luxon_1.Duration.fromMillis(absMillis)
        .shiftTo('hours', 'minutes', 'seconds', 'milliseconds')
        .toObject();
    const hours = (obj.hours || 0) > 0 ? `${obj.hours}` : '';
    const minutes = `${obj.minutes}`;
    const seconds = `${obj.seconds}`;
    const fractionalSecond = Math.floor(obj.milliseconds ? obj.milliseconds / 10 : 0);
    const fract = `${fractionalSecond}`;
    return `${(millis && showSign) ? sign : ''}${hours}${hours.length > 0 ? ':' : ''}${hours.length > 0 ? addPadding(minutes, false) : minutes}:${addPadding(seconds, false)}.${addPadding(fract, false)}`;
};
exports.renderDurationStringFromMillis = renderDurationStringFromMillis;
class DurationTimer extends React.Component {
    constructor() {
        super(...arguments);
        this.timer = setTimeout(() => 69, 100);
        this.state = {
            durationString: '',
        };
    }
    updateTime() {
        this.setState({ durationString: this.getTimeString() }, () => {
            if (this.props.activeAt !== 0) {
                this.timer = setTimeout(() => this.updateTime(), 1000 / 60);
            }
        });
    }
    componentDidMount() {
        this.updateTime();
    }
    componentWillUnmount() {
        clearTimeout(this.timer);
    }
    componentDidUpdate(prevProps) {
        if (prevProps.activeAt !== this.props.activeAt ||
            prevProps.milliseconds !== this.props.milliseconds) {
            clearTimeout(this.timer);
            this.updateTime();
        }
    }
    getTimeString() {
        const { activeAt } = this.props;
        const currentMilliseconds = this.props.milliseconds;
        const addedTime = activeAt > 0 ? new Date().getTime() - activeAt : 0;
        const num = currentMilliseconds + addedTime;
        return (0, exports.renderDurationStringFromMillis)(num, Boolean(this.props.showSign));
    }
    renderLargeType() {
        const str = this.getDurationString();
        const parts = str.split('.');
        return (React.createElement(React.Fragment, null,
            React.createElement(material_1.Typography, { variant: "h3", sx: {
                    fontFamily: 'timer',
                    display: 'inline-block'
                } }, parts[0] || ''),
            React.createElement(material_1.Typography, { variant: "h5", sx: {
                    fontFamily: 'timer',
                    display: 'inline-block'
                } },
                ".",
                parts[1] || '')));
    }
    getDurationString() {
        if (this.props.activeAt > 0) {
            return this.state.durationString;
        }
        return (0, exports.renderDurationStringFromMillis)(this.props.milliseconds, Boolean(this.props.showSign));
    }
    renderNormal() {
        return (React.createElement(React.Fragment, null, this.getDurationString()));
    }
    render() {
        switch (this.props.type) {
            case 'large': {
                return this.renderLargeType();
            }
            case 'normal':
            default: {
                return this.renderNormal();
            }
        }
    }
}
exports.DurationTimer = DurationTimer;
//# sourceMappingURL=duration_timer.js.map