import { ExtendedToken } from '../types';

export const filterTokens = (
    tokens: ExtendedToken[],
    terms: string | undefined,
    withColorScheme: boolean,
): ExtendedToken[] => {
    return tokens.filter(token => {
        if (terms === '-' || terms === '--') {
            return true;
        }
        if (terms?.startsWith('--')) {
            return token.id.toLowerCase().startsWith(terms.substring(2).toLowerCase());
        }
        if (terms) {
            const nameMatch = token.name.toLowerCase().indexOf(terms.toLowerCase()) > -1;
            const valueMatch = token.value.toLowerCase().indexOf(terms.toLowerCase()) > -1;
            return (
                (nameMatch || valueMatch) &&
                (withColorScheme ? token.colorSchemeAware : !token.colorSchemeAware)
            );
        }
        return withColorScheme ? token.colorSchemeAware : !token.colorSchemeAware;
    });
};
