angular.module("app").directive("tsq", function ($rootScope, $timeout, SQEvents, L, UniqueIdGenerator) {
    return {
        restrict: 'EA',
        link: function (scope, element, attrs) {
            var originalText = element[0] ? element[0].innerHTML : null;
            var id = UniqueIdGenerator.generateId();

            function updateContent(language) {
                element[0].innerHTML = L.tsq(originalText, language.title);
            }

            SQEvents.addListener("tsq-" + id, [SQEvents.get("LANGUAGE_CHANGED")], function (event, language) {
                updateContent(language);
            });

            scope.$on('$destroy', function () {
                UniqueIdGenerator.removeId(id);
            });

            updateContent($rootScope.selectedLanguage);
        }
    };
});