(()=>{function e(c,e,l,n,o,t,r,s,p,a){var i,u=this,d={inProgress:!1,progress:0,label:null,deferred:null,cancelled:!1};function f(e){var t=c.defer();return d.deferred=t,d.cancelled&&t.reject({cancelled:!0}),c.when(e).then(t.resolve,t.reject),t.promise.catch(function(e){e=e.cancelled;e&&(d.cancelled=e)}),i=function(){_.has(e,"abort")&&e.abort(),d.cancelled=!0,t.reject({cancelled:!0})},t.promise}function g(e,t){return _.head(_.compact(_.at(e,t)))}function h(e){var t=c.resolve();return t=_.get(u.wizardCtrl.data.chosenMethod,"expectedType")!==e?c.reject({type:"typeValidation",message:a.getString(u.wizardCtrl.data.chosenMethod.invalidFileTypeMessage)}):t}function y(e){return v(e).then(function(e){return _.get(e,"type")||(_.get(e,"apiVersion")?"MICROSERVICE":"HOSTED")}).catch(function(){return"HOSTED"})}function v(e){return t.getJsonData(e,{filename:"cumulocity.json"})}function m(){d.cancelled=!0,i&&i()}_.assign(u,{processingStatus:d,createAppAndUploadArchive:function(e){var t;return d.inProgress=!0,d.progress=0,d.cancelled=!1,y(e).then(h).then(function(){return o=e,i={manifest:{}},d.label=p("Creating application"),y(o).then(function(t){return"HOSTED"===t?v(o).catch(function(){return{}}).then(function(e){return{appType:t,appModel:e}}):{appType:t}}).then(function(e){var t=e.appType,e=e.appModel,n=((e,t,n)=>(n=_.get(n,"name",e.name.replace(/.zip$/i,"")),n="MICROSERVICE"===t?n.replace(/-\d+\.\d+\.\d+(-SNAPSHOT)?$/,""):n))(o,t,e),r=n.replace(/[^a-zA-Z0-9-_]/g,""),a=((e,t)=>e=(e=_.get(e,"key"))||"".concat(_.kebabCase(t),"-key"))(e,r),e=_.get(e,"contextPath",r.toLowerCase());return _.assign(i,{type:t,name:n,key:a,contextPath:e})}).then(function(e){var t="MICROSERVICE"===e.type?n.save(e).then(l.getResData):n.trySave(e);return"MARKET"===e.availability&&"HOSTED"===e.type?c.all({newApp:t,tenant:s.current()}).then(function(e){var t=e.newApp;return s.addApplication({id:e.tenant.name},t),t}):t}).finally(function(){d.progress+=10});var o,i}).then(function(e){t=e}).then(function(){return f("MICROSERVICE"===t.type?r({title:p("Subscribe to microservice"),size:"sm",labels:{ok:p("Subscribe"),cancel:p("Don't subscribe")},body:p("You are about to subscribe to the microservice after upload. Do you want to subscribe to it?"),status:"info"}).then(function(){return!0},function(){return!1}).then(function(e){u.activateAfterUpdate=e}):c.resolve())}).then(function(){return f(((e,t)=>(d.label=p("Uploading file"),(t=n.uploadArchive(e,t,{silentError:!0})).progress(function(e){d.progress=10+e.loaded/e.total*90}).then(l.getResData).then(_.partial(n.setActiveArchive,e)).then(function(){return e}),t))(t,e))}).then(function(){return f((e=>{var t=c.resolve();return"MICROSERVICE"===(u.wizardCtrl.data.createdApp=e).type?t=((r,a)=>s.current(!0).then(function(e){var t=e.applications.references.some(function(e){return e.application.id===a.id}),n=c.resolve();return!t&&r?n=s.addApplication(e.name,a,{silentError:!0}).catch(function(e){409===e.status?o.warning(p("Could not subscribe to the microservice because another application with the same context path is already subscribed.")):o.danger(l.getResErrorMessage(e))}):t&&!r&&(n=s.removeApplication(e.name,a)),n}))(u.activateAfterUpdate,u.wizardCtrl.data.createdApp).then(function(){return u.wizardCtrl.goTo("success")}):u.wizardCtrl.goTo("success"),t})(t))}).catch(function(e){t&&n.remove(t,{force:!0}),"typeValidation"===(e=e).type?o.warning(e.message,e.details):e.cancelled||o.danger((e=>(e=g(e,["data.message","data.error","errorMessage"])||p("An internal error occurred, try to upload again."),a.getString(e)))(e),(e=>g(e,["data.details","errorDetails"]))(e))}).finally(function(){d.inProgress=!1,d.progress=100})},goTo:function(e){m(),u.wizardCtrl.goTo(e)}}),e.$parent.$on("modal.closing",m),e.$watch(function(){return u.wizardCtrl.getCurrentStep()},function(e){"upload"===_.get(e,"stepId")&&u.wizardCtrl.data.appUpload&&(u.wizardCtrl.data.appUpload.files=[{file:null}])})}e.$inject=["$q","$scope","c8yBase","c8yApplication","c8yAlert","c8yZip","c8yModal","c8yTenant","gettext","gettextCatalog"],angular.module("c8y.parts.applications").component("appWizardUploadStep",{require:{wizardCtrl:"^c8yWizardStep"},templateUrl:":::PLUGIN_PATH:::/appWizard/appWizardUploadStep.html",controller:e,controllerAs:"vm"})})();