import ngModule from '../module';

/**
 * Calls a passed function if is the last element from an ng-repeat.
 *
 * @attribute {String} onLast - Callback function
 * @return {Object} The directive
 */
directive.$inject = ['$parse'];
export function directive($parse) {
    return {
        restrict: 'A',
        link: function($scope, $element, $attrs) {
            if ($scope.$last && $attrs.onLast) {
                let fn = $parse($attrs.onLast);
                setTimeout(() => {
                    fn($scope);
                });
            }
        }
    };
}
ngModule.directive('vnRepeatLast', directive);