import dayjs from 'dayjs';
export function toShortString(str, keepDigitals, maxLength) {
    if (keepDigitals === void 0) { keepDigitals = 6; }
    if (maxLength === void 0) { maxLength = 16; }
    var stringify = (str || '').toString();
    if (stringify.length <= maxLength) {
        return stringify;
    }
    return "".concat(stringify.slice(0, keepDigitals), "\u2026").concat(stringify.slice(-keepDigitals));
}
/**
 *
 * @param time In second
 * @param now
 * @param shouldTruncate
 * @returns
 */
export function timeAgo(time, now, shouldTruncate) {
    if (now === void 0) { now = Date.now(); }
    time = +time * 1000;
    var d = new Date(time);
    var diff = parseInt(((now - d.valueOf()) / 1000).toString(), 10);
    if (diff < 1) {
        return '0 sec ago';
    }
    else if (diff < 60) {
        return "".concat(diff, " ").concat(diff === 1 ? 'sec' : 'secs', " ago");
    }
    else if (diff < 3600) {
        // less 1 hour
        var min = Math.floor(diff / 60);
        return "".concat(min, " ").concat(min === 1 ? 'min' : 'mins', " ago");
    }
    else if (diff < 86400) {
        // less 1 day
        var hr = Math.floor(diff / 3600);
        var min = Math.floor((diff / 60) % 60);
        if (shouldTruncate) {
            return "".concat(hr, " ").concat(hr === 1 ? 'hr' : 'hrs', " ago");
        }
        else {
            return "".concat(hr, " ").concat(hr === 1 ? 'hr' : 'hrs').concat(min === 0 ? '' : min === 1 ? ' 1 min' : " ".concat(min, " mins"), " ago");
        }
    }
    else if (diff < 90000) {
        // less 1 day 60 min
        var day = Math.floor(diff / 86400);
        var min = Math.floor((diff / 60) % 60);
        if (shouldTruncate) {
            return "".concat(day, " ").concat(day === 1 ? 'day' : 'days', " ago");
        }
        else {
            return "".concat(day, " ").concat(day === 1 ? 'day' : 'days').concat(min === 0 ? '' : min === 1 ? ' 1 min' : " ".concat(min, " mins"), " ago");
        }
    }
    else {
        var day = Math.floor(diff / 86400);
        var hr = Math.floor((diff / 60 / 60) % 24);
        if (shouldTruncate) {
            return "".concat(day, " ").concat(day === 1 ? 'day' : 'days', " ago");
        }
        else {
            return "".concat(day, " ").concat(day === 1 ? 'day' : 'days').concat(hr === 0 ? '' : hr === 1 ? ' 1 hr' : " ".concat(hr, " hrs"), " ago");
        }
    }
}
export function parseTimeToUtc(time, cFormat, hasUTCText) {
    if (hasUTCText === void 0) { hasUTCText = true; }
    if (arguments.length === 0) {
        return '';
    }
    if (!time) {
        return '';
    }
    if ((time + '').length >= 10) {
        time = +time * 1000;
    }
    var format = cFormat || 'YYYY-MM-DD HH:mm:ss';
    var date = dayjs(time);
    if (hasUTCText) {
        return "".concat(date.format(format), " (+UTC)");
    }
    else {
        return "".concat(date.format(format));
    }
}
export var generateUUID = function () {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c === 'x' ? r : (r & 0x7) | 0x8).toString(16);
    });
    return uuid;
};
