(function () {
    'use strict';

    /**
     * @param {$q} $q
     * @param {app.utils} utils
     * @param {Migration} migration
     * @param {State} state
     * @param {storageSelect} storageSelect
     */
    const factory = function ($q, utils, migration, state, storageSelect) {

        const usedStorage = storageSelect();

        const MIGRATION_MAP = {
            '1.0.41': function (storage) {
                return addNewGateway(storage, WavesApp.defaultAssets.BSV);
            },
            '1.2.1': function (storage) {
                return newTerms(storage);
            },
            '1.3.18': function (storage) {
                return addDefaultUserName(storage);
            }
        };

        function newTerms(storage) {
            return storage.load('userList').then((users = []) => {
                const needShowNewTerms = users.some((user) => {
                    const settings = user.settings || Object.create(null);
                    return typeof settings.termsAccepted === 'undefined';
                });
                if (needShowNewTerms) {
                    return storage.save('needReadNewTerms', true);
                }
            });
        }

        function addNewGateway(storage, gateway) {
            return storage.load('userList').then((users = []) => {
                users.forEach((user) => {
                    const settings = user.settings || Object.create(null);
                    const idList = settings.pinnedAssetIdList;
                    if (idList && !idList.includes(gateway)) {
                        idList.push(gateway);
                    }
                });

                return storage.save('userList', users);
            });
        }

        function addDefaultUserName(storage) {
            return storage.load('userList').then((users = []) => {
                let counter = 0;
                users.forEach((user) => {
                    if (!user.name) {
                        user.name = ++counter >= 2 ? `Account ${counter}` : 'Account';
                    }
                });

                return storage.save('userList', users);
            });
        }

        class Storage {

            constructor() {
                usedStorage.init();
                this._isNewDefer = $q.defer();

                this.load('lastVersion')
                    .then((version) => {
                        this.save('lastVersion', WavesApp.version);
                        state.lastOpenVersion = version;

                        if (version) {
                            const versions = migration.migrateFrom(version, Object.keys(MIGRATION_MAP));
                            return utils.chainCall(versions.map((version) => MIGRATION_MAP[version].bind(null, this)))
                                .then(() => {
                                    this._isNewDefer.resolve(version);
                                });
                        } else {
                            this._isNewDefer.resolve(version);
                            return Promise.resolve();
                        }
                    });
            }

            onReady() {
                return this._isNewDefer.promise;
            }

            save(key, value) {
                return utils.when(usedStorage.write(key, value));
            }

            load(key) {
                return utils.when(usedStorage.read(key));
            }

            clear() {
                return utils.when(usedStorage.clear());
            }

        }

        return new Storage();
    };

    factory.$inject = ['$q', 'utils', 'migration', 'state', 'storageSelect'];

    angular.module('app.utils')
        .factory('storage', factory);
})();

@@ -1841,32 +1841,191 @@
 sers = %5B%5D) =%3E %7B%0A
+%0A                users.forEach((user, i) =%3E %7B%0A%0A                    if (!user.name) %7B%0A                        user.name = 'Account';%0A                    %7D%0A%0A    
                 
@@ -2038,17 +2038,17 @@
 unter = 
-0
+1
 ;%0A      
@@ -2061,34 +2061,117 @@
     
+ 
-users.forEach((user
+   const restUsers = users.filter((user, j) =%3E j !== i);%0A                    const getIdx = (name
 ) =%3E %7B%0A
+    
     
@@ -2186,34 +2186,66 @@
         if (
-!user.name
+restUsers.some(user =%3E user.name === name)
 ) %7B%0A        
@@ -2252,32 +2252,53 @@
                 
+    return getIdx(%60$%7B
 user.name = ++co
@@ -2290,19 +2290,20 @@
 ser.name
- = 
+%7D $%7B
 ++counte
@@ -2307,48 +2307,283 @@
 nter
- %3E= 2 ? %60Account $%7Bcounter%7D%60 : 'Account'
+%7D%60);%0A                        %7D else %7B%0A                            return counter;%0A                        %7D%0A                    %7D;%0A%0A                    const idx = getIdx(user.name);%0A                    if (idx %3E 1) %7B%0A                        user.name = %60$%7Buser.name%7D $%7Bidx%7D%60
 ;%0A  

change migration map logic