import { loadNodeRuntime as k } from "@php-wasm/node";
import { EmscriptenDownloadMonitor as v } from "@php-wasm/progress";
import { exposeAPI as M, PHPWorker as S, consumeAPI as W, consumeAPISync as E, sandboxedSpawnHandlerFactory as F } from "@php-wasm/universal";
import { sprintf as H } from "@php-wasm/util";
import { RecommendedPHPVersion as I } from "@wp-playground/common";
import { bootWordPress as L } from "@wp-playground/wordpress";
import { rootCertificates as x } from "tls";
import { jspi as A } from "wasm-feature-detect";
import { MessageChannel as D, parentPort as R } from "worker_threads";
import { m } from "./mounts-B-Qdcyyt.js";
function C(n, e, ...t) {
  console.log(
    performance.now().toFixed(6).padStart(15, "0"),
    n.toString().padStart(16, "0"),
    H(e, ...t)
  );
}
class G extends S {
  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 A() ? this.fileLockManager = W(e) : this.fileLockManager = await E(e);
  }
  async boot({
    absoluteUrl: e,
    mountsBeforeWpInstall: t,
    mountsAfterWpInstall: f,
    phpVersion: P = I,
    wordPressZip: c,
    sqliteIntegrationPluginZip: p,
    firstProcessId: i,
    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 o = i;
    const _ = i + h - 1;
    try {
      const r = {
        WP_DEBUG: !0,
        WP_DEBUG_LOG: !0,
        WP_DEBUG_DISPLAY: !1
      }, l = await L({
        siteUrl: e,
        createPhpRuntime: async () => {
          const s = o;
          return o < _ ? o++ : o = i, await k(P, {
            emscriptenOptions: {
              fileLockManager: this.fileLockManager,
              processId: s,
              trace: y ? C : 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": x.join(`
`)
        },
        constants: r,
        phpIniEntries: {
          "openssl.cafile": "/internal/shared/ca-bundle.crt",
          allow_url_fopen: "1",
          disable_functions: ""
        },
        hooks: {
          async beforeWordPressFiles(s) {
            m(s, t);
          }
        },
        cookieStore: g ? void 0 : !1,
        dataSqlPath: u,
        spawnHandler: F
      });
      this.__internal_setRequestHandler(l);
      const d = await l.getPrimaryPhp();
      await this.setPrimaryPHP(d), m(d, f), U();
    } catch (r) {
      throw q(r), r;
    }
  }
  // Provide a named disposal method that can be invoked via comlink.
  async dispose() {
    await this[Symbol.asyncDispose]();
  }
}
const a = new D(), [U, q] = M(
  new G(new v()),
  void 0,
  a.port1
);
R.postMessage(
  {
    command: "worker-script-initialized",
    phpPort: a.port2
  },
  [a.port2]
);
export {
  G as PlaygroundCliWorker
};
//# sourceMappingURL=worker-thread.js.map
