/*
 * Copyright 2021 The Kubernetes Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import React from 'react';
import prettyPrintDuration from 'pretty-ms';
export default class Timer extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = Timer.getDerivedStateFromProps(props);
    }
    static getDerivedStateFromProps(props, state) {
        const counter = state ? Timer.updateCountup(props, state) : null;
        if (!state) {
            return {
                counter,
                durationDom: React.createRef()
            };
        }
        else {
            return Object.assign({}, state, { counter });
        }
    }
    componentWillUnmount() {
        if (this.state.counter) {
            clearInterval(this.state.counter);
        }
    }
    static newCountup(startTime, durationDom) {
        if (durationDom.current) {
            durationDom.current.innerText = '';
        }
        return setInterval(() => {
            const millisSinceStart = (~~(Date.now() - startTime) / 1000) * 1000;
            if (millisSinceStart > 0 && durationDom.current) {
                durationDom.current.innerText = prettyPrintDuration(millisSinceStart);
            }
        }, 1000);
    }
    static updateCountup(props, state) {
        const counter = props.status === 'processing' ? state.counter || Timer.newCountup(props.startTime, state.durationDom) : undefined;
        if (!counter && state.counter) {
            clearInterval(state.counter);
            if (state.durationDom.current) {
                state.durationDom.current.innerText = '';
            }
        }
        return counter;
    }
    render() {
        const completed = !!this.props.endTime;
        const showingDate = this.props.showDate && completed;
        const now = completed ? this.props.endTime : Date.now();
        const duration = completed ? prettyPrintDuration(Math.max(0, now - this.props.startTime)) : undefined;
        const noParen = !showingDate || !duration;
        const openParen = noParen ? '' : '(';
        const closeParen = noParen ? '' : ')';
        const children = [openParen, duration, closeParen].filter(Boolean);
        return (React.createElement("span", { className: this.props.className },
            showingDate && new Date(this.props.startTime).toLocaleTimeString(),
            React.createElement("span", { className: this.props.innerClassName },
                this.props.status === 'processing' && React.createElement("span", { ref: this.state.durationDom }),
                children)));
    }
}
//# sourceMappingURL=Timer.js.map