'use strict';

var child_process = require('child_process');
var fs5 = require('fs');
var path3 = require('path');
var util = require('util');
var module$1 = require('module');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var fs5__default = /*#__PURE__*/_interopDefault(fs5);
var path3__default = /*#__PURE__*/_interopDefault(path3);

// src/features/gen-html/infrastructure/local-server/dependencies/installer.ts
function stringifyUnknown(value) {
  if (value === void 0 || value === null) {
    return "";
  }
  if (typeof value === "string") {
    return value;
  }
  if (Buffer.isBuffer(value)) {
    return value.toString("utf8");
  }
  if (value instanceof Error) {
    return value.stack || value.message;
  }
  return util.inspect(value, { depth: 6, breakLength: 120 });
}
function getErrorSummary(error) {
  if (error instanceof Error) {
    return `${error.name}: ${error.message}`;
  }
  return stringifyUnknown(error) || "Unknown error";
}
var LOCAL_SERVER_LEGACY_FILE_NAMES = ["server.mjs"];
var LOCAL_SERVER_STYLE_EXPORT_FILE_PATTERN = /\.(?:css|s[ac]ss)$/i;
new Set(
  module$1.builtinModules.flatMap((moduleName) => [moduleName, moduleName.replace(/^node:/, ""), `node:${moduleName.replace(/^node:/, "")}`])
);
function getLocalServerPaths(paths) {
  const directoryPath = path3__default.default.join(paths.baseDir, "local-server");
  const generatedDirectoryPath = path3__default.default.join(directoryPath, "generated");
  const multipleDirectoryPath = path3__default.default.join(directoryPath, "multiple");
  const storybookConfigDirectoryPath = path3__default.default.join(directoryPath, ".storybook");
  return {
    directoryPath,
    generatedDirectoryPath,
    generatedRegistryPath: path3__default.default.join(directoryPath, "generated-registry.json"),
    multipleDirectoryPath,
    logPath: path3__default.default.join(directoryPath, "server.log"),
    packageJsonPath: path3__default.default.join(directoryPath, "package.json"),
    packageCustomJsonPath: path3__default.default.join(directoryPath, "package-custom.json"),
    previewTargetPath: path3__default.default.join(directoryPath, "preview-target.json"),
    refreshLockPath: path3__default.default.join(directoryPath, "refresh.lock"),
    scssModuleDeclarationPath: path3__default.default.join(directoryPath, "scss.d.ts"),
    serverLockPath: path3__default.default.join(directoryPath, "server.lock"),
    statePath: path3__default.default.join(directoryPath, "server-state.json"),
    storybookConfigDirectoryPath,
    storybookMainConfigPath: path3__default.default.join(storybookConfigDirectoryPath, "main.ts"),
    storybookManagerConfigPath: path3__default.default.join(storybookConfigDirectoryPath, "manager.tsx"),
    storybookPreviewConfigPath: path3__default.default.join(storybookConfigDirectoryPath, "preview.ts"),
    tsconfigPath: path3__default.default.join(directoryPath, "tsconfig.json")
  };
}
function isRecord(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
function readJsonFile(filePath) {
  if (!fs5__default.default.existsSync(filePath)) {
    return void 0;
  }
  const raw = fs5__default.default.readFileSync(filePath, "utf8").trim();
  if (!raw) {
    return void 0;
  }
  return JSON.parse(raw);
}

// src/features/gen-html/infrastructure/local-server/dependencies/manifest.ts
function normalizeDependencies(value) {
  if (!isRecord(value)) {
    return {};
  }
  return Object.entries(value).reduce((accumulator, [key, dependencyValue]) => {
    if (typeof dependencyValue === "string" && dependencyValue.trim()) {
      accumulator[key] = dependencyValue;
    }
    return accumulator;
  }, {});
}
function resolveLocalServerStyleExportTarget(value) {
  if (typeof value === "string") {
    return LOCAL_SERVER_STYLE_EXPORT_FILE_PATTERN.test(value) ? value : "";
  }
  if (Array.isArray(value)) {
    for (const entry of value) {
      const resolvedTarget = resolveLocalServerStyleExportTarget(entry);
      if (resolvedTarget) {
        return resolvedTarget;
      }
    }
    return "";
  }
  if (isRecord(value)) {
    return resolveLocalServerStyleExportTarget(value.default) || resolveLocalServerStyleExportTarget(value.import) || resolveLocalServerStyleExportTarget(value.require);
  }
  return "";
}
function buildLocalServerStyleExportBridgeContent(bridgeFilePath, targetFilePath) {
  const relativeTargetPath = path3__default.default.relative(path3__default.default.dirname(bridgeFilePath), targetFilePath).split(path3__default.default.sep).join("/");
  const normalizedTargetPath = relativeTargetPath.startsWith(".") ? relativeTargetPath : `./${relativeTargetPath}`;
  if (path3__default.default.extname(targetFilePath).toLowerCase() === ".css") {
    return `@import '${normalizedTargetPath}';
`;
  }
  return `@forward '${normalizedTargetPath}';
`;
}
function synchronizeLocalServerStyleExportBridges(paths, trace) {
  const { directoryPath, packageJsonPath } = getLocalServerPaths(paths);
  const localServerPackageJson = readJsonFile(packageJsonPath);
  const dependencyNames = Object.keys(normalizeDependencies(localServerPackageJson?.dependencies));
  for (const dependencyName of dependencyNames) {
    const dependencyDirectoryPath = path3__default.default.join(directoryPath, "node_modules", ...dependencyName.split("/"));
    const dependencyPackageJsonPath = path3__default.default.join(dependencyDirectoryPath, "package.json");
    const dependencyPackageJson = readJsonFile(dependencyPackageJsonPath);
    const dependencyExports = isRecord(dependencyPackageJson?.exports) ? dependencyPackageJson.exports : {};
    for (const [exportKey, exportValue] of Object.entries(dependencyExports)) {
      if (!exportKey.startsWith("./")) {
        continue;
      }
      const exportTargetPath = resolveLocalServerStyleExportTarget(exportValue);
      if (!exportTargetPath) {
        continue;
      }
      const exportSubpath = exportKey.slice(2);
      if (!exportSubpath) {
        continue;
      }
      const targetFilePath = path3__default.default.resolve(dependencyDirectoryPath, exportTargetPath);
      if (!fs5__default.default.existsSync(targetFilePath) || !fs5__default.default.statSync(targetFilePath).isFile()) {
        trace("local-server:style-export-bridge:missing-target", `${dependencyName} ${exportKey} -> ${targetFilePath}`);
        continue;
      }
      const bridgeFilePath = path3__default.default.join(
        dependencyDirectoryPath,
        ...exportSubpath.split("/").filter(Boolean)
      ) + path3__default.default.extname(targetFilePath);
      const bridgeContent = buildLocalServerStyleExportBridgeContent(bridgeFilePath, targetFilePath);
      const currentBridgeContent = fs5__default.default.existsSync(bridgeFilePath) ? fs5__default.default.readFileSync(bridgeFilePath, "utf8") : "";
      if (currentBridgeContent === bridgeContent) {
        continue;
      }
      fs5__default.default.mkdirSync(path3__default.default.dirname(bridgeFilePath), { recursive: true });
      fs5__default.default.writeFileSync(bridgeFilePath, bridgeContent, "utf8");
      trace("local-server:style-export-bridge:written", `${dependencyName} ${exportKey} -> ${bridgeFilePath}`);
    }
  }
}

// src/features/gen-html/infrastructure/local-server/dependencies/installer.ts
var LOCAL_SERVER_NPM_INSTALL_ARGS = ["install", "--legacy-peer-deps", "--no-audit", "--no-fund"];
function hasAllLocalServerDependencies(directoryPath) {
  const packageJsonPath = path3__default.default.join(directoryPath, "package.json");
  const packageJson = readJsonFile(packageJsonPath);
  const dependencies = Object.keys(normalizeDependencies(packageJson?.dependencies));
  if (dependencies.length === 0) {
    return true;
  }
  return dependencies.every((dependencyName) => fs5__default.default.existsSync(path3__default.default.join(directoryPath, "node_modules", ...dependencyName.split("/"))));
}
function installLocalServerDependencies(directoryPath, trace) {
  trace("local-server:install:start", `${directoryPath} :: npm ${LOCAL_SERVER_NPM_INSTALL_ARGS.join(" ")}`);
  const result = child_process.spawnSync("npm", [...LOCAL_SERVER_NPM_INSTALL_ARGS], {
    cwd: directoryPath,
    encoding: "utf8",
    maxBuffer: 1024 * 1024 * 20,
    stdio: "inherit"
  });
  if (result.error || result.status !== 0) {
    trace("local-server:install:failed", getErrorSummary(result.error || result.stderr || result.status));
    throw new Error("\uB85C\uCEEC \uD504\uB9AC\uBDF0 \uC11C\uBC84 \uC758\uC874\uC131 \uC124\uCE58\uC5D0 \uC2E4\uD328\uD588\uC2B5\uB2C8\uB2E4.");
  }
  trace("local-server:install:done", directoryPath);
}
function ensureLocalServerDependencies(paths, trace, forceInstall = false) {
  const { directoryPath } = getLocalServerPaths(paths);
  const nodeModulesPath = path3__default.default.join(directoryPath, "node_modules");
  const hasNodeModules = fs5__default.default.existsSync(nodeModulesPath);
  const hasRequiredDependencies = hasNodeModules ? hasAllLocalServerDependencies(directoryPath) : false;
  const shouldInstall = forceInstall || !hasNodeModules || !hasRequiredDependencies;
  if (hasNodeModules && !hasRequiredDependencies) {
    trace("local-server:install:repair", directoryPath);
  }
  if (shouldInstall) {
    installLocalServerDependencies(directoryPath, trace);
  }
  synchronizeLocalServerStyleExportBridges(paths, trace);
}
function removeLegacyLocalServerFiles(localServerPaths, trace) {
  for (const fileName of LOCAL_SERVER_LEGACY_FILE_NAMES) {
    const targetPath = path3__default.default.join(localServerPaths.directoryPath, fileName);
    if (!fs5__default.default.existsSync(targetPath)) {
      continue;
    }
    fs5__default.default.unlinkSync(targetPath);
    trace("local-server:cleanup:legacy-file", targetPath);
  }
}

exports.ensureLocalServerDependencies = ensureLocalServerDependencies;
exports.hasAllLocalServerDependencies = hasAllLocalServerDependencies;
exports.installLocalServerDependencies = installLocalServerDependencies;
exports.removeLegacyLocalServerFiles = removeLegacyLocalServerFiles;
