'use strict';

var fs7 = require('fs/promises');
var path5 = require('path');
var url = require('url');
require('os');
require('posthog-node');
require('fs');
var child_process = require('child_process');
var util = require('util');

var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var fs7__default = /*#__PURE__*/_interopDefault(fs7);
var path5__default = /*#__PURE__*/_interopDefault(path5);

// src/platform/command-runtime/loader.ts

// src/platform/runtime-profile.ts
var runtimeState = globalThis.__JU_HONG_E_RUNTIME_PROFILE__ ?? (globalThis.__JU_HONG_E_RUNTIME_PROFILE__ = {
  binaryName: "ju-hong-e",
  profile: "all"
});
var ALL_PROFILE_CONFIGS = {
  all: {},
  be: {
    allowedBuiltinCommands: /* @__PURE__ */ new Set([
      "/review",
      "/commit",
      "/change-service",
      "/security-policy",
      "/show-stream",
      "/sh",
      "/token-optimizer",
      "/minify-md",
      "/help",
      "/exit"
    ])
  },
  fe: {}
};
function getProfileConfig(profile = runtimeState.profile) {
  return ALL_PROFILE_CONFIGS[profile];
}
function getAllowedBuiltinCommands(profile = runtimeState.profile) {
  return getProfileConfig(profile).allowedBuiltinCommands;
}

// src/platform/analytics/posthog.ts
globalThis.__JU_HONG_E_POSTHOG_RUNTIME_STATE__ ?? (globalThis.__JU_HONG_E_POSTHOG_RUNTIME_STATE__ = {});
util.promisify(child_process.execFile);

// src/platform/request-security/registry.ts
var securityRegistryState = globalThis.__JUHONG_E_SECURITY_REGISTRY__ ?? (globalThis.__JUHONG_E_SECURITY_REGISTRY__ = {
  patternRules: /* @__PURE__ */ new Map(),
  validatorRules: /* @__PURE__ */ new Map()
});
securityRegistryState.patternRules;
securityRegistryState.validatorRules;

// src/platform/command-runtime/registry.ts
var registryState = globalThis.__JU_HONG_E_COMMAND_REGISTRY__ ?? (globalThis.__JU_HONG_E_COMMAND_REGISTRY__ = {
  juHongECommands: /* @__PURE__ */ new Map()
});
var JU_HONG_E_COMMANDS = registryState.juHongECommands;
function clearCommandRegistry() {
  JU_HONG_E_COMMANDS.clear();
}
function applyBuiltinCommandFilter(allowedCommands) {
  if (!allowedCommands) {
    return;
  }
  for (const commandName of [...JU_HONG_E_COMMANDS.keys()]) {
    if (!allowedCommands.has(commandName)) {
      JU_HONG_E_COMMANDS.delete(commandName);
    }
  }
}

// src/platform/command-runtime/loader.ts
var loadPromise;
async function collectCommandModulePaths(directoryPath) {
  const entries = await fs7__default.default.readdir(directoryPath, { withFileTypes: true });
  const modulePaths = [];
  for (const entry of entries) {
    const nextPath = path5__default.default.join(directoryPath, entry.name);
    if (entry.isDirectory()) {
      modulePaths.push(...await collectCommandModulePaths(nextPath));
      continue;
    }
    if (entry.isFile() && entry.name === "index.js") {
      modulePaths.push(nextPath);
    }
  }
  return modulePaths;
}
async function isCommandModuleRootDirectory(directoryPath) {
  try {
    const entries = await fs7__default.default.readdir(directoryPath, { withFileTypes: true });
    for (const entry of entries) {
      if (!entry.isDirectory()) {
        continue;
      }
      const moduleEntryPath = path5__default.default.join(directoryPath, entry.name, "index.js");
      try {
        const moduleEntryStat = await fs7__default.default.stat(moduleEntryPath);
        if (moduleEntryStat.isFile()) {
          return true;
        }
      } catch {
      }
    }
    return false;
  } catch {
    return false;
  }
}
async function resolveCommandModuleRootDirectoryPath() {
  let currentDirectoryPath = path5__default.default.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('loader.cjs', document.baseURI).href))));
  for (let depth = 0; depth < 8; depth += 1) {
    const candidatePath = path5__default.default.join(currentDirectoryPath, "command-modules");
    if (await isCommandModuleRootDirectory(candidatePath)) {
      return candidatePath;
    }
    const parentDirectoryPath = path5__default.default.dirname(currentDirectoryPath);
    if (parentDirectoryPath === currentDirectoryPath) {
      break;
    }
    currentDirectoryPath = parentDirectoryPath;
  }
  throw new Error(`command-modules \uB8E8\uD2B8\uB97C \uCC3E\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4. start=${url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('loader.cjs', document.baseURI).href)))}`);
}
async function loadCommandModulesInternal() {
  clearCommandRegistry();
  const commandModuleRootDirectoryPath = await resolveCommandModuleRootDirectoryPath();
  const modulePaths = (await collectCommandModulePaths(commandModuleRootDirectoryPath)).sort(
    (left, right) => left.localeCompare(right)
  );
  for (const modulePath of modulePaths) {
    await import(url.pathToFileURL(modulePath).href);
  }
  applyBuiltinCommandFilter(getAllowedBuiltinCommands());
}
async function loadCommandModules() {
  if (!loadPromise) {
    loadPromise = loadCommandModulesInternal();
  }
  return loadPromise;
}

exports.loadCommandModules = loadCommandModules;
