"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/dev-tools/remix/index.ts
var index_exports = {};
__export(index_exports, {
  default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var import_core = require("../core/index.cjs");
var import_node = require("../node/index.cjs");
var import_server = require("../server/index.cjs");
var import_node_path = require("node:path");
var import_node_fs = require("node:fs");
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 = (0, import_node_path.dirname)(configPath);
    process.env.BUILDER_REMIX_DEVTOOLS_URL = `http://localhost:5273/`;
    withBuilderConfig.watchPaths = async () => {
      const sys = await (0, import_node.createDevToolsNodeSys)({
        cwd: rootDir
      });
      const devTools = await (0, import_core.createDevTools)(sys);
      const devToolsServer = await (0, import_server.createDevToolsServer)({
        ...devTools,
        getClientId: () => "remix-builder-dev-tools",
        closeAppServer: async () => {
        },
        restartAppServer: async () => {
          sys.debug("restart server");
          const content = (0, import_node_fs.readFileSync)(configPath, "utf-8");
          (0, import_node_fs.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 (true) {
      dir = __dirname;
    } else {
      dir = (0, import_node_path.dirname)(fileURLToPath(import_meta.url));
    }
    const devToolServerEntryPath = (0, import_node_path.join)(dir, "server-build", "index.mjs");
    if (!(0, import_node_fs.existsSync)(devToolServerEntryPath)) {
      console.log(
        `Builder Devtools unable to run: "${devToolServerEntryPath}" not found`
      );
      return remixConfig;
    }
    if (typeof remixConfig.server === "string") {
      const userServerEntryPath = (0, import_node_path.resolve)(
        (0, import_node_path.dirname)(configPath),
        remixConfig.server
      );
      if (!(0, import_node_fs.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 = (0, import_node_fs.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) {
        (0, import_node_fs.writeFileSync)(userServerEntryPath, userServerEntryContent);
      }
    } else {
      withBuilderConfig.server = devToolServerEntryPath;
    }
    return withBuilderConfig;
  };
};
function findRemixConfigPath(dir) {
  const fsRoot = (0, import_node_path.resolve)("/");
  for (let i = 0; i < 20; i++) {
    let configPath = (0, import_node_path.join)(dir, "remix.config.js");
    if ((0, import_node_fs.existsSync)(configPath)) {
      return configPath;
    }
    configPath = (0, import_node_path.join)(dir, "remix.config.ts");
    if ((0, import_node_fs.existsSync)(configPath)) {
      return configPath;
    }
    if (dir === fsRoot) {
      break;
    }
    dir = (0, import_node_path.dirname)(dir);
  }
  throw new Error(`Could not find Remix config`);
}
