import { createRequire } from 'module'; const require = createRequire(import.meta.url);

// packages/dev-tools/remix/index.ts
import { createDevTools } from "../core/index.mjs";
import { createDevToolsNodeSys } from "../node/index.mjs";
import { createDevToolsServer } from "../server/index.mjs";
import { dirname, join, resolve } from "node:path";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
var index_default = (devToolsOpts = {}) => {
  return (remixConfig) => {
    if (devToolsOpts.enabled == null) {
      devToolsOpts.enabled = process.env.NODE_ENV !== "production";
    }
    if (devToolsOpts.enabled === false) {
      return remixConfig;
    }
    const withBuilderConfig = { ...remixConfig };
    const configPath = findRemixConfigPath(process.cwd());
    const rootDir = dirname(configPath);
    process.env.BUILDER_REMIX_DEVTOOLS_URL = `http://localhost:5273/`;
    withBuilderConfig.watchPaths = async () => {
      const sys = await createDevToolsNodeSys({
        cwd: rootDir
      });
      const devTools = await createDevTools(sys);
      const devToolsServer = await createDevToolsServer({
        ...devTools,
        getClientId: () => "remix-builder-dev-tools",
        closeAppServer: async () => {
        },
        restartAppServer: async () => {
          sys.debug("restart server");
          const content = readFileSync(configPath, "utf-8");
          writeFileSync(configPath, content);
        },
        enableAppWatch: async (enable) => {
          return enable;
        },
        ...sys,
        ...devToolsOpts
      });
      process.env.BUILDER_REMIX_DEVTOOLS_URL = devToolsServer.getUrl();
      if (typeof remixConfig.watchPaths === "function") {
        return remixConfig.watchPaths();
      }
      if (remixConfig.watchPaths) {
        return remixConfig.watchPaths;
      }
      return [];
    };
    let dir;
    if (false) {
      dir = __dirname;
    } else {
      dir = dirname(fileURLToPath(import.meta.url));
    }
    const devToolServerEntryPath = join(dir, "server-build", "index.mjs");
    if (!existsSync(devToolServerEntryPath)) {
      console.log(
        `Builder Devtools unable to run: "${devToolServerEntryPath}" not found`
      );
      return remixConfig;
    }
    if (typeof remixConfig.server === "string") {
      const userServerEntryPath = resolve(
        dirname(configPath),
        remixConfig.server
      );
      if (!existsSync(userServerEntryPath)) {
        console.log(
          `Builder Devtools unable to run: "${userServerEntryPath}" set from "${remixConfig.server}" server config not found`
        );
        return remixConfig;
      }
      let madeChanges = false;
      let userServerEntryContent = readFileSync(userServerEntryPath, "utf-8");
      while (userServerEntryContent.includes("@remix-run/dev/server-build")) {
        userServerEntryContent = userServerEntryContent.replace(
          `@remix-run/dev/server-build`,
          `@builder.io/dev-tools/remix/server-build`
        );
        madeChanges = true;
      }
      if (madeChanges) {
        writeFileSync(userServerEntryPath, userServerEntryContent);
      }
    } else {
      withBuilderConfig.server = devToolServerEntryPath;
    }
    return withBuilderConfig;
  };
};
function findRemixConfigPath(dir) {
  const fsRoot = resolve("/");
  for (let i = 0; i < 20; i++) {
    let configPath = join(dir, "remix.config.js");
    if (existsSync(configPath)) {
      return configPath;
    }
    configPath = join(dir, "remix.config.ts");
    if (existsSync(configPath)) {
      return configPath;
    }
    if (dir === fsRoot) {
      break;
    }
    dir = dirname(dir);
  }
  throw new Error(`Could not find Remix config`);
}
export {
  index_default as default
};
