'use strict';

var fs = require('fs');
var module$1 = require('module');
var path = require('path');

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

var fs__default = /*#__PURE__*/_interopDefault(fs);
var path__default = /*#__PURE__*/_interopDefault(path);

// src/features/gen-html/infrastructure/local-server/paths.ts
var DEFAULT_LOCAL_SERVER_PORT = 4173;
var LOCAL_SERVER_HOST = "127.0.0.1";
var LOCAL_SERVER_HEALTH_PATH = "/index.json";
var LOCAL_SERVER_PORT_SEARCH_WINDOW = 20;
var LOCAL_SERVER_START_TIMEOUT_MS = 3e4;
var LOCAL_SERVER_STOP_TIMEOUT_MS = 5e3;
var LOCAL_SERVER_LOCK_STALE_MS = 12e4;
var LOCAL_SERVER_STATE_VERSION = 2;
var LOCAL_SERVER_SASS_VERSION = "^1.86.3";
var LOCAL_SERVER_REACT_QUERY_VERSION = "^5.81.2";
var LOCAL_SERVER_TYPES_NODE_VERSION = "^25.0.10";
var LOCAL_SERVER_TYPES_REACT_VERSION = "19.1.0";
var LOCAL_SERVER_TYPES_REACT_DOM_VERSION = "19.1.1";
var LOCAL_SERVER_TYPESCRIPT_VERSION = "5.8.2";
var LOCAL_SERVER_VITE_VERSION = "^6.3.1";
var STORYBOOK_VERSION = "8.6.14";
var LOCAL_SERVER_LEGACY_FILE_NAMES = ["server.mjs"];
var LOCAL_SERVER_STYLE_EXPORT_FILE_PATTERN = /\.(?:css|s[ac]ss)$/i;
var REQUIRED_LOCAL_SERVER_PUBLISHED_PACKAGE_NAMES = [
  "sales-frontend-api",
  "sales-frontend-assets",
  "sales-frontend-bridge",
  "sales-frontend-components",
  "sales-frontend-design-system",
  "sales-frontend-hooks",
  "sales-frontend-solution",
  "sales-frontend-stores",
  "sales-frontend-typescript-config"
];
var IMPORT_SPECIFIER_PATTERN = /\b(?:import|export)\s+(?:type\s+)?(?:[^'"`]*?\s+from\s*)?['"]([^'"`]+)['"]|\bimport\s*\(\s*['"]([^'"`]+)['"]\s*\)/g;
var SOURCE_FILE_EXTENSION_PATTERN = /\.[cm]?[jt]sx?$/i;
var WORKSPACE_PACKAGE_GROUP_NAMES = ["apps", "packages", "configs"];
var WORKSPACE_ROOT_MARKER_FILE_NAMES = ["pnpm-workspace.yaml", "pnpm-workspace.yml"];
var LOCAL_SERVER_RUNTIME_PACKAGE_NAMES = ["react", "react-dom", "@tanstack/react-query"];
var PACKAGE_MANIFEST_DEPENDENCY_FIELDS = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"];
var NODE_BUILTIN_MODULE_NAMES = new Set(
  module$1.builtinModules.flatMap((moduleName) => [moduleName, moduleName.replace(/^node:/, ""), `node:${moduleName.replace(/^node:/, "")}`])
);
var LOCAL_SERVER_DEFAULT_PACKAGE_JSON = {
  dependencies: {
    "@types/node": LOCAL_SERVER_TYPES_NODE_VERSION,
    "@types/react": LOCAL_SERVER_TYPES_REACT_VERSION,
    "@types/react-dom": LOCAL_SERVER_TYPES_REACT_DOM_VERSION,
    "@tanstack/react-query": LOCAL_SERVER_REACT_QUERY_VERSION,
    "@storybook/addon-essentials": STORYBOOK_VERSION,
    "@storybook/react": STORYBOOK_VERSION,
    "@storybook/react-vite": STORYBOOK_VERSION,
    react: "^19.1.0",
    "react-dom": "^19.1.0",
    sass: LOCAL_SERVER_SASS_VERSION,
    storybook: STORYBOOK_VERSION,
    typescript: LOCAL_SERVER_TYPESCRIPT_VERSION,
    vite: LOCAL_SERVER_VITE_VERSION
  },
  name: "ju-hong-e-local-server",
  private: true,
  scripts: {
    dev: "storybook dev -c ./.storybook",
    start: "storybook dev -c ./.storybook"
  },
  type: "commonjs"
};
function getLocalServerPaths(paths) {
  const directoryPath = path__default.default.join(paths.baseDir, "local-server");
  const generatedDirectoryPath = path__default.default.join(directoryPath, "generated");
  const multipleDirectoryPath = path__default.default.join(directoryPath, "multiple");
  const storybookConfigDirectoryPath = path__default.default.join(directoryPath, ".storybook");
  return {
    directoryPath,
    generatedDirectoryPath,
    generatedRegistryPath: path__default.default.join(directoryPath, "generated-registry.json"),
    multipleDirectoryPath,
    logPath: path__default.default.join(directoryPath, "server.log"),
    packageJsonPath: path__default.default.join(directoryPath, "package.json"),
    packageCustomJsonPath: path__default.default.join(directoryPath, "package-custom.json"),
    previewTargetPath: path__default.default.join(directoryPath, "preview-target.json"),
    refreshLockPath: path__default.default.join(directoryPath, "refresh.lock"),
    scssModuleDeclarationPath: path__default.default.join(directoryPath, "scss.d.ts"),
    serverLockPath: path__default.default.join(directoryPath, "server.lock"),
    statePath: path__default.default.join(directoryPath, "server-state.json"),
    storybookConfigDirectoryPath,
    storybookMainConfigPath: path__default.default.join(storybookConfigDirectoryPath, "main.ts"),
    storybookManagerConfigPath: path__default.default.join(storybookConfigDirectoryPath, "manager.tsx"),
    storybookPreviewConfigPath: path__default.default.join(storybookConfigDirectoryPath, "preview.ts"),
    tsconfigPath: path__default.default.join(directoryPath, "tsconfig.json")
  };
}
function isRecord(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
function readJsonFile(filePath) {
  if (!fs__default.default.existsSync(filePath)) {
    return void 0;
  }
  const raw = fs__default.default.readFileSync(filePath, "utf8").trim();
  if (!raw) {
    return void 0;
  }
  return JSON.parse(raw);
}
function writeJsonFile(filePath, value) {
  fs__default.default.mkdirSync(path__default.default.dirname(filePath), { recursive: true });
  fs__default.default.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}
`, "utf8");
}
function readLocalServerState(paths) {
  try {
    return readJsonFile(getLocalServerPaths(paths).statePath) || {};
  } catch {
    return {};
  }
}
function writeLocalServerState(paths, state) {
  writeJsonFile(getLocalServerPaths(paths).statePath, {
    ...state,
    version: LOCAL_SERVER_STATE_VERSION
  });
}
function updateLocalServerState(paths, patch) {
  const nextState = {
    ...readLocalServerState(paths),
    ...patch,
    version: LOCAL_SERVER_STATE_VERSION
  };
  writeLocalServerState(paths, nextState);
  return nextState;
}
function removeLocalServerState(paths) {
  const { statePath } = getLocalServerPaths(paths);
  if (fs__default.default.existsSync(statePath)) {
    fs__default.default.unlinkSync(statePath);
  }
}

exports.DEFAULT_LOCAL_SERVER_PORT = DEFAULT_LOCAL_SERVER_PORT;
exports.IMPORT_SPECIFIER_PATTERN = IMPORT_SPECIFIER_PATTERN;
exports.LOCAL_SERVER_DEFAULT_PACKAGE_JSON = LOCAL_SERVER_DEFAULT_PACKAGE_JSON;
exports.LOCAL_SERVER_HEALTH_PATH = LOCAL_SERVER_HEALTH_PATH;
exports.LOCAL_SERVER_HOST = LOCAL_SERVER_HOST;
exports.LOCAL_SERVER_LEGACY_FILE_NAMES = LOCAL_SERVER_LEGACY_FILE_NAMES;
exports.LOCAL_SERVER_LOCK_STALE_MS = LOCAL_SERVER_LOCK_STALE_MS;
exports.LOCAL_SERVER_PORT_SEARCH_WINDOW = LOCAL_SERVER_PORT_SEARCH_WINDOW;
exports.LOCAL_SERVER_REACT_QUERY_VERSION = LOCAL_SERVER_REACT_QUERY_VERSION;
exports.LOCAL_SERVER_RUNTIME_PACKAGE_NAMES = LOCAL_SERVER_RUNTIME_PACKAGE_NAMES;
exports.LOCAL_SERVER_SASS_VERSION = LOCAL_SERVER_SASS_VERSION;
exports.LOCAL_SERVER_START_TIMEOUT_MS = LOCAL_SERVER_START_TIMEOUT_MS;
exports.LOCAL_SERVER_STATE_VERSION = LOCAL_SERVER_STATE_VERSION;
exports.LOCAL_SERVER_STOP_TIMEOUT_MS = LOCAL_SERVER_STOP_TIMEOUT_MS;
exports.LOCAL_SERVER_STYLE_EXPORT_FILE_PATTERN = LOCAL_SERVER_STYLE_EXPORT_FILE_PATTERN;
exports.LOCAL_SERVER_TYPESCRIPT_VERSION = LOCAL_SERVER_TYPESCRIPT_VERSION;
exports.LOCAL_SERVER_TYPES_NODE_VERSION = LOCAL_SERVER_TYPES_NODE_VERSION;
exports.LOCAL_SERVER_TYPES_REACT_DOM_VERSION = LOCAL_SERVER_TYPES_REACT_DOM_VERSION;
exports.LOCAL_SERVER_TYPES_REACT_VERSION = LOCAL_SERVER_TYPES_REACT_VERSION;
exports.LOCAL_SERVER_VITE_VERSION = LOCAL_SERVER_VITE_VERSION;
exports.NODE_BUILTIN_MODULE_NAMES = NODE_BUILTIN_MODULE_NAMES;
exports.PACKAGE_MANIFEST_DEPENDENCY_FIELDS = PACKAGE_MANIFEST_DEPENDENCY_FIELDS;
exports.REQUIRED_LOCAL_SERVER_PUBLISHED_PACKAGE_NAMES = REQUIRED_LOCAL_SERVER_PUBLISHED_PACKAGE_NAMES;
exports.SOURCE_FILE_EXTENSION_PATTERN = SOURCE_FILE_EXTENSION_PATTERN;
exports.STORYBOOK_VERSION = STORYBOOK_VERSION;
exports.WORKSPACE_PACKAGE_GROUP_NAMES = WORKSPACE_PACKAGE_GROUP_NAMES;
exports.WORKSPACE_ROOT_MARKER_FILE_NAMES = WORKSPACE_ROOT_MARKER_FILE_NAMES;
exports.getLocalServerPaths = getLocalServerPaths;
exports.isRecord = isRecord;
exports.readJsonFile = readJsonFile;
exports.readLocalServerState = readLocalServerState;
exports.removeLocalServerState = removeLocalServerState;
exports.updateLocalServerState = updateLocalServerState;
exports.writeJsonFile = writeJsonFile;
exports.writeLocalServerState = writeLocalServerState;
