function getDomain(url) {
  try {
    return new URL(url).hostname.toLowerCase();
  } catch {
    return "";
  }
}
function normalizeDomain(domain) {
  return domain.replace(/\/+$/, "").toLowerCase();
}
function domainMatches(hostname, domain) {
  const normalized = normalizeDomain(domain);
  return hostname === normalized || hostname.endsWith("." + normalized);
}
function filterByDomains(results, includeDomains, excludeDomains) {
  if (!includeDomains?.length && !excludeDomains?.length) return results;
  return results.filter((r) => {
    const hostname = getDomain(r.url);
    if (!hostname) {
      return !includeDomains?.length;
    }
    if (includeDomains?.length) {
      if (!includeDomains.some((d) => domainMatches(hostname, d))) return false;
    }
    if (excludeDomains?.length) {
      if (excludeDomains.some((d) => domainMatches(hostname, d))) return false;
    }
    return true;
  });
}
function isValidIsoDate(dateStr) {
  const parsed = new Date(dateStr);
  return !isNaN(parsed.getTime()) && /^\d{4}-\d{2}-\d{2}$/.test(dateStr);
}
function filterByDateRange(results, fromDate, toDate) {
  if (!fromDate && !toDate) return results;
  if (fromDate && !isValidIsoDate(fromDate)) return results;
  if (toDate && !isValidIsoDate(toDate)) return results;
  if (fromDate && toDate && new Date(fromDate) > new Date(toDate)) return results;
  const fromMs = fromDate ? new Date(fromDate).getTime() : null;
  const toMs = toDate ? new Date(toDate).getTime() + 24 * 3600 * 1e3 - 1 : null;
  return results.filter((r) => {
    const published = r.published_date;
    if (typeof published !== "string" || !published) return true;
    const t = Date.parse(published);
    if (isNaN(t)) return true;
    if (fromMs !== null && t < fromMs) return false;
    if (toMs !== null && t > toMs) return false;
    return true;
  });
}
function filterByCategory(results, _category) {
  return results;
}
function applyAllFilters(results, options) {
  let filtered = filterByDomains(results, options.includeDomains, options.excludeDomains);
  filtered = filterByDateRange(filtered, options.fromDate, options.toDate);
  filtered = filterByCategory(filtered, options.category);
  return filtered;
}
export {
  applyAllFilters,
  filterByCategory,
  filterByDateRange,
  filterByDomains
};
//# sourceMappingURL=filters.js.map