import {
  PtyAdapter
} from "./index-gzh4v9tz.js";
import"./index-y24mzzdg.js";
import {
  exports_Effect,
  exports_FileSystem,
  exports_Layer,
  exports_Path
} from "./index-w9j14fjm.js";
import {
  __require
} from "./index-sdksp5px.js";

// src/terminal/Layers/NodePTY.ts
import { createRequire } from "node:module";
var didEnsureSpawnHelperExecutable = false;
var resolveNodePtySpawnHelperPath = exports_Effect.gen(function* () {
  const requireForNodePty = createRequire(import.meta.url);
  const path = yield* exports_Path.Path;
  const fs = yield* exports_FileSystem.FileSystem;
  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", `${process.platform}-${process.arch}`, "spawn-helper")
  ];
  for (const candidate of candidates) {
    if (yield* fs.exists(candidate)) {
      return candidate;
    }
  }
  return null;
}).pipe(exports_Effect.orElseSucceed(() => null));
var ensureNodePtySpawnHelperExecutable = exports_Effect.fn(function* (explicitPath) {
  const fs = yield* exports_FileSystem.FileSystem;
  if (process.platform === "win32")
    return;
  if (!explicitPath && didEnsureSpawnHelperExecutable)
    return;
  const helperPath = explicitPath ?? (yield* resolveNodePtySpawnHelperPath);
  if (!helperPath)
    return;
  if (!explicitPath) {
    didEnsureSpawnHelperExecutable = true;
  }
  if (!(yield* fs.exists(helperPath))) {
    return;
  }
  yield* fs.chmod(helperPath, 493).pipe(exports_Effect.orElseSucceed(() => {
    return;
  }));
});

class NodePtyProcess {
  process;
  constructor(process2) {
    this.process = process2;
  }
  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();
    };
  }
}
var layer = exports_Layer.effect(PtyAdapter, exports_Effect.gen(function* () {
  const fs = yield* exports_FileSystem.FileSystem;
  const path = yield* exports_Path.Path;
  const nodePty = yield* exports_Effect.promise(() => import("node-pty"));
  const ensureNodePtySpawnHelperExecutableCached = yield* exports_Effect.cached(ensureNodePtySpawnHelperExecutable().pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path), exports_Effect.orElseSucceed(() => {
    return;
  })));
  return {
    spawn: exports_Effect.fn(function* (input) {
      yield* ensureNodePtySpawnHelperExecutableCached;
      const ptyProcess = nodePty.spawn(input.shell, input.args ?? [], {
        cwd: input.cwd,
        cols: input.cols,
        rows: input.rows,
        env: input.env,
        name: globalThis.process.platform === "win32" ? "xterm-color" : "xterm-256color"
      });
      return new NodePtyProcess(ptyProcess);
    })
  };
}));
export {
  layer,
  ensureNodePtySpawnHelperExecutable
};
