#!/usr/bin/env node

import { c as HostProcessPlatform, n as PtySpawnError, t as PtyAdapter } from "./PtyAdapter-DLKyoorI.mjs";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
//#region src/terminal/BunPtyAdapter.ts
var BunPtyUnsupportedPlatformError = class extends Schema.TaggedErrorClass()("BunPtyUnsupportedPlatformError", { platform: Schema.Literal("win32") }) {
	get message() {
		return `Bun PTY terminal support is unavailable on ${this.platform}. Please use Node.js (e.g. by running \`npx p4\`) instead.`;
	}
};
var BunPtyOperationUnavailableError = class extends Schema.TaggedErrorClass()("BunPtyOperationUnavailableError", {
	operation: Schema.Literals(["write", "resize"]),
	pid: Schema.Number
}) {
	get message() {
		return `Bun PTY ${this.operation} is unavailable for process ${this.pid}.`;
	}
};
var BunPtyProcess = class {
	dataListeners = /* @__PURE__ */ new Set();
	exitListeners = /* @__PURE__ */ new Set();
	decoder = new TextDecoder();
	process;
	didExit = false;
	constructor(process) {
		this.process = process;
		this.process.exited.then((exitCode) => {
			this.emitExit({
				exitCode: Number.isInteger(exitCode) ? exitCode : 0,
				signal: typeof this.process.signalCode === "number" ? this.process.signalCode : null
			});
		}).catch(() => {
			this.emitExit({
				exitCode: 1,
				signal: null
			});
		});
	}
	get pid() {
		return this.process.pid;
	}
	write(data) {
		if (!this.process.terminal) throw new BunPtyOperationUnavailableError({
			operation: "write",
			pid: this.pid
		});
		this.process.terminal.write(data);
	}
	resize(cols, rows) {
		if (!this.process.terminal?.resize) throw new BunPtyOperationUnavailableError({
			operation: "resize",
			pid: this.pid
		});
		this.process.terminal.resize(cols, rows);
	}
	kill(signal) {
		if (!signal) {
			this.process.kill();
			return;
		}
		this.process.kill(signal);
	}
	onData(callback) {
		this.dataListeners.add(callback);
		return () => {
			this.dataListeners.delete(callback);
		};
	}
	onExit(callback) {
		this.exitListeners.add(callback);
		return () => {
			this.exitListeners.delete(callback);
		};
	}
	emitData(data) {
		if (this.didExit) return;
		const text = this.decoder.decode(data, { stream: true });
		if (text.length === 0) return;
		for (const listener of this.dataListeners) listener(text);
	}
	emitExit(event) {
		if (this.didExit) return;
		this.didExit = true;
		const remainder = this.decoder.decode();
		if (remainder.length > 0) for (const listener of this.dataListeners) listener(remainder);
		for (const listener of this.exitListeners) listener(event);
	}
};
const make = Effect.fn("BunPtyAdapter.make")(function* () {
	const platform = yield* HostProcessPlatform;
	if (platform === "win32") return yield* Effect.die(new BunPtyUnsupportedPlatformError({ platform }));
	return PtyAdapter.of({ spawn: (input) => Effect.try({
		try: () => {
			let processHandle = null;
			const command = [input.shell, ...input.args ?? []];
			processHandle = new BunPtyProcess(Bun.spawn(command, {
				cwd: input.cwd,
				env: input.env,
				terminal: {
					cols: input.cols,
					rows: input.rows,
					data: (_terminal, data) => {
						processHandle?.emitData(data);
					}
				}
			}));
			return processHandle;
		},
		catch: (cause) => new PtySpawnError({
			adapter: "bun",
			shell: input.shell,
			cause
		})
	}) });
});
const layer = Layer.effect(PtyAdapter, make());
//#endregion
export { BunPtyOperationUnavailableError, BunPtyUnsupportedPlatformError, layer, make };

//# sourceMappingURL=BunPtyAdapter-CLDq7yr1.mjs.map