import { createNodeFsMountHandler as k, loadNodeRuntime as v } from "@php-wasm/node";
import { EmscriptenDownloadMonitor as M } from "@php-wasm/progress";
import { exposeAPI as F, PHPWorker as H, consumeAPI as S, consumeAPISync as W, sandboxedSpawnHandlerFactory as E } from "@php-wasm/universal";
import { sprintf as I } from "@php-wasm/util";
import { RecommendedPHPVersion as L } from "@wp-playground/common";
import { bootWordPress as A } from "@wp-playground/wordpress";
import { rootCertificates as D } from "tls";
import { jspi as R } from "wasm-feature-detect";
import { MessageChannel as x, parentPort as C } from "worker_threads";
import "fs";
import "path";
async function m(t, e) {
  for (const o of e)
    await t.mount(
      o.vfsPath,
      k(o.hostPath)
    );
}
function G(t, e, ...o) {
  console.log(
    performance.now().toFixed(6).padStart(15, "0"),
    t.toString().padStart(16, "0"),
    I(e, ...o)
  );
}
class U extends H {
  constructor(e) {
    super(void 0, e), this.booted = !1;
  }
  /**
   * Call this method before boot() to use file locking.
   *
   * This method is separate from boot() to simplify the related Comlink.transferHandlers
   * setup – if an argument is a MessagePort, we're transferring it, not copying it.
   *
   * @see comlink-sync.ts
   * @see phpwasm-emscripten-library-file-locking-for-node.js
   */
  async useFileLockManager(e) {
    await R() ? this.fileLockManager = S(e) : this.fileLockManager = await W(e);
  }
  async boot({
    absoluteUrl: e,
    mountsBeforeWpInstall: o,
    mountsAfterWpInstall: f,
    phpVersion: P = L,
    wordPressZip: c,
    sqliteIntegrationPluginZip: p,
    firstProcessId: n,
    processIdSpaceLength: h,
    dataSqlPath: u,
    followSymlinks: w,
    trace: y,
    internalCookieStore: g,
    withXdebug: b
  }) {
    if (this.booted)
      throw new Error("Playground already booted");
    this.booted = !0;
    let r = n;
    const _ = n + h - 1;
    try {
      const a = {
        WP_DEBUG: !0,
        WP_DEBUG_LOG: !0,
        WP_DEBUG_DISPLAY: !1
      }, l = await A({
        siteUrl: e,
        createPhpRuntime: async () => {
          const i = r;
          return r < _ ? r++ : r = n, await v(P, {
            emscriptenOptions: {
              fileLockManager: this.fileLockManager,
              processId: i,
              trace: y ? G : void 0
            },
            followSymlinks: w,
            withXdebug: b
          });
        },
        wordPressZip: c !== void 0 ? new File([c], "wordpress.zip") : void 0,
        sqliteIntegrationPluginZip: p !== void 0 ? new File(
          [p],
          "sqlite-integration-plugin.zip"
        ) : void 0,
        sapiName: "cli",
        createFiles: {
          "/internal/shared/ca-bundle.crt": D.join(`
`)
        },
        constants: a,
        phpIniEntries: {
          "openssl.cafile": "/internal/shared/ca-bundle.crt",
          allow_url_fopen: "1",
          disable_functions: ""
        },
        hooks: {
          async beforeWordPressFiles(i) {
            m(i, o);
          }
        },
        cookieStore: g ? void 0 : !1,
        dataSqlPath: u,
        spawnHandler: E
      });
      this.__internal_setRequestHandler(l);
      const d = await l.getPrimaryPhp();
      await this.setPrimaryPHP(d), m(d, f), q();
    } catch (a) {
      throw z(a), a;
    }
  }
  // Provide a named disposal method that can be invoked via comlink.
  async dispose() {
    await this[Symbol.asyncDispose]();
  }
}
const s = new x(), [q, z] = F(
  new U(new M()),
  void 0,
  s.port1
);
C.postMessage(
  {
    command: "worker-script-initialized",
    phpPort: s.port2
  },
  [s.port2]
);
//# sourceMappingURL=worker-thread-CYvRK9UX.js.map
