import { extractJsonLd } from "../jsonld.js";
const MIN_CONTENT_THRESHOLD = 100;
async function extractRecipe(html, _url) {
  if (!html) return null;
  let blocks;
  try {
    blocks = extractJsonLd(html);
  } catch {
    return null;
  }
  const recipe = blocks.find((block) => typeIncludes(block["@type"], "recipe"));
  if (!recipe) return null;
  const name = stringField(recipe["name"]);
  if (!name) return null;
  const lines = [`# ${name}`];
  const description = stringField(recipe["description"]);
  if (description) {
    lines.push("", description);
  }
  const metaLines = [];
  const totalTime = stringField(recipe["totalTime"]);
  if (totalTime) metaLines.push(`**Total time:** ${totalTime}`);
  const prepTime = stringField(recipe["prepTime"]);
  if (prepTime) metaLines.push(`**Prep time:** ${prepTime}`);
  const cookTime = stringField(recipe["cookTime"]);
  if (cookTime) metaLines.push(`**Cook time:** ${cookTime}`);
  const recipeYield = stringField(recipe["recipeYield"]);
  if (recipeYield) metaLines.push(`**Yield:** ${recipeYield}`);
  const cuisine = stringField(recipe["recipeCuisine"]);
  if (cuisine) metaLines.push(`**Cuisine:** ${cuisine}`);
  if (metaLines.length > 0) {
    lines.push("", ...metaLines);
  }
  const ingredients = stringArray(recipe["recipeIngredient"]);
  if (ingredients.length > 0) {
    lines.push("", "## Ingredients");
    for (const ingredient of ingredients) {
      lines.push(`- ${ingredient}`);
    }
  }
  const instructions = readInstructions(recipe["recipeInstructions"]);
  if (instructions.length > 0) {
    lines.push("", "## Instructions");
    instructions.forEach((step, idx) => {
      lines.push(`${idx + 1}. ${step}`);
    });
  }
  const markdown = lines.join("\n").trim();
  if (markdown.length < MIN_CONTENT_THRESHOLD) return null;
  const author = readAuthor(recipe["author"]);
  const datePublished = stringField(recipe["datePublished"]);
  return {
    title: name,
    markdown,
    metadata: {
      ...description ? { description } : {},
      ...author ? { author } : {},
      ...datePublished ? { date: datePublished } : {}
    },
    links: [],
    images: [],
    extractor: "site-specific"
  };
}
function typeIncludes(raw, want) {
  const target = want.toLowerCase();
  if (typeof raw === "string") return normalizeType(raw) === target;
  if (Array.isArray(raw)) {
    return raw.some((entry) => typeof entry === "string" && normalizeType(entry) === target);
  }
  return false;
}
function normalizeType(raw) {
  const tail = raw.split(/[/#:]/).pop() ?? raw;
  return tail.toLowerCase();
}
function stringField(value) {
  if (typeof value !== "string") return void 0;
  const trimmed = value.trim();
  return trimmed.length > 0 ? trimmed : void 0;
}
function stringArray(value) {
  if (!Array.isArray(value)) return [];
  const out = [];
  for (const entry of value) {
    if (typeof entry === "string") {
      const trimmed = entry.trim();
      if (trimmed) out.push(trimmed);
    }
  }
  return out;
}
function readInstructions(value) {
  if (!Array.isArray(value)) {
    if (typeof value === "string") {
      const trimmed = value.trim();
      return trimmed ? [trimmed] : [];
    }
    return [];
  }
  const out = [];
  for (const entry of value) {
    if (typeof entry === "string") {
      const trimmed = entry.trim();
      if (trimmed) out.push(trimmed);
      continue;
    }
    if (entry && typeof entry === "object") {
      const text = entry["text"];
      if (typeof text === "string") {
        const trimmed = text.trim();
        if (trimmed) out.push(trimmed);
      }
    }
  }
  return out;
}
function readAuthor(value) {
  if (typeof value === "string") return stringField(value);
  if (Array.isArray(value)) {
    for (const entry of value) {
      const name = readAuthor(entry);
      if (name) return name;
    }
    return void 0;
  }
  if (value && typeof value === "object") {
    const name = value["name"];
    if (typeof name === "string") return stringField(name);
  }
  return void 0;
}
export {
  extractRecipe
};
//# sourceMappingURL=recipe.js.map