"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
function sanitizeMermaidText(text) {
  return text.replace(/"/g, "&quot;").replace(/\n/g, "<br/>").replace(/[()]/g, "");
}
function getNodeShape(type) {
  switch (type) {
    case "start":
      return { start: "((", end: "))" };
    case "end":
      return { start: "(((", end: ")))" };
    case "choices":
      return { start: "[", end: "]" };
    case "fill_in":
      return { start: "[/", end: "\\]" };
    case "rating":
      return { start: "{{", end: "}}" };
    default:
      return { start: "[", end: "]" };
  }
}
function getNodeLabel(node) {
  const title = sanitizeMermaidText(node.title || node.name);
  const typeEmoji = {
    start: "🚀",
    end: "🏁",
    choices: "☑️",
    fill_in: "✏️",
    rating: "⭐"
  }[node.type] || "📝";
  return `${typeEmoji} ${title}`;
}
function formatCondition(condition, nodes, sourceNodeId) {
  if (condition === "true") {
    return "";
  }
  if ("type" in condition) {
    switch (condition.type) {
      case "option_selected": {
        const cond = condition;
        const optionTexts = [];
        if (sourceNodeId && nodes[sourceNodeId] && nodes[sourceNodeId].type === "choices") {
          const choicesNode = nodes[sourceNodeId];
          for (const optionId of cond.optionIds) {
            const option = choicesNode.options.find((opt) => opt.id === optionId);
            if (option) {
              optionTexts.push(option.text);
            }
          }
        } else {
          for (const nodeId in nodes) {
            const node = nodes[nodeId];
            if (node.type === "choices") {
              const choicesNode = node;
              for (const optionId of cond.optionIds) {
                const option = choicesNode.options.find((opt) => opt.id === optionId);
                if (option) {
                  optionTexts.push(option.text);
                }
              }
            }
          }
        }
        if (optionTexts.length > 0) {
          if (cond.mode === "any") {
            return optionTexts.join(" or ");
          } else if (cond.mode === "all") {
            return optionTexts.join(" and ");
          } else if (cond.mode === "none") {
            return `Not: ${optionTexts.join(" or ")}`;
          }
        }
        return `${cond.mode} of ${cond.optionIds.length} options`;
      }
      case "fill_in_text":
      case "fill_in_number": {
        const cond = condition;
        let optionText = "";
        if (sourceNodeId && nodes[sourceNodeId] && nodes[sourceNodeId].type === "fill_in") {
          const fillInNode = nodes[sourceNodeId];
          const option = fillInNode.options.find((opt) => opt.id === cond.optionId);
          if (option) {
            optionText = option.text;
          }
        } else {
          for (const nodeId in nodes) {
            const node = nodes[nodeId];
            if (node.type === "fill_in") {
              const fillInNode = node;
              const option = fillInNode.options.find((opt) => opt.id === cond.optionId);
              if (option) {
                optionText = option.text;
                break;
              }
            }
          }
        }
        const opMap = {
          includes: "includes",
          equals: "=",
          not_equals: "≠",
          gt: ">",
          lt: "<"
        };
        const prefix = optionText ? `${optionText} ` : "";
        return `${prefix}${opMap[cond.op] || cond.op} ${cond.value}`;
      }
      case "rating_score": {
        const cond = condition;
        let optionText = "";
        if (sourceNodeId && nodes[sourceNodeId] && nodes[sourceNodeId].type === "rating") {
          const ratingNode = nodes[sourceNodeId];
          const option = ratingNode.options.find((opt) => opt.id === cond.optionId);
          if (option) {
            optionText = option.text;
          }
        } else {
          for (const nodeId in nodes) {
            const node = nodes[nodeId];
            if (node.type === "rating") {
              const ratingNode = node;
              const option = ratingNode.options.find((opt) => opt.id === cond.optionId);
              if (option) {
                optionText = option.text;
                break;
              }
            }
          }
        }
        const opMap = {
          equals: "=",
          not_equals: "≠",
          gt: ">",
          lt: "<",
          gte: "≥",
          lte: "≤"
        };
        const prefix = optionText ? `${optionText} ` : "";
        return `${prefix}${opMap[cond.op] || cond.op} ${cond.value}`;
      }
      case "and":
      case "or": {
        const cond = condition;
        const operator = cond.type === "and" ? " AND " : " OR ";
        return cond.conditions.map((c) => formatCondition(c, nodes, sourceNodeId)).filter(Boolean).join(operator);
      }
    }
  }
  return "";
}
function surveyToMermaid(survey) {
  const lines = ["graph TD"];
  const { nodes, edges } = survey;
  Object.entries(nodes).forEach(([nodeId, node]) => {
    const shape = getNodeShape(node.type);
    const label = getNodeLabel(node);
    lines.push(`    ${nodeId}${shape.start}"${label}"${shape.end}`);
  });
  lines.push("");
  const sortedEdges = Object.values(edges).sort((a, b) => a.priority - b.priority);
  sortedEdges.forEach((edge) => {
    const conditionLabel = formatCondition(edge.condition, nodes, edge.sourceNodeId);
    const edgeLabel = edge.label ? sanitizeMermaidText(edge.label) : conditionLabel;
    if (edgeLabel) {
      lines.push(`    ${edge.sourceNodeId} -->|"${edgeLabel}"| ${edge.targetNodeId}`);
    } else {
      lines.push(`    ${edge.sourceNodeId} --> ${edge.targetNodeId}`);
    }
  });
  lines.push("");
  lines.push("    %% Styling");
  lines.push("    classDef startNode fill:#4ade80,stroke:#16a34a,stroke-width:2px,color:#052e16");
  lines.push("    classDef endNode fill:#f87171,stroke:#dc2626,stroke-width:2px,color:#450a0a");
  lines.push("    classDef choicesNode fill:#60a5fa,stroke:#2563eb,stroke-width:2px,color:#172554");
  lines.push("    classDef fillInNode fill:#fbbf24,stroke:#d97706,stroke-width:2px,color:#451a03");
  lines.push("    classDef ratingNode fill:#a78bfa,stroke:#7c3aed,stroke-width:2px,color:#2e1065");
  Object.entries(nodes).forEach(([nodeId, node]) => {
    const className = `${node.type}Node`;
    lines.push(`    class ${nodeId} ${className}`);
  });
  return lines.join("\n");
}
exports.surveyToMermaid = surveyToMermaid;
//# sourceMappingURL=survey-to-mermaid.cjs.map
