import {
  require_browser
} from "./chunk-chunk-LLVGV6IH.mjs";
import {
  wait
} from "./chunk-chunk-YPDNFE33.mjs";
import {
  __name,
  __toESM,
  init_define_process
} from "./chunk-chunk-5RXWZEI4.mjs";

// src/cf-esbuild.mjs
init_define_process();

// ../js.spike.land/src/index.mjs
init_define_process();
import wasmModule from "./esbuild-INHDOF55.wasm";

// src/transpile.ts
init_define_process();
var import_esbuild_wasm = __toESM(require_browser(), 1);

// src/esbuildWASM.ts
init_define_process();
import wasmFile from "./esbuild-INHDOF55.wasm";

// src/transpile.ts
var mod = self.mod = self.mod || {
  init: false,
  initialize: (wasmModule2) => self.mod.init || (0, import_esbuild_wasm.initialize)({
    wasmModule: wasmModule2,
    worker: false
  }).then(() => self.mod.init = true)
};
var transpile = /* @__PURE__ */ __name(async (code, origin, wasmModule2) => {
  if (wasmModule2) {
    const initFinished = mod.initialize(wasmModule2);
    if (initFinished !== true)
      await initFinished;
  } else {
    mod.init = mod.init || (0, import_esbuild_wasm.initialize)({
      wasmURL: `/${wasmFile}`,
      worker: false
    }).then(() => {
      return mod.init = true;
    });
    const offLoadToServer = /* @__PURE__ */ __name((code2) => fetch(`https://js.spike.land?v=${swVersion}`, {
      method: "POST",
      body: code2,
      headers: { TR_ORIGIN: origin }
    }).then((resp) => resp.text()), "offLoadToServer");
    if (mod.init !== true)
      await wait(1e3);
    if (mod.init !== true)
      return offLoadToServer(code);
  }
  return (0, import_esbuild_wasm.transform)(code, {
    loader: "tsx",
    format: "esm",
    treeShaking: true,
    platform: "browser",
    minify: false,
    charset: "utf8",
    //   globalName: md5(code),
    keepNames: true,
    tsconfigRaw: {
      compilerOptions: {
        jsx: "react-jsx",
        useDefineForClassFields: false,
        jsxFragmentFactory: "Fragment",
        jsxImportSource: "@emotion/react"
      }
    },
    target: "chrome88"
  }).then((x) => x.code);
}, "transpile");
Object.assign(self, { transpile });

// ../js.spike.land/src/index.mjs
import ASSET_HASH from "./dist.shasum";
Object.assign(globalThis, {
  performance: {
    now: () => Date.now()
  }
});
var initAndTransform = /* @__PURE__ */ __name((code, origin) => transpile(code, origin, wasmModule), "initAndTransform");
var src_default = {
  async fetch(request) {
    if (request.method === "POST") {
      return new Response(
        await initAndTransform(
          await request.text(),
          request.headers.get("TR_ORIGIN"),
          ASSET_HASH
        ).catch(
          async () => await initAndTransform(
            `
        export default ()=><h1>Transpile error</h1>
        `,
            request.headers.get("TR_ORIGIN"),
            ASSET_HASH
          )
        ),
        {
          ...request,
          headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": "*"
          }
        }
      );
    }
    return new Response("try to POST", {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "*"
      }
    });
  }
};

// src/cf-esbuild.mjs
var cf_esbuild_default = src_default;
export {
  cf_esbuild_default as default
};
