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