import {
  startExec
} from "../chunk-Z3RHLDHR.js";
import {
  createSprite,
  deleteSprite,
  getSprite,
  httpExec,
  listSprites
} from "../chunk-5UXN6QXJ.js";
import "../chunk-CTVBTCTF.js";
import {
  getConfig
} from "../chunk-2MAQ6JFL.js";
import "../chunk-HFDLUBWN.js";
import "../chunk-RII4M3J2.js";
import "../chunk-DDJOPRB7.js";
import "../chunk-TLEKKGH7.js";
import "../chunk-KBM3MISV.js";
import "../chunk-EZYKRG4W.js";
import "../chunk-2ESYSVXG.js";

// src/providers/sprites.ts
var READY_POLL_INTERVAL_MS = 500;
var READY_POLL_TIMEOUT_MS = 3e4;
var spritesProvider = {
  name: "sprites",
  stripControlChars: true,
  // sprites.dev HTTP exec multiplexes stdout/stderr with control bytes
  supportsWarmPool: true,
  async checkAvailability(secrets) {
    const cfg = getConfig();
    if (!(secrets?.SPRITE_TOKEN ?? cfg.spriteToken)) {
      return { available: false, message: "SPRITE_TOKEN required \u2014 add to vault or .env" };
    }
    return { available: true };
  },
  async create({ name, secrets }) {
    const token = secrets?.SPRITE_TOKEN;
    const sprite = await createSprite({ name, tokenOverride: token });
    if (sprite.status === "warm" || sprite.status === "running") return;
    const deadline = Date.now() + READY_POLL_TIMEOUT_MS;
    while (Date.now() < deadline) {
      await new Promise((r) => setTimeout(r, READY_POLL_INTERVAL_MS));
      const s = await getSprite(name, token);
      if (s?.status === "warm" || s?.status === "running") return;
    }
    throw new Error(`sprites.dev container ${name} not ready after 30s`);
  },
  async delete(name, secrets) {
    await deleteSprite(name, secrets?.SPRITE_TOKEN);
  },
  async list(opts) {
    const res = await listSprites({
      prefix: opts?.prefix,
      max_results: 100
    });
    return res.sprites.map((s) => ({ name: s.name }));
  },
  async exec(name, argv, opts) {
    return httpExec(name, argv, {
      stdin: opts?.stdin,
      timeoutMs: opts?.timeoutMs,
      tokenOverride: opts?.secrets?.SPRITE_TOKEN
    });
  },
  startExec(name, opts) {
    return startExec(name, opts);
  }
};
export {
  spritesProvider
};
