// src/presentation/features/hooks/useFeatureFilters.ts
import { useState, useMemo, useCallback } from "react";
"use client";
var defaultFilters = {
  search: "",
  tags: [],
  stability: [],
  domain: []
};
function useFeatureFilters(features) {
  const [filters, setFilters] = useState(defaultFilters);
  const setSearch = useCallback((search) => {
    setFilters((prev) => ({ ...prev, search }));
  }, []);
  const toggleTag = useCallback((tag) => {
    setFilters((prev) => ({
      ...prev,
      tags: prev.tags.includes(tag) ? prev.tags.filter((t) => t !== tag) : [...prev.tags, tag]
    }));
  }, []);
  const toggleStability = useCallback((stability) => {
    setFilters((prev) => ({
      ...prev,
      stability: prev.stability.includes(stability) ? prev.stability.filter((s) => s !== stability) : [...prev.stability, stability]
    }));
  }, []);
  const toggleDomain = useCallback((domain) => {
    setFilters((prev) => ({
      ...prev,
      domain: prev.domain.includes(domain) ? prev.domain.filter((d) => d !== domain) : [...prev.domain, domain]
    }));
  }, []);
  const clearFilters = useCallback(() => setFilters(defaultFilters), []);
  const hasActiveFilters = useMemo(() => {
    return filters.search.length > 0 || filters.tags.length > 0 || filters.stability.length > 0 || filters.domain.length > 0;
  }, [filters]);
  const filteredFeatures = useMemo(() => {
    return features.filter((feature) => {
      if (filters.search) {
        const searchLower = filters.search.toLowerCase();
        const title = feature.meta.title ?? "";
        const matches = title.toLowerCase().includes(searchLower) || feature.meta.description?.toLowerCase().includes(searchLower) || feature.meta.key.toLowerCase().includes(searchLower);
        if (!matches)
          return false;
      }
      if (filters.tags.length > 0) {
        const hasTag = filters.tags.some((tag) => feature.meta.tags?.includes(tag));
        if (!hasTag)
          return false;
      }
      if (filters.stability.length > 0) {
        if (!filters.stability.includes(feature.meta.stability ?? "unknown"))
          return false;
      }
      if (filters.domain.length > 0) {
        if (!filters.domain.includes(feature.meta.domain ?? "unknown"))
          return false;
      }
      return true;
    });
  }, [features, filters]);
  return {
    filters,
    setSearch,
    toggleTag,
    toggleStability,
    toggleDomain,
    clearFilters,
    filteredFeatures,
    hasActiveFilters
  };
}
export {
  useFeatureFilters
};
