interface HasName {
	name: string;
}

export function filterAssets<T extends HasName>(
	assets: T[],
	searchTerm: string
): {
	item: T;
	index: number;
}[] {
	searchTerm = searchTerm.toLowerCase();

	const included = assets
		.map((item) => {
			const assetName = item.name.toLowerCase();
			const isInclude = assetName.includes(searchTerm);
			const index = assetName.indexOf(searchTerm);

			return {
				item,
				isInclude,
				index,
			};
		})
		.filter((x) => x.isInclude);

	return included.sort((a, b) =>
		a.item.name.toLowerCase().startsWith(searchTerm)
			? -1
			: b.item.name.toLowerCase().startsWith(searchTerm)
			? 1
			: 0
	);
}
