const require_logger = require('../../../config/dist/esm/logger.cjs');
const require_getConfiguration = require('../../../config/dist/esm/configFile/getConfiguration.cjs');

//#region ../cli/dist/esm/pushLog.mjs
var PushLogger = class {
	statuses = [];
	spinnerTimer = null;
	spinnerIndex = 0;
	renderedLines = 0;
	spinnerFrames = require_logger.spinnerFrames;
	isFinished = false;
	prefix;
	lastRenderedState = "";
	constructor() {
		this.prefix = require_getConfiguration.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, pushed, modified, errors } = this.computeProgress();
		const frame = this.spinnerFrames[this.spinnerIndex];
		const lines = [];
		const isDone = done === total;
		const progressLabel = `dictionaries: ${done}/${total}`;
		const details = [];
		if (pushed > 0) details.push(`new: ${pushed}`);
		if (modified > 0) details.push(`modified: ${modified}`);
		if (errors > 0) details.push(require_logger.colorize(`errors: ${errors}`, require_logger.ANSIColors.RED));
		const suffix = details.length > 0 ? ` (${details.join(", ")})` : "";
		if (isDone) lines.push(`${this.prefix} ${require_logger.colorize("✔", require_logger.ANSIColors.GREEN)} pushed ${progressLabel}${suffix}`);
		else lines.push(`${this.prefix} ${require_logger.colorize(frame, require_logger.ANSIColors.BLUE)} pushing ${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 pushed = this.statuses.filter((s) => s.status === "pushed").length;
		const modified = this.statuses.filter((s) => s.status === "modified").length;
		const errors = this.statuses.filter((s) => s.status === "error").length;
		const done = pushed + modified + errors;
		return {
			total: keys.size,
			done,
			pushed,
			modified,
			errors
		};
	}
};

//#endregion
exports.PushLogger = PushLogger;
//# sourceMappingURL=pushLog.cjs.map