(function () {
    global.dashModule
        .controller('shopPackageSearchCtrl', ['$scope', shopPackageSearchCtrl])
        .directive('shopPackageSearch', [shopPackageSearchDirective]);

    function shopPackageSearchDirective() {
        return {
            restrict: 'E',
            templateUrl: 'shop/html/shopPackageSearchTemplate'
        };
    }

    function shopPackageSearchCtrl($scope) {
        $scope.searchString = global.getParameterByName('search') || '';
        $scope.searchInPage = searchInPage;
        $scope.clearSearch = clearSearch;
        $scope.updateUrl = updateUrl;

        function updateUrl(){
            const url = $scope.searchString ? '?search=' + $scope.searchString : '?';
            history.pushState({
                search: $scope.searchString
            }, null, url);
        }

        function searchInPage(event){

            if((event.type && event.type === 'click') || (event.which && event.which === 13)){
                window.location = 'shop?search=' + $scope.searchString;
            }
        }

        function clearSearch(){
            $scope.searchInPage = '';
            window.location = 'shop';
        }
    }
})();
