interface FilterProps {
  assets: AssetProp[] | DecalProp[];
  searchValue: string | null;
  selectedCategory: string | null;
  selectedDecalCategory: string | null;
}

export const getFilteredAssets = ({
  assets,
  searchValue,
  selectedCategory,
  selectedDecalCategory,
}: FilterProps) => {
  const term = searchValue?.trim().toLowerCase();
  if (!term) return assets;

  if (selectedCategory === "Decals" || selectedDecalCategory) {
    return (assets as DecalProp[]).filter((a) =>
      a.decalName?.toLowerCase().includes(term)
    );
  }

  return (assets as AssetProp[]).filter((a) => {
    const tags = a.tags?.toLowerCase() ?? "";
    const filename = a.filename?.toLowerCase() ?? "";
    const category = a.category?.toLowerCase() ?? "";

    if (term.startsWith(":")) return tags.includes(term.slice(1));
    if (selectedCategory)
      return category.includes(selectedCategory.toLowerCase()) && filename.includes(term);

    return filename.includes(term);
  });
};
