/* lib/terminal/utilities/humanTime - A utility to generate human readable time sequences. */ /*eslint no-console: 0*/ import common from "../../common/common.js";
import vars from "./vars.js";
// converting time durations into something people read
const humanTime = function terminal_utilities_humanTime(finished) {
    const numberString = function terminal_utilities_humanTime_numberString(numb) {
        const str = numb.toString();
        return str.length < 2 ? `0${str}` : str;
    }, plural = function terminal_utilities_humanTime_plural(x, y) {
        if (y === " second") {
            if (x === 1n) {
                if (nanosecond === 0n) {
                    return `01.${nanoString} second `;
                }
                return `01.${nanoString} seconds `;
            }
            return `${numberString(x)}.${nanoString} seconds `;
        }
        if (x === 1n) {
            return `${numberString(x) + y} `;
        }
        return `${numberString(x) + y}s `;
    }, elapsed = process.hrtime.bigint() - vars.environment.startTime, factorSec = BigInt(1e9), factorMin = 60n * factorSec, factorHour = 3600n * factorSec, hours = elapsed / factorHour, elapsedHour = hours * factorHour, minutes = (elapsed - elapsedHour) / factorMin, elapsedMin = minutes * factorMin, seconds = (elapsed - (elapsedHour + elapsedMin)) / factorSec, nanosecond = elapsed - (elapsedHour + elapsedMin + seconds * factorSec), nanoString = function terminal_utilities_humanTime_nanoString() {
        let nano = nanosecond.toString(), a = nano.length;
        if (a < 9) {
            do {
                nano = `0${nano}`;
                a = a + 1;
            }while (a < 9)
        }
        return nano;
    }(), secondString = finished === true ? plural(seconds, " second") : `${numberString(seconds)}.${nanoString}`, minuteString = finished === true ? plural(minutes, " minute") : numberString(minutes), hourString = finished === true ? plural(hours, " hour") : numberString(hours);
    //last line for additional instructions without bias to the timer
    if (finished === true) {
        const finalMem = common.prettyBytes(process.memoryUsage.rss()), finalTime = hourString + minuteString + secondString, // eslint-disable-next-line
        logger = console.log;
        logger("");
        logger(`${finalMem} of memory consumed`);
        logger(`${finalTime}total time`);
        logger("");
    }
    return `${vars.text.cyan}[${hourString}:${minuteString}:${secondString}]${vars.text.none} `;
};
export default humanTime;
