import {
  createCliProvider
} from "../chunk-5ZFOKZGR.js";
import "../chunk-2ESYSVXG.js";

// src/providers/apple-container.ts
var DEFAULT_IMAGE = process.env.APPLE_CONTAINER_IMAGE ?? "node:22";
var appleProvider = Object.assign(createCliProvider({
  name: "apple-container",
  binary: "container",
  createSteps: (name) => [
    ["create", "--name", name, DEFAULT_IMAGE, "sleep", "infinity"],
    ["start", name]
  ],
  deleteArgs: (name) => ["rm", "-f", name],
  execArgs: (name, argv) => ["exec", "-i", name, ...argv],
  // Apple uses `container ls` not `docker ps`
  listArgs: (prefix) => [
    "ls",
    "-a",
    "--filter",
    `name=${prefix}`,
    "--format",
    "{{.Names}}"
  ],
  parseList: (stdout) => stdout.trim().split("\n").filter(Boolean),
  checkAvailability: async () => {
    if (process.platform !== "darwin") {
      return { available: false, message: "Apple Containers requires macOS" };
    }
    try {
      const { spawn } = await import("child_process");
      await new Promise((resolve, reject) => {
        const proc = spawn("container", ["--version"], {
          stdio: ["pipe", "pipe", "pipe"]
        });
        proc.on(
          "close",
          (code) => code === 0 ? resolve() : reject(new Error(`exit ${code}`))
        );
        proc.on("error", reject);
        const t = setTimeout(() => {
          proc.kill("SIGKILL");
          reject(new Error("timeout"));
        }, 3e3);
        proc.on("close", () => clearTimeout(t));
      });
      const { spawn: spawn2 } = await import("child_process");
      const daemonOk = await new Promise((resolve) => {
        const proc = spawn2("container", ["system", "status"], {
          stdio: ["pipe", "pipe", "pipe"]
        });
        let out = "";
        proc.stdout?.on("data", (buf) => {
          out += buf.toString();
        });
        proc.on("close", (code) => {
          resolve(code === 0 && out.includes("running"));
        });
        proc.on("error", () => resolve(false));
        setTimeout(() => {
          proc.kill("SIGKILL");
          resolve(false);
        }, 3e3);
      });
      if (!daemonOk) {
        return {
          available: false,
          message: "Apple Containers service not running. Run: brew services start container"
        };
      }
      return { available: true };
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      if (msg.includes("ENOENT")) {
        return {
          available: false,
          message: "Apple Containers not installed. Run: brew install container (requires macOS 26+)"
        };
      }
      return {
        available: false,
        message: `Apple Containers not accessible: ${msg}`
      };
    }
  }
}), { supportsWarmPool: true });
export {
  appleProvider
};
