// Resolve user id for given object id (should be either user or business process id)

'use strict';

var businessProcessDerivedMap = require('./business-process-derived-map')
  , businessProcessUserMap    = require('./business-process-user-map')
  , userIds                   = require('./user-ids');

module.exports = function (id) {
	return userIds(function (set) {
		if (set.has(id)) return id;
		return businessProcessUserMap(function self(userMap) {
			if (userMap.has('7' + id)) return userMap.get('7' + id);
			return businessProcessDerivedMap(function (map) {
				while (map.has('7' + id)) {
					id = map.get('7' + id);
					if (userMap.has('7' + id)) return userMap.get('7' + id);
				}
				return null;
			});
		});
	});
};
