import { parseHTML } from "linkedom";
import { extractJsonLd } from "../../jsonld.js";
async function extractProductSchema(html, _url) {
  if (!html) return null;
  const fromJsonLd = tryJsonLd(html);
  if (fromJsonLd) return fromJsonLd;
  return tryOpenGraph(html);
}
function tryJsonLd(html) {
  let blocks;
  try {
    blocks = extractJsonLd(html);
  } catch {
    return null;
  }
  const product = blocks.find((block) => typeIncludes(block["@type"], "product"));
  if (!product) return null;
  const name = stringField(product["name"]);
  if (!name) return null;
  const data = { name };
  const description = stringField(product["description"]);
  if (description) data.description = description;
  const brand = readBrand(product["brand"]);
  if (brand) data.brand = brand;
  const offer = pickOffer(product["offers"]);
  if (offer) {
    const amount = readNumber(offer["price"]);
    const currency = stringField(offer["priceCurrency"]);
    if (amount !== void 0 && currency) {
      data.price = { amount, currency };
    }
  }
  const sku = stringField(product["sku"]);
  if (sku) data.sku = sku;
  const rating = readRating(product["aggregateRating"]);
  if (rating) data.rating = rating;
  const image = firstImage(product["image"]);
  if (image) data.image = image;
  return data;
}
function tryOpenGraph(html) {
  let document;
  try {
    ({ document } = parseHTML(html));
  } catch {
    return null;
  }
  const name = metaContent(document, 'meta[property="og:title"]');
  if (!name) return null;
  const data = { name };
  const description = metaContent(document, 'meta[property="og:description"]');
  if (description) data.description = description;
  const image = metaContent(document, 'meta[property="og:image"]');
  if (image) data.image = image;
  const priceAmt = metaContent(document, 'meta[property="product:price:amount"]');
  const priceCur = metaContent(document, 'meta[property="product:price:currency"]');
  if (priceAmt && priceCur) {
    const amount = Number.parseFloat(priceAmt);
    if (Number.isFinite(amount)) {
      data.price = { amount, currency: priceCur };
    }
  }
  return data;
}
function metaContent(document, selector) {
  const el = document.querySelector(selector);
  const content = el?.getAttribute("content")?.trim();
  return content && content.length > 0 ? content : void 0;
}
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") {
    const trimmed = value.trim();
    return trimmed.length > 0 ? trimmed : void 0;
  }
  if (typeof value === "number" && Number.isFinite(value)) {
    return String(value);
  }
  return void 0;
}
function readNumber(value) {
  if (typeof value === "number" && Number.isFinite(value)) return value;
  if (typeof value === "string") {
    const parsed = Number.parseFloat(value);
    if (Number.isFinite(parsed)) return parsed;
  }
  return void 0;
}
function readBrand(value) {
  if (typeof value === "string") return stringField(value);
  if (Array.isArray(value)) {
    for (const entry of value) {
      const name = readBrand(entry);
      if (name) return name;
    }
    return void 0;
  }
  if (value && typeof value === "object") {
    const name = value["name"];
    return stringField(name);
  }
  return void 0;
}
function pickOffer(value) {
  if (Array.isArray(value)) {
    const first = value.find((entry) => entry && typeof entry === "object");
    return first;
  }
  if (value && typeof value === "object") return value;
  return void 0;
}
function readRating(value) {
  if (!value || typeof value !== "object") return void 0;
  const obj = value;
  const ratingValue = readNumber(obj["ratingValue"]);
  const reviewCount = readNumber(obj["reviewCount"]) ?? readNumber(obj["ratingCount"]);
  if (ratingValue === void 0) return void 0;
  return { value: ratingValue, count: reviewCount ?? 0 };
}
function firstImage(value) {
  if (typeof value === "string") return stringField(value);
  if (Array.isArray(value)) {
    for (const entry of value) {
      const img = firstImage(entry);
      if (img) return img;
    }
    return void 0;
  }
  if (value && typeof value === "object") {
    const url = value["url"];
    return stringField(url);
  }
  return void 0;
}
export {
  extractProductSchema
};
//# sourceMappingURL=Product.js.map