module.export(function SearchService() {

    function make(getSearch) {
        let searchTerm = new Rx.Subject()

        function search(term) {
            searchTerm.next(term)
        }

        function getResults() {
            return searchTerm
                .debounceTime(500)
                .distinctUntilChanged()
                .switchMap(term => {
                    if (term) return Rx.Observable.fromPromise(getSearch(term))
                    return Rx.Observable.of([])
                })
        }

        return {
            search: search,
            getResults: getResults
        }
    }

    return {
        make: make
    }
})