import { randomBytes } from 'node:crypto';

// src/features/gen-html/domain/storybook.ts
var STORY_FILE_PATTERN = /\.stories\.[cm]?[jt]sx?$/i;
var STORY_META_TITLE_PATTERN = /(\btitle\s*:\s*)(['"`])([^'"`]+)\2/;
function createStoryTitlePrefix() {
  return `jh-${randomBytes(3).toString("hex")}`;
}
function buildGenHtmlMaterializationSummary(writtenFiles, prefixedFiles) {
  return [
    `- writtenFiles: ${writtenFiles.length > 0 ? writtenFiles.map((filePath) => `\`${filePath}\``).join(", ") : "(\uC5C6\uC74C)"}`,
    prefixedFiles.length > 0 ? `- storyTitlePrefixedFiles: ${prefixedFiles.map((filePath) => `\`${filePath}\``).join(", ")}` : ""
  ].filter(Boolean).join("\n");
}
function isGenHtmlStoryFilePath(filePath) {
  return STORY_FILE_PATTERN.test(filePath);
}
function normalizeStoryTitlePrefix(prefix) {
  return prefix.trim().replace(/^\/+|\/+$/g, "");
}
function ensureStoryTitlePrefix(title, prefix) {
  const normalizedPrefix = normalizeStoryTitlePrefix(prefix);
  if (!normalizedPrefix) {
    return title.trim();
  }
  const normalizedTitle = title.trim().replace(/^\/+/, "");
  return normalizedTitle.startsWith(`${normalizedPrefix}/`) ? normalizedTitle : `${normalizedPrefix}/${normalizedTitle}`;
}
function applyStorybookTitlePrefixToStorySource(content, prefix) {
  const normalizedPrefix = normalizeStoryTitlePrefix(prefix);
  if (!normalizedPrefix) {
    return content;
  }
  const match = content.match(STORY_META_TITLE_PATTERN);
  if (!match) {
    return content;
  }
  const nextTitle = ensureStoryTitlePrefix(match[3] || "", normalizedPrefix);
  return content.replace(STORY_META_TITLE_PATTERN, (_fullMatch, titlePrefix, quoteCharacter) => {
    return `${titlePrefix}${quoteCharacter}${nextTitle}${quoteCharacter}`;
  });
}

export { applyStorybookTitlePrefixToStorySource, buildGenHtmlMaterializationSummary, createStoryTitlePrefix, isGenHtmlStoryFilePath };
