var angular = require('angular');
require('../modules');

var module = angular.module('flowmodulebase.filters');

module.config(function($filterProvider) {
  $filterProvider.register('writeuseridfilter', function() {
    var splitRegexp = /\|\$\|/;
    return function(text) {
      if (text && text.indexOf('*') >= 0) {
        var userDepts = text.split(splitRegexp);
        var userDept = userDepts[userDepts.length - 2];
        return userDept? userDept.split('*')[1] : '';
      } else {
        return text || '';
      }
    };
  });

  $filterProvider.register('prewriteidfilter', function() {
    var splitRegexp = /\|\$\|/;
    return function(text) {
      if (text && text.workflow && text.workflow.preMailUserId) {
        text = text.workflow.preMailUserId;
      } else if(text && text.flow && text.flow.preMailUserId) {
        text = text.flow.preMailUserId;
      } else {
        //text = text.userList;
        text = false;
      }
      if (text) {
        var userDepts = text.split(splitRegexp);
        var userDept = userDepts[userDepts.length -2];
        return userDept? userDept.split('*')[1] : '';
      } else {
        return '';
      }
    };
  });
});
