'use strict';

angular.module('qusion.ui.alias', [
])
    .directive('qnAlias', function() {
        return {
            require: 'ngModel',
            scope: {
                string: '=qnAlias'
            },
            link: function(scope, elem) {

                scope.$watch('string', function(value) {
                    if (value != undefined) {

                       var alias = value.replace(/[^a-zA-Z0-9čćšđžČĆŠĐŽ\-]/g, "-")
                            .replace(/-{2,}/g, "-")
                            .replace(/[čćČĆ]/g, "c")
                            .replace(/[žŽ]/g, "z")
                            .replace(/[đĐ]/g, "d")
                            .replace(/[šŠ]/g, "s")
                            .replace(/^-/g, "")
                            .replace(/-$/g, "")
                            .toLowerCase();
                        elem.val(alias);

                    }
                });
            }
        };
    });