import { createNodeFsMountHandler as W, loadNodeRuntime as _ } from "@php-wasm/node";
import { EmscriptenDownloadMonitor as v } from "@php-wasm/progress";
import { exposeAPI as A, PHPWorker as M, consumeAPI as S, consumeAPISync as F, sandboxedSpawnHandlerFactory as H } from "@php-wasm/universal";
import { sprintf as E } from "@php-wasm/util";
import { RecommendedPHPVersion as I } from "@wp-playground/common";
import { bootWordPress as L } from "@wp-playground/wordpress";
import { rootCertificates as D } from "tls";
import { jspi as R } from "wasm-feature-detect";
import { MessageChannel as x, parentPort as B } from "worker_threads";
import "fs";
import "path";
async function m(r, e) {
  for (const o of e)
    await r.mount(
      o.vfsPath,
      W(o.hostPath)
    );
}
function C(r, e, ...o) {
  console.log(
    performance.now().toFixed(6).padStart(15, "0"),
    r.toString().padStart(16, "0"),
    E(e, ...o)
  );
}
class G extends M {
  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 F(e);
  }
  async bootAsPrimaryWorker({
    absoluteUrl: e,
    mountsBeforeWpInstall: o,
    mountsAfterWpInstall: P,
    phpVersion: f = I,
    wordPressZip: c,
    sqliteIntegrationPluginZip: p,
    firstProcessId: n,
    processIdSpaceLength: u,
    dataSqlPath: h,
    followSymlinks: w,
    trace: y,
    internalCookieStore: b,
    withXdebug: g
  }) {
    if (this.booted)
      throw new Error("Playground already booted");
    this.booted = !0;
    let t = n;
    const k = n + u - 1;
    try {
      const a = {
        WP_DEBUG: !0,
        WP_DEBUG_LOG: !0,
        WP_DEBUG_DISPLAY: !1
      }, l = await L({
        siteUrl: e,
        createPhpRuntime: async () => {
          const s = t;
          return t < k ? t++ : t = n, await _(f, {
            emscriptenOptions: {
              fileLockManager: this.fileLockManager,
              processId: s,
              trace: y ? C : void 0
            },
            followSymlinks: w,
            withXdebug: g
          });
        },
        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(s) {
            m(s, o);
          }
        },
        cookieStore: b ? void 0 : !1,
        dataSqlPath: h,
        spawnHandler: H
      });
      this.__internal_setRequestHandler(l);
      const d = await l.getPrimaryPhp();
      await this.setPrimaryPHP(d), m(d, P), U();
    } catch (a) {
      throw q(a), a;
    }
  }
  async bootAsSecondaryWorker(e) {
    return this.bootAsPrimaryWorker(e);
  }
  // Provide a named disposal method that can be invoked via comlink.
  async dispose() {
    await this[Symbol.asyncDispose]();
  }
}
const i = new x(), [U, q] = A(
  new G(new v()),
  void 0,
  i.port1
);
B.postMessage(
  {
    command: "worker-script-initialized",
    phpPort: i.port2
  },
  [i.port2]
);
//# sourceMappingURL=worker-thread-v1-BTJIbQLy.js.map
