import {
	get,
	Route
} from "Ember";
import InfiniteScrollMixin from "mixins/InfiniteScrollMixin";
import RefreshRouteMixin from "mixins/RefreshRouteMixin";
import { mapBy } from "utils/ember/recordArrayMethods";
import preload from "utils/preload";


function filterMatches( filter, value ) {
	return filter === "all" || filter === value;
}


export default Route.extend( InfiniteScrollMixin, RefreshRouteMixin, {
	contentPath: "controller.model.streams",

	itemSelector: ".stream-item-component",

	queryParams: {
		filter: {
			refreshModel: true,
			replace: true
		},
		query: {
			refreshModel: true,
			replace: true
		}
	},

	model( params ) {
		let store = get( this, "store" );

		return Promise.all([
			// search for games
			filterMatches( params.filter, "games" )
				? store.query( "twitchSearchGame", {
					query: params.query,
					type : "suggest",
					live : true
				})
					.then( mapBy( "game" ) )
					.then( preload( "box.largeLatest" ) )
				: Promise.resolve( [] ),

			// search for channels
			filterMatches( params.filter, "channels" )
				? store.query( "twitchSearchChannel", {
					query : params.query,
					offset: 0,
					limit : 10
				})
					.then( mapBy( "channel" ) )
					.then( preload( "logo" ) )
				: Promise.resolve( [] ),

			// search for streams
			filterMatches( params.filter, "streams" )
				? store.query( "twitchSearchStream", {
					query : params.query,
					offset: get( this, "offset" ),
					limit : get( this, "limit" )
				})
					.then( mapBy( "stream" ) )
					.then( preload( "preview.mediumLatest" ) )
				: Promise.resolve( [] )
		])
			.then( ([ games, channels, streams ]) => ({ games, channels, streams }) );
	},

	fetchContent() {
		if ( !filterMatches( get( this, "filter" ), "streams" ) ) {
			return Promise.resolve( [] );
		}

		return get( this, "store" ).query( "twitchSearchStream", {
			query : get( this, "query" ),
			offset: get( this, "offset" ),
			limit : get( this, "limit" )
		})
			.then( mapBy( "stream" ) )
			.then( preload( "preview.mediumLatest" ) );
	}
});

@@ -163,15 +163,26 @@
 rt %7B
- 
+%0A%09toArray,%0A%09
 mapBy
- 
+%0A
 %7D fr
@@ -913,22 +913,16 @@
 en( 
-mapBy( %22game%22 
+toArray(
 ) )%0A
@@ -943,16 +943,21 @@
 eload( %22
+game.
 box.larg

Fix GameItemComponents in SearchRoute being empty