"use strict";
/* eslint no-console: 0 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLogger = void 0;
const chalk_1 = __importDefault(require("chalk"));
const readline_1 = __importDefault(require("readline"));
const LogLevels = {
    silent: 0,
    error: 1,
    warn: 2,
    info: 3
};
let lastType;
let lastMsg;
let sameCount = 0;
function clearScreen() {
    const blank = '\n'.repeat(process.stdout.rows - 2);
    console.log(blank);
    readline_1.default.cursorTo(process.stdout, 0, 0);
    readline_1.default.clearScreenDown(process.stdout);
}
function createLogger(level = 'info') {
    const thresh = LogLevels[level];
    function output(type, msg, options = {}) {
        if (thresh >= LogLevels[type]) {
            const method = type === 'info' ? 'log' : type;
            const format = () => {
                if (options.timestamp) {
                    const tag = type === 'info'
                        ? chalk_1.default.cyan.bold(`[vite]`)
                        : type === 'warn'
                            ? chalk_1.default.yellow.bold(`[vite]`)
                            : chalk_1.default.red.bold(`[vite]`);
                    return `${chalk_1.default.dim(new Date().toLocaleTimeString())} ${tag} ${msg}`;
                }
                else {
                    return msg;
                }
            };
            if (type === lastType && msg === lastMsg) {
                sameCount++;
                clearScreen();
                console[method](format(), chalk_1.default.yellow(`(x${sameCount + 1})`));
            }
            else {
                sameCount = 0;
                lastMsg = msg;
                lastType = type;
                if (options.clear) {
                    clearScreen();
                }
                console[method](format());
            }
        }
    }
    return {
        info(msg, opts) {
            output('info', msg, opts);
        },
        warn(msg, opts) {
            output('warn', msg, opts);
        },
        error(msg, opts) {
            output('error', msg, opts);
        },
        clearScreen(type) {
            if (thresh >= LogLevels[type]) {
                clearScreen();
            }
        }
    };
}
exports.createLogger = createLogger;
//# sourceMappingURL=logger.js.map