'use strict';

var fs5 = require('fs');
var path3 = require('path');
require('child_process');
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/retouch/application/retouch-files.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";
}
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")
  };
}

// src/features/retouch/application/retouch-files.ts
var RETOUCH_SOURCE_FILE_EXTENSIONS = /* @__PURE__ */ new Set([".tsx", ".ts", ".jsx", ".js", ".scss", ".css"]);
function collectRetouchSourceFiles(directory) {
  if (!fs5__default.default.existsSync(directory)) {
    return [];
  }
  return fs5__default.default.readdirSync(directory, { withFileTypes: true }).filter((entry) => entry.isFile() && RETOUCH_SOURCE_FILE_EXTENSIONS.has(path3__default.default.extname(entry.name))).map((entry) => {
    const absolutePath = path3__default.default.join(directory, entry.name);
    return {
      absolutePath,
      content: fs5__default.default.readFileSync(absolutePath, "utf8"),
      relativePath: entry.name
    };
  }).sort((left, right) => left.relativePath.localeCompare(right.relativePath));
}
function computeRetouchFileChanges(directory, beforeFiles, afterFiles) {
  const beforeMap = new Map(beforeFiles.map((file) => [file.relativePath, file.content]));
  const afterMap = new Map(afterFiles.map((file) => [file.relativePath, file.content]));
  const relativePaths = Array.from(/* @__PURE__ */ new Set([...beforeMap.keys(), ...afterMap.keys()])).sort((left, right) => left.localeCompare(right));
  return relativePaths.map((relativePath) => ({
    absolutePath: path3__default.default.join(directory, relativePath),
    after: afterMap.get(relativePath) ?? "",
    before: beforeMap.get(relativePath) ?? "",
    relativePath
  })).filter((change) => change.before !== change.after);
}
function resolveRetouchTargetFile(paths, importPath) {
  const localServerPaths = getLocalServerPaths(paths);
  const absolutePath = path3__default.default.resolve(localServerPaths.directoryPath, importPath);
  const relativeToGenerated = path3__default.default.relative(localServerPaths.generatedDirectoryPath, absolutePath);
  if (relativeToGenerated === "" || relativeToGenerated.startsWith("..") || path3__default.default.isAbsolute(relativeToGenerated)) {
    throw new Error(`generated \uB514\uB809\uD130\uB9AC \uBC16\uC758 \uD30C\uC77C\uC740 \uD3B8\uC9D1\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4. importPath=${importPath}`);
  }
  if (!fs5__default.default.existsSync(absolutePath)) {
    throw new Error(`\uB300\uC0C1 \uC2A4\uD1A0\uB9AC \uD30C\uC77C\uC744 \uCC3E\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4. ${absolutePath}`);
  }
  return { absolutePath, directory: path3__default.default.dirname(absolutePath) };
}
function applyRetouchFiles(paths, files, trace) {
  const { generatedDirectoryPath } = getLocalServerPaths(paths);
  const written = [];
  for (const file of files) {
    const normalizedPath = path3__default.default.normalize(file.absolutePath);
    const relativeToGenerated = path3__default.default.relative(generatedDirectoryPath, normalizedPath);
    if (relativeToGenerated === "" || relativeToGenerated.startsWith("..") || path3__default.default.isAbsolute(relativeToGenerated)) {
      throw new Error(`generated \uB514\uB809\uD130\uB9AC \uBC16\uC758 \uD30C\uC77C\uC740 \uC800\uC7A5\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4. ${file.absolutePath}`);
    }
    try {
      fs5__default.default.mkdirSync(path3__default.default.dirname(normalizedPath), { recursive: true });
      fs5__default.default.writeFileSync(normalizedPath, file.content, "utf8");
      written.push(normalizedPath);
    } catch (error) {
      trace("retouch:apply:failed", `${normalizedPath} ${getErrorSummary(error)}`);
      throw error;
    }
  }
  trace("retouch:apply:done", `written=${written.length}`);
  return written;
}

exports.applyRetouchFiles = applyRetouchFiles;
exports.collectRetouchSourceFiles = collectRetouchSourceFiles;
exports.computeRetouchFileChanges = computeRetouchFileChanges;
exports.resolveRetouchTargetFile = resolveRetouchTargetFile;
