const require_promises = require('./promises.cjs');

//#region src/io.ts
/**
* Adapter between input stream and vm. It moves forward when [awaitNextProgress] is invoked.
*/
var InputPump = class {
	currentRead;
	closed;
	constructor(coreVm, inputReader, errorCallback) {
		this.coreVm = coreVm;
		this.inputReader = inputReader;
		this.errorCallback = errorCallback;
		this.closed = false;
	}
	awaitNextProgress() {
		if (this.currentRead === void 0) this.currentRead = this.readNext().finally(() => {
			this.currentRead = void 0;
		});
		return new Promise((resolve) => this.currentRead?.finally(resolve));
	}
	async readNext() {
		if (this.closed) return require_promises.pendingPromise();
		let nextValue;
		try {
			nextValue = await this.inputReader.next();
		} catch (e) {
			this.errorCallback(e);
			return require_promises.pendingPromise();
		}
		if (nextValue.done) {
			this.closed = true;
			this.coreVm.notify_input_closed();
		} else if (nextValue.value !== void 0) this.coreVm.notify_input(nextValue.value);
	}
};
/**
* Adapter between output stream and vm. It moves forward when [awaitNextProgress] is invoked.
*/
var OutputPump = class {
	constructor(coreVm, outputWriter) {
		this.coreVm = coreVm;
		this.outputWriter = outputWriter;
	}
	async awaitNextProgress() {
		const nextOutput = this.coreVm.take_output();
		if (nextOutput instanceof Uint8Array) await this.outputWriter.write(nextOutput);
	}
};

//#endregion
exports.InputPump = InputPump;
exports.OutputPump = OutputPump;