;'use strict';(function(exports){var Deferred=function(){this.promise=new Promise(function(resolve,reject){this.resolve=resolve;this.reject=reject;}.bind(this));return this;};var LayoutDictionary=function(list,dict){this.dbStore=list.dbStore;this.imEngineId=dict.imEngineId;this.filename=dict.filename;this.filePath=dict.filePath;this.fileSize=dict.fileSize;this.databaseId=dict.databaseId;this.installedLayoutIds=dict.installedLayoutIds;if(dict.preloaded){this.state=this.STATE_PRELOADED;}else if(this.installedLayoutIds.size!==0){this.state=this.STATE_INSTALLED;}else{this.state=this.STATE_INSTALLABLE;} this._downloader=null;this._installingLayoutDeferreds=null;this._installingPromise=null;this._removingPromise=null;};LayoutDictionary.prototype.state=0;LayoutDictionary.prototype.imEngineId='';LayoutDictionary.prototype.filename='';LayoutDictionary.prototype.filePath='';LayoutDictionary.prototype.fileSize=0;LayoutDictionary.prototype.databaseId=0;LayoutDictionary.prototype.installedLayoutIds=null;LayoutDictionary.prototype.downloadLoadedSize=0;LayoutDictionary.prototype.downloadTotalSize=0;LayoutDictionary.prototype.STATE_PRELOADED=1;LayoutDictionary.prototype.STATE_INSTALLABLE=2;LayoutDictionary.prototype.STATE_INSTALLING_CANCELLABLE=3;LayoutDictionary.prototype.STATE_INSTALLING=4;LayoutDictionary.prototype.STATE_INSTALLED=5;LayoutDictionary.prototype.STATE_REMOVING=6;LayoutDictionary.prototype.start=function(){this._installingLayoutDeferreds=new Map();};LayoutDictionary.prototype.stop=function(){this._downloader=null;this._installingLayoutDeferreds=null;this._installingPromise=null;this._removingPromise=null;};LayoutDictionary.prototype.installForLayout=function(layoutItem){var deferred;switch(this.state){case this.STATE_PRELOADED:throw new Error('LayoutDictionary: '+'Can\'t install a dictionary under this state.');case this.STATE_INSTALLABLE:this._startInstall();deferred=new Deferred();this._installingLayoutDeferreds.set(layoutItem,deferred);return deferred.promise;case this.STATE_INSTALLING_CANCELLABLE:case this.STATE_INSTALLING:if(this._installingLayoutDeferreds.has(layoutItem)){return this._installingLayoutDeferreds.get(layoutItem).promise;} deferred=new Deferred();this._installingLayoutDeferreds.set(layoutItem,deferred);return deferred.promise;case this.STATE_INSTALLED:this.installedLayoutIds.add(layoutItem.id);return Promise.resolve();case this.STATE_REMOVING:return this._removingPromise.then(this.installForLayout.bind(this,layoutItem),this.installForLayout.bind(this,layoutItem));default:throw new Error('LayoutDictionary: Invalid state.');}};LayoutDictionary.prototype.removeForLayout=function(layoutItem){switch(this.state){case this.STATE_PRELOADED:throw new Error('LayoutDictionary: '+'Can\'t remove a dictionary under this state.');case this.STATE_INSTALLABLE:return Promise.resolve();case this.STATE_INSTALLING_CANCELLABLE:if(!this._installingLayoutDeferreds.has(layoutItem)){return Promise.resolve();} var deferred=this._installingLayoutDeferreds.get(layoutItem);deferred.reject('Cancelled');this._installingLayoutDeferreds.delete(layoutItem);if(this._installingLayoutDeferreds.size>0){return deferred.promise.catch(function(){});} this._downloader.abort();return this._installingPromise.catch(function(){});case this.STATE_INSTALLING:if(!this._installingLayoutDeferreds.has(layoutItem)){return Promise.resolve();} deferred=this._installingLayoutDeferreds.get(layoutItem);deferred.reject('Cancelled');this._installingLayoutDeferreds.delete(layoutItem);if(this._installingLayoutDeferreds.size>0){return deferred.promise.catch(function(){});} return this._installingPromise.catch(function(){});case this.STATE_INSTALLED:if(!this.installedLayoutIds.has(layoutItem.id)){return Promise.resolve();} if(this.installedLayoutIds.size>1){this.installedLayoutIds.delete(layoutItem.id);return Promise.resolve();} this._startRemove();this._removingPromise=this._removingPromise.then(function(){this.installedLayoutIds.delete(layoutItem.id);}.bind(this));return this._removingPromise;case this.STATE_REMOVING:if(this.installedLayoutIds.has(layoutItem.id)){return this._removingPromise;} return Promise.resolve();default:throw new Error('LayoutDictionary: Invalid state.');}};LayoutDictionary.prototype._startInstall=function(){if(this.state!==this.STATE_INSTALLABLE){throw new Error('LayoutDictionary: '+'Can\'t start install a dictionary under this state.');} this.state=this.STATE_INSTALLING_CANCELLABLE;this.downloadLoadedSize=0;this.downloadTotalSize=this.fileSize;this._downloader=new LayoutDictionaryDownloader(this.imEngineId,this.filename);this._downloader.onprogress=this._updateProgress.bind(this);this._installingPromise=this._downloader.load().then(function(data){this._downloader=null;this.state=this.STATE_INSTALLING;return this.dbStore.setItem(this.databaseId,data);}.bind(this)).then(function(){this._installingPromise=null;this._installingLayoutDeferreds.forEach(function(deferred,layoutItem){this.installedLayoutIds.add(layoutItem.id);deferred.resolve();},this);this._installingLayoutDeferreds.clear();this.state=this.STATE_INSTALLED;this.downloadLoadedSize=0;this.downloadTotalSize=0;}.bind(this)).catch(function(e){console.warn('LayoutDictionary: Installation failed with error:',e);this._downloader=null;this._installingPromise=null;this._installingLayoutDeferreds.forEach(function(deferred){deferred.reject('Failed');},this);this._installingLayoutDeferreds.clear();this.state=this.STATE_INSTALLABLE;this.downloadLoadedSize=0;this.downloadTotalSize=0;}.bind(this)).then(function(){if(this.state===this.STATE_INSTALLED&&this._installingLayoutDeferreds.size===0&&this.installedLayoutIds.size===0){return this._startRemove();}}.bind(this));};LayoutDictionary.prototype._startRemove=function(){if(this.state!==this.STATE_INSTALLED){throw new Error('LayoutDictionary: '+'Can\'t start remove a dictionary under this state.');} this.state=this.STATE_REMOVING;this._removingPromise=this.dbStore.deleteItem(this.databaseId).then(function(){this._removingPromise=null;this.state=this.STATE_INSTALLABLE;}.bind(this)).catch(function(e){this._removingPromise=null;this.state=this.STATE_INSTALLED;throw e;}.bind(this));};LayoutDictionary.prototype._updateProgress=function(loaded,total){this.downloadLoadedSize=loaded;this.downloadTotalSize=total;Array.from(this._installingLayoutDeferreds).map(function(keyValue){return keyValue[0];}).forEach(function(layoutItem){layoutItem.updateInstallProgress(loaded,total);});};exports.LayoutDictionary=LayoutDictionary;}(window));