function parseFrontmatter(content) {
  const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
  if (!frontmatterMatch) {
    return { frontmatter: {}, body: content };
  }
  const [, yamlContent, body] = frontmatterMatch;
  const frontmatter = {};
  yamlContent.split("\n").forEach((line) => {
    const match = line.match(/^(\w+):\s*(.+)$/);
    if (match) {
      const [, key, value] = match;
      if (value.startsWith("[") && value.endsWith("]")) {
        frontmatter[key] = value.slice(1, -1).split(",").map((s) => s.trim());
      } else {
        frontmatter[key] = value;
      }
    }
  });
  return { frontmatter, body };
}
function parseFeature(frontmatter, body) {
  const titleMatch = body.match(/^#\s+(.+)$/m);
  const descMatch = body.match(/^>\s+(.+)$/m);
  let grepTags = frontmatter.grepTags;
  if (grepTags) {
    grepTags = grepTags.map((tag) => tag.replace(/^['"]|['"]$/g, ""));
  }
  return {
    title: frontmatter.feature || (titleMatch == null ? void 0 : titleMatch[1]) || "Untitled Feature",
    description: (descMatch == null ? void 0 : descMatch[1]) || "",
    priority: frontmatter.priority,
    tags: frontmatter.tags,
    grepTags,
    coverage: frontmatter.coverage ? parseInt(String(frontmatter.coverage), 10) : void 0
  };
}
function parseTestMetadata(section) {
  const metadata = {};
  const priorityMatch = section.match(/\*\*Priority:\*\*\s*(\w+)/i);
  if (priorityMatch) {
    metadata.priority = priorityMatch[1].toLowerCase();
  }
  const typeMatch = section.match(/\*\*Type:\*\*\s*(.+?)(?:\n|$)/i);
  if (typeMatch) {
    const rawType = typeMatch[1].trim().toLowerCase();
    if (rawType.includes("selector")) {
      metadata.type = "selector";
    } else {
      metadata.type = rawType.split(/\s+/)[0];
    }
  }
  const tagsMatch = section.match(/\*\*Tags:\*\*\s*(.+)/i);
  if (tagsMatch) {
    metadata.tags = tagsMatch[1].split(",").map((t) => t.trim());
  }
  const automatedMatch = section.match(/\*\*Automated:\*\*\s*(yes|true)/i);
  metadata.automated = !!automatedMatch;
  const statusMatch = section.match(/\*\*Status:\*\*\s*(\w+)(?:\s*[-–]\s*(.+))?/i);
  if (statusMatch) {
    const [, status, reason] = statusMatch;
    const normalizedStatus = status.toLowerCase();
    if (normalizedStatus === "skip" || normalizedStatus === "skipped") {
      metadata.status = "skipped";
    } else if (normalizedStatus === "pass" || normalizedStatus === "passing" || normalizedStatus === "passed") {
      metadata.status = "passing";
    } else if (normalizedStatus === "fail" || normalizedStatus === "failing" || normalizedStatus === "failed") {
      metadata.status = "failing";
    } else if (normalizedStatus === "pending" || normalizedStatus === "todo") {
      metadata.status = "pending";
    } else if (normalizedStatus === "active") {
      metadata.status = "active";
    } else {
      metadata.status = normalizedStatus;
    }
    if (reason) {
      metadata.statusReason = reason.trim();
    }
  }
  const grepMatch = section.match(/\*\*Grep:\*\*\s*(.+)/i);
  if (grepMatch) {
    const grepTags = grepMatch[1].match(/@[\w-]+/g);
    if (grepTags && grepTags.length > 0) {
      metadata.grepTags = grepTags;
    }
  }
  return metadata;
}
function extractGherkinScenarios(section) {
  const scenarios = {};
  const langBlockPattern = /```gherkin:(en|es)\n([\s\S]*?)```/g;
  let match;
  while ((match = langBlockPattern.exec(section)) !== null) {
    const [, lang, content] = match;
    scenarios[lang] = content.trim();
  }
  if (!scenarios.en && !scenarios.es) {
    const genericMatch = section.match(/```gherkin\n([\s\S]*?)```/);
    if (genericMatch) {
      scenarios.en = genericMatch[1].trim();
    }
  }
  return scenarios;
}
function extractGherkinScenario(section) {
  const scenarios = extractGherkinScenarios(section);
  return scenarios.en || scenarios.es || "";
}
function extractExpectedResults(section) {
  const resultsMatch = section.match(/### Expected Results\n([\s\S]*?)(?=\n---|\n## @test|$)/);
  if (!resultsMatch) return [];
  return resultsMatch[1].split("\n").filter((line) => line.startsWith("- ")).map((line) => line.slice(2).trim());
}
function parseNewFormatTests(body) {
  const testSections = body.split(/(?=## @test )/);
  const tests = [];
  for (const section of testSections) {
    const headerMatch = section.match(/^## @test\s+([\w-]+):\s*(.+)$/m);
    if (!headerMatch) continue;
    const [, id, title] = headerMatch;
    const scenarios = extractGherkinScenarios(section);
    tests.push({
      id,
      title: title.trim(),
      metadata: parseTestMetadata(section),
      scenario: scenarios.en || scenarios.es || "",
      scenarios,
      expectedResults: extractExpectedResults(section)
    });
  }
  return tests;
}
function parseLegacyFormatTests(body) {
  const tests = [];
  const testSections = body.split(/(?=## (?:\d+\.|TC-|TEST-))/i);
  for (const section of testSections) {
    const headerMatch = section.match(/^## (?:(\d+)\.\s*)?(?:(TC-[\w-]+|TEST-[\w-]+)[:\s]*)?\s*(.+)$/m);
    if (!headerMatch) continue;
    const [, num, explicitId, title] = headerMatch;
    const id = explicitId || `TC-${String(num || tests.length + 1).padStart(3, "0")}`;
    const scenarios = extractGherkinScenarios(section);
    const scenario = scenarios.en || scenarios.es || "";
    const metadata = {};
    if (section.toLowerCase().includes("smoke")) metadata.type = "smoke";
    if (section.toLowerCase().includes("critical") || section.toLowerCase().includes("high"))
      metadata.priority = "high";
    if (scenario) {
      tests.push({
        id,
        title: title.trim(),
        metadata,
        scenario,
        scenarios
      });
    }
  }
  return tests;
}
function parseBDDDocument(content) {
  const { frontmatter, body } = parseFrontmatter(content);
  const feature = parseFeature(frontmatter, body);
  const isNewFormat = body.includes("## @test ");
  const tests = isNewFormat ? parseNewFormatTests(body) : parseLegacyFormatTests(body);
  if (!feature.coverage && tests.length > 0) {
    feature.coverage = tests.length;
  }
  return {
    feature,
    tests,
    rawContent: content
  };
}
export {
  parseBDDDocument
};
