import { LogType } from "../types.js";
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
const LOG_SYMBOL = Symbol('rspack logger raw log method');
const TIMERS_SYMBOL = Symbol('rspack logger times');
const TIMERS_AGGREGATES_SYMBOL = Symbol('rspack logger aggregated times');
let _LOG_SYMBOL = LOG_SYMBOL, _TIMERS_SYMBOL = TIMERS_SYMBOL, _TIMERS_AGGREGATES_SYMBOL = TIMERS_AGGREGATES_SYMBOL;
class RspackLogger {
    error(...args) {
        this[LOG_SYMBOL](LogType.error, args);
    }
    warn(...args) {
        this[LOG_SYMBOL](LogType.warn, args);
    }
    info(...args) {
        this[LOG_SYMBOL](LogType.info, args);
    }
    log(...args) {
        this[LOG_SYMBOL](LogType.log, args);
    }
    debug(...args) {
        this[LOG_SYMBOL](LogType.debug, args);
    }
    assert(assertion, ...args) {
        if (!assertion) this[LOG_SYMBOL](LogType.error, args);
    }
    trace() {
        this[LOG_SYMBOL](LogType.trace, [
            'Trace'
        ]);
    }
    clear() {
        this[LOG_SYMBOL](LogType.clear);
    }
    status(...args) {
        this[LOG_SYMBOL](LogType.status, args);
    }
    group(...args) {
        this[LOG_SYMBOL](LogType.group, args);
    }
    groupCollapsed(...args) {
        this[LOG_SYMBOL](LogType.groupCollapsed, args);
    }
    groupEnd() {
        this[LOG_SYMBOL](LogType.groupEnd);
    }
    profile(label) {
        this[LOG_SYMBOL](LogType.profile, [
            label
        ]);
    }
    profileEnd(label) {
        this[LOG_SYMBOL](LogType.profileEnd, [
            label
        ]);
    }
    time(label) {
        this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
        this[TIMERS_SYMBOL].set(label, process.hrtime());
    }
    timeLog(label) {
        const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
        if (!prev) throw new Error(`No such label '${label}' for RspackLogger.timeLog()`);
        const time = process.hrtime(prev);
        this[LOG_SYMBOL](LogType.time, [
            label,
            ...time
        ]);
    }
    timeEnd(label) {
        const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
        if (!prev) throw new Error(`No such label '${label}' for RspackLogger.timeEnd()`);
        const time = process.hrtime(prev);
        this[TIMERS_SYMBOL].delete(label);
        this[LOG_SYMBOL](LogType.time, [
            label,
            ...time
        ]);
    }
    timeAggregate(label) {
        const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
        if (!prev) throw new Error(`No such label '${label}' for RspackLogger.timeAggregate()`);
        const time = process.hrtime(prev);
        this[TIMERS_SYMBOL].delete(label);
        this[TIMERS_AGGREGATES_SYMBOL] = this[TIMERS_AGGREGATES_SYMBOL] || new Map();
        const current = this[TIMERS_AGGREGATES_SYMBOL].get(label);
        if (void 0 !== current) if (time[1] + current[1] > 1e9) {
            time[0] += current[0] + 1;
            time[1] = time[1] - 1e9 + current[1];
        } else {
            time[0] += current[0];
            time[1] += current[1];
        }
        this[TIMERS_AGGREGATES_SYMBOL].set(label, time);
    }
    timeAggregateEnd(label) {
        if (void 0 === this[TIMERS_AGGREGATES_SYMBOL]) return;
        const time = this[TIMERS_AGGREGATES_SYMBOL].get(label);
        if (void 0 === time) return;
        this[TIMERS_AGGREGATES_SYMBOL].delete(label);
        this[LOG_SYMBOL](LogType.time, [
            label,
            ...time
        ]);
    }
    constructor(log){
        _define_property(this, _LOG_SYMBOL, void 0);
        _define_property(this, _TIMERS_SYMBOL, new Map());
        _define_property(this, _TIMERS_AGGREGATES_SYMBOL, new Map());
        this[LOG_SYMBOL] = log;
    }
}
export { RspackLogger as Logger };
