"use strict";exports.__esModule = true;function _interopRequireDefault(obj){return obj && obj.__esModule?obj:{"default":obj};}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _inherits(subClass,superClass){if(typeof superClass !== "function" && superClass !== null){throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__ = superClass;}var _Channel2=require("./Channel");var _Channel3=_interopRequireDefault(_Channel2);var _UtilCache=require("../Util/Cache");var _UtilCache2=_interopRequireDefault(_UtilCache);var _PermissionOverwrite=require("./PermissionOverwrite");var _PermissionOverwrite2=_interopRequireDefault(_PermissionOverwrite);var _ChannelPermissions=require("./ChannelPermissions");var _ChannelPermissions2=_interopRequireDefault(_ChannelPermissions);var _UtilArgumentRegulariser=require("../Util/ArgumentRegulariser");var ServerChannel=(function(_Channel){_inherits(ServerChannel,_Channel);function ServerChannel(data,client,server){var _this=this;_classCallCheck(this,ServerChannel);_Channel.call(this,data,client);this.name = data.name;this.type = data.type;this.position = data.position;this.permissionOverwrites = data.permissionOverwrites || new _UtilCache2["default"]();this.server = server;if(!data.permissionOverwrites){data.permission_overwrites.forEach(function(permission){_this.permissionOverwrites.add(new _PermissionOverwrite2["default"](permission));});}}ServerChannel.prototype.permissionsOf = function permissionsOf(userOrRole){userOrRole = this.client.internal.resolver.resolveUser(userOrRole);if(userOrRole){if(this.server.ownerID === userOrRole.id){return new _ChannelPermissions2["default"](4294967295);}var everyoneRole=this.server.roles.get("id",this.server.id);var userRoles=[everyoneRole].concat(this.server.rolesOf(userOrRole) || []);var userRolesID=userRoles.filter(function(v){return !!v;}).map(function(v){return v.id;});var roleOverwrites=[],memberOverwrites=[];this.permissionOverwrites.forEach(function(overwrite){if(overwrite.type === "member" && overwrite.id === userOrRole.id){memberOverwrites.push(overwrite);}else if(overwrite.type === "role" && ~userRolesID.indexOf(overwrite.id)){roleOverwrites.push(overwrite);}});var permissions=0;for(var _iterator=userRoles,_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[Symbol.iterator]();;) {var _ref;if(_isArray){if(_i >= _iterator.length)break;_ref = _iterator[_i++];}else {_i = _iterator.next();if(_i.done)break;_ref = _i.value;}var serverRole=_ref;if(serverRole){permissions |= serverRole.permissions;}}for(var _iterator2=roleOverwrites.concat(memberOverwrites),_isArray2=Array.isArray(_iterator2),_i2=0,_iterator2=_isArray2?_iterator2:_iterator2[Symbol.iterator]();;) {var _ref2;if(_isArray2){if(_i2 >= _iterator2.length)break;_ref2 = _iterator2[_i2++];}else {_i2 = _iterator2.next();if(_i2.done)break;_ref2 = _i2.value;}var overwrite=_ref2;if(overwrite){permissions = permissions & ~overwrite.deny;permissions = permissions | overwrite.allow;}}return new _ChannelPermissions2["default"](permissions);}else {userOrRole = this.client.internal.resolver.resolveRole(userOrRole);if(userOrRole){var permissions=this.server.roles.get("id",this.server.id).permissions | userOrRole.permissions;var overwrite=this.permissionOverwrites.get("id",this.server.id);permissions = permissions & ~overwrite.deny | overwrite.allow;overwrite = this.permissionOverwrites.get("id",userOrRole.id);if(overwrite){permissions = permissions & ~overwrite.deny | overwrite.allow;}return new _ChannelPermissions2["default"](permissions);}else {return null;}}};ServerChannel.prototype.permsOf = function permsOf(user){return this.permissionsOf(user);};ServerChannel.prototype.mention = function mention(){return "<#" + this.id + ">";};ServerChannel.prototype.toString = function toString(){return this.mention();};ServerChannel.prototype.setName = function setName(){return this.client.setChannelName.apply(this.client,_UtilArgumentRegulariser.reg(this,arguments));};ServerChannel.prototype.setPosition = function setPosition(){return this.client.setChannelPosition.apply(this.client,_UtilArgumentRegulariser.reg(this,arguments));};ServerChannel.prototype.update = function update(){return this.client.updateChannel.apply(this.client,_UtilArgumentRegulariser.reg(this,arguments));};return ServerChannel;})(_Channel3["default"]);exports["default"] = ServerChannel;module.exports = exports["default"];
