const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
let __intlayer_config = require("@intlayer/config");

//#region src/push/pullLog.ts
var PullLogger = class {
	statuses = [];
	spinnerTimer = null;
	spinnerIndex = 0;
	renderedLines = 0;
	spinnerFrames = __intlayer_config.spinnerFrames;
	isFinished = false;
	prefix;
	lastRenderedState = "";
	constructor() {
		this.prefix = (0, __intlayer_config.getConfiguration)().log.prefix;
	}
	update(newStatuses) {
		if (this.isFinished) return;
		for (const status of newStatuses) {
			const index = this.statuses.findIndex((s) => s.dictionaryKey === status.dictionaryKey);
			if (index >= 0) this.statuses[index] = status;
			else this.statuses.push(status);
		}
		this.startSpinner();
		this.render();
	}
	finish() {
		this.isFinished = true;
		this.stopSpinner();
		this.render();
	}
	startSpinner() {
		if (this.spinnerTimer || this.isFinished) return;
		this.spinnerTimer = setInterval(() => {
			this.spinnerIndex = (this.spinnerIndex + 1) % this.spinnerFrames.length;
			this.render();
		}, 100);
	}
	stopSpinner() {
		if (!this.spinnerTimer) return;
		clearInterval(this.spinnerTimer);
		this.spinnerTimer = null;
	}
	render() {
		const { total, done, success, errors } = this.computeProgress();
		const frame = this.spinnerFrames[this.spinnerIndex];
		const lines = [];
		const isDone = done === total;
		const progressLabel = `dictionaries: ${done}/${total}`;
		const details = [];
		if (success > 0) details.push(`ok: ${success}`);
		if (errors > 0) details.push((0, __intlayer_config.colorize)(`errors: ${errors}`, __intlayer_config.ANSIColors.RED));
		const suffix = details.length > 0 ? ` (${details.join(", ")})` : "";
		if (isDone) lines.push(`${this.prefix} ${(0, __intlayer_config.colorize)("✔", __intlayer_config.ANSIColors.GREEN)} fetched ${progressLabel}${suffix}`);
		else lines.push(`${this.prefix} ${(0, __intlayer_config.colorize)(frame, __intlayer_config.ANSIColors.BLUE)} fetching ${progressLabel}${suffix}`);
		const currentState = lines.join("\n");
		if (currentState === this.lastRenderedState) return;
		this.lastRenderedState = currentState;
		if (this.renderedLines > 0) process.stdout.write(`\x1b[${this.renderedLines}F`);
		const totalLinesToClear = Math.max(this.renderedLines, lines.length);
		for (let i = 0; i < totalLinesToClear; i++) {
			process.stdout.write("\x1B[2K");
			const line = lines[i];
			if (line !== void 0) process.stdout.write(line);
			process.stdout.write("\n");
		}
		this.renderedLines = lines.length;
	}
	computeProgress() {
		const keys = new Set(this.statuses.map((s) => s.dictionaryKey));
		const doneSet = new Set([
			"fetched",
			"imported",
			"updated",
			"up-to-date",
			"reimported in JSON",
			"new content file",
			"error"
		]);
		const successesSet = new Set([
			"fetched",
			"imported",
			"updated",
			"up-to-date",
			"reimported in JSON",
			"new content file"
		]);
		const done = this.statuses.filter((s) => doneSet.has(s.status)).length;
		const success = this.statuses.filter((s) => successesSet.has(s.status)).length;
		const errors = this.statuses.filter((s) => s.status === "error").length;
		return {
			total: keys.size,
			done,
			success,
			errors
		};
	}
};

//#endregion
exports.PullLogger = PullLogger;
//# sourceMappingURL=pullLog.cjs.map