/*
Copyright 2015, 2016 OpenMarket Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

module.exports = {
    /**
     * Given a room object, return the alias we should use for it,
     * if any. This could be the canonical alias if one exists, otherwise
     * an alias selected arbitrarily but deterministically from the list
     * of aliases. Otherwise return null;
     */
    getDisplayAliasForRoom: function(room) {
        return room.getCanonicalAlias() || room.getAliases()[0];
    },

    /**
     * Given a list of room objects, return the room which has the given alias,
     * else null.
     */
    getRoomForAlias: function(rooms, room_alias) {
        var room;
        for (var i = 0; i < rooms.length; i++) {
            var aliasEvents = rooms[i].currentState.getStateEvents(
                "m.room.aliases"
            );
            for (var j = 0; j < aliasEvents.length; j++) {
                var aliases = aliasEvents[j].getContent().aliases || [];
                for (var k = 0; k < aliases.length; k++) {
                    if (aliases[k] === room_alias) {
                        room = rooms[i];
                        break;
                    }
                }
                if (room) { break; }
            }
            if (room) { break; }
        }
        return room || null;
    }
}


@@ -972,842 +972,8 @@
   %7D,
-%0A%0A    /**%0A     * Given a list of room objects, return the room which has the given alias,%0A     * else null.%0A     */%0A    getRoomForAlias: function(rooms, room_alias) %7B%0A        var room;%0A        for (var i = 0; i %3C rooms.length; i++) %7B%0A            var aliasEvents = rooms%5Bi%5D.currentState.getStateEvents(%0A                %22m.room.aliases%22%0A            );%0A            for (var j = 0; j %3C aliasEvents.length; j++) %7B%0A                var aliases = aliasEvents%5Bj%5D.getContent().aliases %7C%7C %5B%5D;%0A                for (var k = 0; k %3C aliases.length; k++) %7B%0A                    if (aliases%5Bk%5D === room_alias) %7B%0A                        room = rooms%5Bi%5D;%0A                        break;%0A                    %7D%0A                %7D%0A                if (room) %7B break; %7D%0A            %7D%0A            if (room) %7B break; %7D%0A        %7D%0A        return room %7C%7C null;%0A    %7D
 %0A%7D%0A%0A

Remove now unused MatrixTools.GetRoomForAlias