var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { aF as EventEmitter, C as BufferPolyfill } from "./fetch-virtual-server-BZi6n2Qk.js";
const MIME_TYPES = {
  html: "text/html; charset=utf-8",
  htm: "text/html; charset=utf-8",
  css: "text/css; charset=utf-8",
  js: "application/javascript; charset=utf-8",
  mjs: "application/javascript; charset=utf-8",
  cjs: "application/javascript; charset=utf-8",
  jsx: "application/javascript; charset=utf-8",
  ts: "application/javascript; charset=utf-8",
  tsx: "application/javascript; charset=utf-8",
  json: "application/json; charset=utf-8",
  png: "image/png",
  jpg: "image/jpeg",
  jpeg: "image/jpeg",
  gif: "image/gif",
  svg: "image/svg+xml",
  ico: "image/x-icon",
  webp: "image/webp",
  woff: "font/woff",
  woff2: "font/woff2",
  ttf: "font/ttf",
  otf: "font/otf",
  eot: "application/vnd.ms-fontobject",
  mp3: "audio/mpeg",
  mp4: "video/mp4",
  webm: "video/webm",
  ogg: "audio/ogg",
  wav: "audio/wav",
  pdf: "application/pdf",
  xml: "application/xml",
  txt: "text/plain; charset=utf-8",
  md: "text/markdown; charset=utf-8",
  wasm: "application/wasm",
  map: "application/json"
};
class DevServer extends EventEmitter {
  constructor(vfs, options) {
    super();
    __publicField(this, "vfs");
    __publicField(this, "port");
    __publicField(this, "root");
    __publicField(this, "running", false);
    this.vfs = vfs;
    this.port = options.port;
    this.root = options.root || "/";
  }
  /**
   * Stop the server and cleanup
   */
  stop() {
    this.running = false;
    this.emit("close");
  }
  /**
   * Start the server
   */
  start() {
    this.running = true;
    this.startWatching();
    this.emit("listening", this.port);
  }
  /**
   * Check if server is running
   */
  isRunning() {
    return this.running;
  }
  /**
   * Get the server's port
   */
  getPort() {
    return this.port;
  }
  /**
   * Serve a static file from the virtual filesystem
   */
  serveFile(filePath) {
    try {
      const normalizedPath = this.resolvePath(filePath);
      const content = this.vfs.readFileSync(normalizedPath);
      const buffer = typeof content === "string" ? BufferPolyfill.from(content) : BufferPolyfill.from(content);
      return {
        statusCode: 200,
        statusMessage: "OK",
        headers: {
          "Content-Type": this.getMimeType(filePath),
          "Content-Length": String(buffer.length),
          "Cache-Control": "no-cache"
        },
        body: buffer
      };
    } catch (error) {
      if (error.code === "ENOENT") {
        return this.notFound(filePath);
      }
      return this.serverError(error);
    }
  }
  /**
   * Resolve a URL path to a filesystem path
   */
  resolvePath(urlPath) {
    let path = urlPath.split("?")[0].split("#")[0];
    if (!path.startsWith("/")) {
      path = "/" + path;
    }
    if (this.root !== "/" && !path.startsWith(this.root + "/")) {
      path = this.root + path;
    }
    return path;
  }
  /**
   * Create a 404 Not Found response
   */
  notFound(path) {
    const body = `Not found: ${path}`;
    return {
      statusCode: 404,
      statusMessage: "Not Found",
      headers: {
        "Content-Type": "text/plain; charset=utf-8",
        "Content-Length": String(BufferPolyfill.byteLength(body))
      },
      body: BufferPolyfill.from(body)
    };
  }
  /**
   * Create a 500 Server Error response
   */
  serverError(error) {
    const message = error instanceof Error ? error.message : "Internal Server Error";
    const body = `Server Error: ${message}`;
    return {
      statusCode: 500,
      statusMessage: "Internal Server Error",
      headers: {
        "Content-Type": "text/plain; charset=utf-8",
        "Content-Length": String(BufferPolyfill.byteLength(body))
      },
      body: BufferPolyfill.from(body)
    };
  }
  /**
   * Create a redirect response
   */
  redirect(location, status = 302) {
    return {
      statusCode: status,
      statusMessage: status === 301 ? "Moved Permanently" : "Found",
      headers: {
        Location: location,
        "Content-Type": "text/plain; charset=utf-8",
        "Content-Length": "0"
      },
      body: BufferPolyfill.from("")
    };
  }
  /**
   * Get MIME type for a file path
   */
  getMimeType(path) {
    var _a;
    const ext = ((_a = path.split(".").pop()) == null ? void 0 : _a.toLowerCase()) || "";
    return MIME_TYPES[ext] || "application/octet-stream";
  }
  /**
   * Check if a path exists in the virtual filesystem
   */
  exists(path) {
    try {
      this.vfs.statSync(path);
      return true;
    } catch {
      return false;
    }
  }
  /**
   * Check if a path is a directory
   */
  isDirectory(path) {
    try {
      return this.vfs.statSync(path).isDirectory();
    } catch {
      return false;
    }
  }
  /**
   * Emit an HMR update event
   */
  emitHMRUpdate(update) {
    this.emit("hmr-update", {
      ...update,
      timestamp: update.timestamp || Date.now()
    });
  }
}
export {
  DevServer as D
};
//# sourceMappingURL=dev-server-nWWQHf5q.js.map
