import path from 'node:path';

// src/features/gen-html/domain/response-files.ts
var GEN_HTML_RESPONSE_FILE_HEADER_PATTERN = /^\s*-\s+(.+)$/;
var GEN_HTML_RESPONSE_FILE_SECTION_PATTERN = /파일 내용.*저장/;
function normalizeOutputRelativePath(outputDirectory, filePath) {
  return path.relative(outputDirectory, filePath).split(path.sep).join("/");
}
function isPathInsideDirectory(directoryPath, filePath) {
  const relativePath = path.relative(directoryPath, filePath);
  return relativePath !== "" && relativePath !== ".." && !relativePath.startsWith(`..${path.sep}`) && !path.isAbsolute(relativePath);
}
function resolveGenHtmlResponseFilePath(rootDir, outputDirectory, rawFilePath) {
  const normalizedRawPath = rawFilePath.trim().replace(/^`|`$/g, "").replace(/:$/, "");
  if (!normalizedRawPath) {
    return "";
  }
  const candidatePaths = path.isAbsolute(normalizedRawPath) ? [path.normalize(normalizedRawPath)] : [path.resolve(rootDir, normalizedRawPath), path.resolve(outputDirectory, normalizedRawPath)];
  const resolvedPath = candidatePaths.find((candidatePath) => isPathInsideDirectory(outputDirectory, candidatePath));
  return resolvedPath ? path.normalize(resolvedPath) : "";
}
function extractGenHtmlResponseFileContent(lines, startIndex) {
  const contentLines = [];
  let index = startIndex;
  const firstLine = lines[index] || "";
  const fenceMatch = firstLine.match(/^(\s*)```/);
  if (fenceMatch) {
    const fenceIndent = fenceMatch[1] || "";
    index += 1;
    while (index < lines.length) {
      const line = lines[index] || "";
      if (line.startsWith(`${fenceIndent}\`\`\``)) {
        index += 1;
        break;
      }
      contentLines.push(line.startsWith(fenceIndent) ? line.slice(fenceIndent.length) : line);
      index += 1;
    }
  } else {
    while (index < lines.length) {
      const line = lines[index] || "";
      if (GEN_HTML_RESPONSE_FILE_HEADER_PATTERN.test(line)) {
        break;
      }
      if (!line) {
        contentLines.push("");
        index += 1;
        continue;
      }
      if (line.startsWith("  ")) {
        contentLines.push(line.slice(2));
        index += 1;
        continue;
      }
      if (line.startsWith("	")) {
        contentLines.push(line.slice(1));
        index += 1;
        continue;
      }
      break;
    }
  }
  while (contentLines.length > 0 && contentLines.at(-1) === "") {
    contentLines.pop();
  }
  return {
    content: contentLines.join("\n"),
    nextIndex: index
  };
}
function parseGenHtmlResponseFiles(rootDir, outputDirectory, responseText) {
  const lines = responseText.split(/\r?\n/);
  const sectionStartIndex = lines.findIndex((line) => GEN_HTML_RESPONSE_FILE_SECTION_PATTERN.test(line));
  if (sectionStartIndex < 0) {
    return [];
  }
  const files = [];
  let index = sectionStartIndex + 1;
  while (index < lines.length) {
    const line = lines[index] || "";
    const headerMatch = line.match(GEN_HTML_RESPONSE_FILE_HEADER_PATTERN);
    if (!headerMatch) {
      index += 1;
      continue;
    }
    const absolutePath = resolveGenHtmlResponseFilePath(rootDir, outputDirectory, headerMatch[1] || "");
    if (!absolutePath) {
      index += 1;
      continue;
    }
    const { content, nextIndex } = extractGenHtmlResponseFileContent(lines, index + 1);
    if (content) {
      files.push({
        absolutePath,
        content,
        relativePath: normalizeOutputRelativePath(outputDirectory, absolutePath)
      });
    }
    index = nextIndex;
  }
  return files;
}

export { normalizeOutputRelativePath, parseGenHtmlResponseFiles };
