/**
 * Service contract and helpers for running child processes through Effect.
 *
 * This module defines the {@link ChildProcessSpawner} service used by child
 * process commands to start operating-system processes. A spawner turns a
 * command description into a {@link ChildProcessHandle}, which exposes scoped
 * lifecycle operations: write to stdin, stream stdout and stderr, wait for the
 * exit code, kill the process, inspect whether it is still running, and
 * temporarily unreference it from the parent process.
 *
 * Use this module when implementing a platform-specific process backend or
 * when code needs direct access to the process service. Most applications build
 * commands with the `ChildProcess` module; this service is the lower-level
 * execution boundary and also provides convenience methods for collecting exit
 * codes, strings, and output lines. The {@link make} constructor derives those
 * helpers from one primitive `spawn` implementation, so adapters only need to
 * supply process creation.
 *
 * @since 4.0.0
 */
import * as Brand from "../../Brand.js";
import * as Context from "../../Context.js";
import * as Effect from "../../Effect.js";
import * as Inspectable from "../../Inspectable.js";
import * as Stream from "../../Stream.js";
/**
 * Constructs branded child process `ExitCode` values.
 *
 * @category constructors
 * @since 4.0.0
 */
export const ExitCode = /*#__PURE__*/Brand.nominal();
/**
 * Constructs branded child process `ProcessId` values.
 *
 * @category constructors
 * @since 4.0.0
 */
export const ProcessId = /*#__PURE__*/Brand.nominal();
const HandleTypeId = "~effect/ChildProcessSpawner/ChildProcessHandle";
const HandleProto = {
  [HandleTypeId]: HandleTypeId,
  ...Inspectable.BaseProto,
  toJSON() {
    return {
      _id: "ChildProcessHandle",
      pid: this.pid
    };
  }
};
/**
 * Constructs a new `ChildProcessHandle`.
 *
 * @category constructors
 * @since 4.0.0
 */
export const makeHandle = params => Object.assign(Object.create(HandleProto), params);
/**
 * Creates a `ChildProcessSpawner` service from a `spawn` function, deriving
 * helpers for exit codes and output collection from that implementation.
 *
 * @category models
 * @since 4.0.0
 */
export const make = spawn => {
  const streamString = (command, options) => spawn(command).pipe(Effect.map(handle => Stream.decodeText(options?.includeStderr === true ? handle.all : handle.stdout)), Stream.unwrap);
  const streamLines = (command, options) => Stream.splitLines(streamString(command, options));
  return ChildProcessSpawner.of({
    spawn,
    exitCode: command => Effect.scoped(Effect.flatMap(spawn(command), handle => handle.exitCode)),
    streamString,
    streamLines,
    lines: (command, options) => Stream.runCollect(streamLines(command, options)),
    string: (command, options) => Stream.mkString(streamString(command, options))
  });
};
/**
 * Service tag for child process spawning.
 *
 * @category services
 * @since 4.0.0
 */
export class ChildProcessSpawner extends /*#__PURE__*/Context.Service()("effect/process/ChildProcessSpawner") {}
//# sourceMappingURL=ChildProcessSpawner.js.map