"use strict";
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);
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const devServer = require("./dev-server-BX5ZYjJS.cjs");
const events = require("./fetch-virtual-server-J6SUa9HS.cjs");
function badMethod(method) {
  const body = `Method not allowed: ${method}`;
  return {
    statusCode: 405,
    statusMessage: "Method Not Allowed",
    headers: {
      "Content-Type": "text/plain; charset=utf-8",
      "Content-Length": String(events.BufferPolyfill.byteLength(body))
    },
    body: events.BufferPolyfill.from(body)
  };
}
class CloudflarePagesDevServer extends devServer.DevServer {
  constructor(vfs, options) {
    super(vfs, {
      ...options,
      root: options.assetsDir
    });
    __publicField(this, "assetsDir");
    this.assetsDir = options.assetsDir;
  }
  startWatching() {
  }
  async handleRequest(method, url, _headers) {
    if (method !== "GET" && method !== "HEAD") {
      return badMethod(method);
    }
    const parsedUrl = new URL(url, `http://localhost:${this.port}`);
    let pathname = parsedUrl.pathname;
    try {
      pathname = decodeURIComponent(pathname);
    } catch {
    }
    const filePath = this.resolveAssetPath(pathname);
    if (filePath) {
      const response = this.serveFile(filePath);
      return method === "HEAD" ? { ...response, body: events.BufferPolyfill.from("") } : response;
    }
    const notFoundPage = events.join(this.assetsDir, "404.html");
    if (this.exists(notFoundPage)) {
      const response = this.serveFile(notFoundPage);
      return {
        ...response,
        statusCode: 404,
        statusMessage: "Not Found",
        body: method === "HEAD" ? events.BufferPolyfill.from("") : response.body
      };
    }
    return this.notFound(pathname);
  }
  resolveAssetPath(requestPath) {
    const normalizedPath = events.normalize(requestPath || "/");
    const resolved = events.normalize(events.join(this.assetsDir, normalizedPath));
    if (!(resolved === this.assetsDir || resolved.startsWith(`${this.assetsDir}/`))) {
      return null;
    }
    if (this.exists(resolved) && !this.isDirectory(resolved)) {
      return resolved;
    }
    const directoryIndex = events.join(resolved, "index.html");
    if (this.exists(directoryIndex)) {
      return directoryIndex;
    }
    if (!events.extname(resolved)) {
      const htmlFile = `${resolved}.html`;
      if (this.exists(htmlFile)) {
        return htmlFile;
      }
    }
    return null;
  }
}
exports.CloudflarePagesDevServer = CloudflarePagesDevServer;
//# sourceMappingURL=cloudflare-pages-dev-server-y3reqN78.cjs.map
