var r=r||{};H5P.ContentUpgradeProcess=function(n){function ContentUpgradeProcess(r,n,e,o,t,a,i){try{if(!((o=JSON.parse(o))instanceof Object))throw!0}catch(r){return i({type:"errorParamsBroken",id:t})}this.loadLibrary=a,this.upgrade(r,n,e,o.params,o.metadata,(function(r,n,e){if(r)return r.id=t,i(r);i(null,JSON.stringify({params:n,metadata:e}))}))}ContentUpgradeProcess.prototype.upgrade=function(r,n,e,o,t,a){var i=this;i.loadLibrary(r,e,(function(r,s){return r?a(r):null===s.semantics?a({type:"libraryMissing",library:s.name+" "+s.version.major+"."+s.version.minor}):void i.processParams(s,n,e,o,t,(function(r,n,e){if(r)return a(r);asyncSerial(s.semantics,(function(r,e,o){i.processField(e,n[e.name],(function(r,t){t&&(n[e.name]=t),o(r)}))}),(function(r){a(r,n,e)}))}))}))},ContentUpgradeProcess.prototype.processParams=function(n,e,o,t,a,i){if(void 0===r[n.name])return n.upgradesScript?i({type:"scriptMissing",library:n.name+" "+o}):i(null,t);asyncSerial(r[n.name],(function(r,n,s){r<e.major||r>o.major?s():asyncSerial(n,(function(r,n,s){if((r=+r)<=e.minor||r>o.minor)s();else{var c=void 0!==n.contentUpgrade?n.contentUpgrade:n;try{c(t,(function(r,n,e){t=n,e&&e.metadata&&(a=e.metadata),s(r)}),{metadata:a})}catch(r){console&&console.error&&(console.error("Error",r.stack),console.error("Error",r.name),console.error("Error",r.message)),i(r)}}}),s)}),(function(r){i(r,t,a)}))},ContentUpgradeProcess.prototype.processField=function(r,e,o){var t=this;if(void 0===e)return o();switch(r.type){case"library":if(void 0===e.library||void 0===e.params)return o();for(var a=e.library.split(" ",2),i=0;i<r.options.length;i++){var s="string"==typeof r.options[i]?r.options[i].split(" ",2):r.options[i].name.split(" ",2);if(s[0]===a[0]){if(s[1]===a[1])return o();var c=new n(a[1]),p=new n(s[1]);return c.major>p.major||c.major===p.major&&c.minor>=p.minor?o({type:"errorTooHighVersion",used:a[0]+" "+c,supported:s[0]+" "+p}):t.upgrade(s[0],c,p,e.params,e.metadata,(function(r,n,t){r||(e.library=s[0]+" "+p.major+"."+p.minor,e.params=n,t&&(e.metadata=t)),o(r,e)}))}}o({type:"errorNotSupported",used:a[0]+" "+c});break;case"group":1===r.fields.length&&!0!==r.isSubContent?t.processField(r.fields[0],e,(function(r,n){n&&(e=n),o(r,e)})):asyncSerial(r.fields,(function(r,n,o){var a=e?e[n.name]:null;t.processField(n,a,(function(r,t){t&&(e[n.name]=t),o(r)}))}),(function(r){o(r,e)}));break;case"list":asyncSerial(e,(function(n,o,a){t.processField(r.field,o,(function(r,o){o&&(e[n]=o),a(r)}))}),(function(r){o(r,e)}));break;default:o()}};var asyncSerial=function(r,n,e){var o,t=r instanceof Array;if(!t){var a=[];for(o in r)r.hasOwnProperty(o)&&a.push(o)}var i=-1,check=function(s){setTimeout((function(){++i===(t?r.length:a.length)||null!=s?e(s):(o=t?i:a[i],n(o,r[o],check))}),0)};check()};return ContentUpgradeProcess}(H5P.Version);