#!/usr/bin/env node

import { c as HostProcessPlatform, n as PtySpawnError, r as HostProcessArchitecture, t as PtyAdapter } from "./PtyAdapter-DLKyoorI.mjs";
import * as NodeModule from "node:module";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
//#region src/terminal/NodePtyAdapter.ts
var NodePtyModuleLoadError = class extends Schema.TaggedErrorClass()("NodePtyModuleLoadError", {
	platform: Schema.String,
	architecture: Schema.String,
	cause: Schema.Defect()
}) {
	get message() {
		return `Failed to load node-pty for ${this.platform}-${this.architecture}.`;
	}
};
let didEnsureSpawnHelperExecutable = false;
const resolveNodePtySpawnHelperPath = Effect.gen(function* () {
	const requireForNodePty = NodeModule.createRequire(import.meta.url);
	const path = yield* Path.Path;
	const fs = yield* FileSystem.FileSystem;
	const platform = yield* HostProcessPlatform;
	const architecture = yield* HostProcessArchitecture;
	const packageJsonPath = requireForNodePty.resolve("node-pty/package.json");
	const packageDir = path.dirname(packageJsonPath);
	const candidates = [
		path.join(packageDir, "build", "Release", "spawn-helper"),
		path.join(packageDir, "build", "Debug", "spawn-helper"),
		path.join(packageDir, "prebuilds", `${platform}-${architecture}`, "spawn-helper")
	];
	for (const candidate of candidates) if (yield* fs.exists(candidate)) return candidate;
	return null;
}).pipe(Effect.orElseSucceed(() => null));
const ensureNodePtySpawnHelperExecutable = Effect.fn(function* () {
	const fs = yield* FileSystem.FileSystem;
	if ((yield* HostProcessPlatform) === "win32") return;
	if (didEnsureSpawnHelperExecutable) return;
	const helperPath = yield* resolveNodePtySpawnHelperPath;
	if (!helperPath) return;
	didEnsureSpawnHelperExecutable = true;
	if (!(yield* fs.exists(helperPath))) return;
	yield* fs.chmod(helperPath, 493).pipe(Effect.orElseSucceed(() => void 0));
});
var NodePtyProcess = class {
	process;
	constructor(process) {
		this.process = process;
	}
	get pid() {
		return this.process.pid;
	}
	write(data) {
		this.process.write(data);
	}
	resize(cols, rows) {
		this.process.resize(cols, rows);
	}
	kill(signal) {
		this.process.kill(signal);
	}
	onData(callback) {
		const disposable = this.process.onData(callback);
		return () => {
			disposable.dispose();
		};
	}
	onExit(callback) {
		const disposable = this.process.onExit((event) => {
			callback({
				exitCode: event.exitCode,
				signal: event.signal ?? null
			});
		});
		return () => {
			disposable.dispose();
		};
	}
};
const make = Effect.fn("NodePtyAdapter.make")(function* (loadNodePtyModule = () => import("node-pty")) {
	const fs = yield* FileSystem.FileSystem;
	const path = yield* Path.Path;
	const platform = yield* HostProcessPlatform;
	const architecture = yield* HostProcessArchitecture;
	const nodePty = yield* Effect.tryPromise({
		try: loadNodePtyModule,
		catch: (cause) => new NodePtyModuleLoadError({
			platform,
			architecture,
			cause
		})
	}).pipe(Effect.orDie);
	const ensureNodePtySpawnHelperExecutableCached = yield* Effect.cached(ensureNodePtySpawnHelperExecutable().pipe(Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path), Effect.provideService(HostProcessPlatform, platform), Effect.provideService(HostProcessArchitecture, architecture), Effect.orElseSucceed(() => void 0)));
	return PtyAdapter.of({ spawn: Effect.fn("NodePtyAdapter.spawn")(function* (input) {
		yield* ensureNodePtySpawnHelperExecutableCached;
		return new NodePtyProcess(yield* Effect.try({
			try: () => nodePty.spawn(input.shell, input.args ?? [], {
				cwd: input.cwd,
				cols: input.cols,
				rows: input.rows,
				env: input.env,
				name: platform === "win32" ? "xterm-color" : "xterm-256color"
			}),
			catch: (cause) => new PtySpawnError({
				adapter: "node-pty",
				shell: input.shell,
				cause
			})
		}));
	}) });
});
const layer = Layer.effect(PtyAdapter, make());
//#endregion
export { NodePtyModuleLoadError, layer, make };

//# sourceMappingURL=NodePtyAdapter-BQAnH2v0.mjs.map