import { eventHandler, createError } from 'h3';
import { withLeadingSlash, withoutTrailingSlash, parseURL } from 'ufo';
import { promises } from 'fs';
import { resolve, dirname } from 'pathe';
import { fileURLToPath } from 'url';

const assets = {
  "/_nuxt/_...slug_-2edc6759.mjs": {
    "type": "application/javascript",
    "etag": "\"5f2-2aZiUL52kJNGeTcm2QnihmFTTp4\"",
    "mtime": "2022-05-07T15:56:31.770Z",
    "path": "../public/_nuxt/_...slug_-2edc6759.mjs"
  },
  "/_nuxt/components-4855da8b.mjs": {
    "type": "application/javascript",
    "etag": "\"20c-HYeoOAq9qo59oT9owXOI7wOHosY\"",
    "mtime": "2022-05-07T15:56:31.770Z",
    "path": "../public/_nuxt/components-4855da8b.mjs"
  },
  "/_nuxt/docs-fdc074cc.mjs": {
    "type": "application/javascript",
    "etag": "\"29b-OO6Jt5GJUnB+yHUe/BECu1tth5M\"",
    "mtime": "2022-05-07T15:56:31.766Z",
    "path": "../public/_nuxt/docs-fdc074cc.mjs"
  },
  "/_nuxt/entry-2ec0c5e7.mjs": {
    "type": "application/javascript",
    "etag": "\"4e9d7-iNu2Kwmsd1HpEQ0R06wdfwePMW0\"",
    "mtime": "2022-05-07T15:56:31.766Z",
    "path": "../public/_nuxt/entry-2ec0c5e7.mjs"
  },
  "/_nuxt/entry.7cd7ed7d.css": {
    "type": "text/css; charset=utf-8",
    "etag": "\"10992-smgS6XBmbw1KZo4XWK6D+XkkY/s\"",
    "mtime": "2022-05-07T15:56:31.766Z",
    "path": "../public/_nuxt/entry.7cd7ed7d.css"
  },
  "/_nuxt/github-c34dd74a.mjs": {
    "type": "application/javascript",
    "etag": "\"168-8wYZIg2zRyHsZvIauH/KdDDm6Iw\"",
    "mtime": "2022-05-07T15:56:31.766Z",
    "path": "../public/_nuxt/github-c34dd74a.mjs"
  },
  "/_nuxt/manifest.json": {
    "type": "application/json",
    "etag": "\"c2b-/8DU4GdBplvWIXcCQeD8u06hF9s\"",
    "mtime": "2022-05-07T15:56:31.766Z",
    "path": "../public/_nuxt/manifest.json"
  },
  "/_nuxt/navigation-bba0934d.mjs": {
    "type": "application/javascript",
    "etag": "\"86-LKuq5ZnxJt+p/epthGLrpKjDNI0\"",
    "mtime": "2022-05-07T15:56:31.766Z",
    "path": "../public/_nuxt/navigation-bba0934d.mjs"
  },
  "/_nuxt/page-83cd1208.mjs": {
    "type": "application/javascript",
    "etag": "\"285-seL71oZF73HcJcW97aCkWX7I9GA\"",
    "mtime": "2022-05-07T15:56:31.766Z",
    "path": "../public/_nuxt/page-83cd1208.mjs"
  },
  "/_nuxt/page-981ba431.mjs": {
    "type": "application/javascript",
    "etag": "\"c3-rRzKPfAehHAIBBtadOeNys6mCfw\"",
    "mtime": "2022-05-07T15:56:31.762Z",
    "path": "../public/_nuxt/page-981ba431.mjs"
  },
  "/_nuxt/web-socket-7ca38556.mjs": {
    "type": "application/javascript",
    "etag": "\"325-YtGkmJKUYsLx5YYobVWIe4Ev28M\"",
    "mtime": "2022-05-07T15:56:31.762Z",
    "path": "../public/_nuxt/web-socket-7ca38556.mjs"
  }
};

const mainDir = dirname(fileURLToPath(globalThis.entryURL));
function readAsset (id) {
  return promises.readFile(resolve(mainDir, assets[id].path)).catch(() => {})
}

const publicAssetBases = ["/_nuxt"];

function isPublicAssetURL(id = '') {
  if (assets[id]) {
    return
  }
  for (const base of publicAssetBases) {
    if (id.startsWith(base)) { return true }
  }
  return false
}

function getAsset (id) {
  return assets[id]
}

const METHODS = ["HEAD", "GET"];
const _static = eventHandler(async (event) => {
  if (event.req.method && !METHODS.includes(event.req.method)) {
    return;
  }
  let id = decodeURIComponent(withLeadingSlash(withoutTrailingSlash(parseURL(event.req.url).pathname)));
  let asset;
  for (const _id of [id, id + "/index.html"]) {
    const _asset = getAsset(_id);
    if (_asset) {
      asset = _asset;
      id = _id;
      break;
    }
  }
  if (!asset) {
    if (isPublicAssetURL(id)) {
      throw createError({
        statusMessage: "Cannot find static asset " + id,
        statusCode: 404
      });
    }
    return;
  }
  const ifNotMatch = event.req.headers["if-none-match"] === asset.etag;
  if (ifNotMatch) {
    event.res.statusCode = 304;
    event.res.end("Not Modified (etag)");
    return;
  }
  const ifModifiedSinceH = event.req.headers["if-modified-since"];
  if (ifModifiedSinceH && asset.mtime) {
    if (new Date(ifModifiedSinceH) >= new Date(asset.mtime)) {
      event.res.statusCode = 304;
      event.res.end("Not Modified (mtime)");
      return;
    }
  }
  if (asset.type) {
    event.res.setHeader("Content-Type", asset.type);
  }
  if (asset.etag) {
    event.res.setHeader("ETag", asset.etag);
  }
  if (asset.mtime) {
    event.res.setHeader("Last-Modified", asset.mtime);
  }
  const contents = await readAsset(id);
  event.res.end(contents);
});

export { _static as default };
//# sourceMappingURL=static.mjs.map
