import { createNodeFsMountHandler as l } from "@php-wasm/node";
import r, { existsSync as c } from "fs";
import m, { basename as p, join as h } from "path";
function $(e) {
  const t = [];
  for (const n of e) {
    const o = n.split(":");
    if (o.length !== 2)
      throw new Error(`Invalid mount format: ${n}.
				Expected format: /host/path:/vfs/path.
				If your path contains a colon, e.g. C:\\myplugin, use the --mount-dir option instead.
				Example: --mount-dir C:\\my-plugin /wordpress/wp-content/plugins/my-plugin`);
    const [s, i] = o;
    if (!c(s))
      throw new Error(`Host path does not exist: ${s}`);
    t.push({ hostPath: s, vfsPath: i });
  }
  return t;
}
function x(e) {
  if (e.length % 2 !== 0)
    throw new Error("Invalid mount format. Expected: /host/path /vfs/path");
  const t = [];
  for (let n = 0; n < e.length; n += 2) {
    const o = e[n], s = e[n + 1];
    if (!c(o))
      throw new Error(`Host path does not exist: ${o}`);
    t.push({
      hostPath: m.resolve(process.cwd(), o),
      vfsPath: s
    });
  }
  return t;
}
async function N(e, t) {
  for (const n of t)
    await e.mount(
      n.vfsPath,
      l(n.hostPath)
    );
}
const u = {
  step: "runPHP",
  code: {
    filename: "activate-theme.php",
    // @TODO: Remove DOCROOT check after moving totally to Blueprints v2.
    content: `<?php
			$docroot = getenv('DOCROOT') ? getenv('DOCROOT') : '/wordpress';
			require_once "$docroot/wp-load.php";
			$theme = wp_get_theme();
			if (!$theme->exists()) {
				$themes = wp_get_themes();
				if (count($themes) > 0) {
					$themeName = array_keys($themes)[0];
					switch_theme($themeName);
				}
			}
		`
  }
};
function E(e) {
  const t = e.autoMount, n = [...e.mount || []], o = [...e["mount-before-install"] || []], s = {
    ...e,
    mount: n,
    "mount-before-install": o,
    "additional-blueprint-steps": [
      ...e["additional-blueprint-steps"] || []
    ]
  };
  if (g(t)) {
    const i = p(t);
    n.push({
      hostPath: t,
      vfsPath: `/wordpress/wp-content/plugins/${i}`
    }), s["additional-blueprint-steps"].push({
      step: "activatePlugin",
      pluginPath: `/wordpress/wp-content/plugins/${p(t)}`
    });
  } else if (w(t)) {
    const i = p(t);
    n.push({
      hostPath: t,
      vfsPath: `/wordpress/wp-content/themes/${i}`
    }), s["additional-blueprint-steps"].push(
      e["experimental-blueprints-v2-runner"] ? {
        step: "activateTheme",
        themeDirectoryName: i
      } : {
        step: "activateTheme",
        themeFolderName: i
      }
    );
  } else if (f(t)) {
    const i = r.readdirSync(t);
    for (const a of i)
      a !== "index.php" && n.push({
        hostPath: `${t}/${a}`,
        vfsPath: `/wordpress/wp-content/${a}`
      });
    s["additional-blueprint-steps"].push(u);
  } else d(t) ? (o.push({ hostPath: t, vfsPath: "/wordpress" }), s.mode = "apply-to-existing-site", s["additional-blueprint-steps"].push(u)) : (n.push({ hostPath: t, vfsPath: "/wordpress" }), s.mode = "mount-only");
  return s;
}
function d(e) {
  const t = r.readdirSync(e);
  return t.includes("wp-admin") && t.includes("wp-includes") && t.includes("wp-content");
}
function f(e) {
  const t = r.readdirSync(e);
  return t.includes("themes") || t.includes("plugins") || t.includes("mu-plugins") || t.includes("uploads");
}
function w(e) {
  if (!r.readdirSync(e).includes("style.css"))
    return !1;
  const n = r.readFileSync(h(e, "style.css"), "utf8");
  return !!/^(?:[ \t]*<\?php)?[ \t/*#@]*Theme Name:(.*)$/im.exec(n);
}
function g(e) {
  const t = r.readdirSync(e), n = /^(?:[ \t]*<\?php)?[ \t/*#@]*Plugin Name:(.*)$/im;
  return !!t.filter((s) => s.endsWith(".php")).find((s) => {
    const i = r.readFileSync(h(e, s), "utf8");
    return !!n.exec(i);
  });
}
export {
  x as a,
  E as e,
  N as m,
  $ as p
};
//# sourceMappingURL=mounts-D1_eXSTw.js.map
