angular.module('productsSearchModule')

.controller('productsSearchController', ['$scope', 'productsSearchDatasource', 'productsDepartmentsDatasource', 'external', function ($scope, productsSearchDatasource, productsDepartmentsDatasource, external) {

    $scope.isSearchVisible;
    $scope.keyphrase;

    $scope.isBusy = function() { return productsSearchDatasource.status() !== 'ready'; };
    $scope.suggested = function() { return productsSearchDatasource.suggested(); };
    $scope.recent = function() { return productsSearchDatasource.recent(); };
    $scope.hasSuggested = function() { return productsSearchDatasource.hasSuggested(); };
    $scope.hasRecent = function() { return productsSearchDatasource.hasRecent(); };
    $scope.departmentsAll = function() { return productsDepartmentsDatasource.departmentsAll(); };
    $scope.canSearch = function() { return external().hasAccessToProductsPage !== '0'; };

    $scope.showSearch = function(keyphrase) {
        $scope.$evalAsync(function() {
            $scope.keyphrase = keyphrase;
            $scope.isSearchVisible = true;
            productsSearchDatasource.getSearchSuggestions(keyphrase);
        });
    };
    
    $scope.hideSearch = function() {
        $scope.$evalAsync(function() {
            $scope.keyphrase = null;
            $scope.isSearchVisible = false;
        });
    };

    productsDepartmentsDatasource.getAll();

}]);