import { useMemo, useState } from "react";
import { AttributeSchema } from "@/lib/client";

type Props = {
    attributes: AttributeSchema[];
};

const useFilteredAttributes = ({ attributes }: Props) => {
    const [search, setSearch] = useState<string>("");

    const filteredAttributes = useMemo(
        () =>
            attributes.filter(
                attr =>
                    attr.type.name.includes(search) ||
                    attr.label.includes(search) ||
                    attr.description.includes(search)
            ),
        [attributes, search]
    );

    return {
        search,
        setSearch,
        filteredAttributes,
    };
};

export default useFilteredAttributes;
