import { Feature } from '@preply/ds-core-types';

interface IconFilter {
    terms?: string;
}

export class IconsContext {
    private icons?: Feature[];
    private error?: Error;

    constructor(icons?: Feature[], error?: Error) {
        this.icons = icons;
        this.error = error;
    }

    getError(): Error | undefined {
        return this.error;
    }

    searchIcons(filter: IconFilter): Feature[] {
        if (!this.icons) {
            return [];
        }
        const searchTerm = filter.terms ? filter.terms.toLowerCase() : '';
        return this.icons.filter(c => {
            return (
                c.name.toLowerCase().match(searchTerm) ||
                c.description?.toLowerCase().match(searchTerm)
            );
        });
    }

    static getIconPath(icon: Feature): string {
        return `${icon.path}/${icon.source}`;
    }
}
