(()=>{function e(o,n,c,a,t,i,d,r,f,s){var g=this,u="",e={c8y_Global:{}};function p(e){var i,n,t,c,a;return g.svgFile.data=e,g.svgFile&&(i=o.config.mapping||{},n=o.config.deviceIds||{},t=o.config.device&&o.config.device.id,o.config.mapping={},o.config.deviceIds={},_.forEach((e=>{for(var i=[],n=/{{\s*([$\w]+?)\s*}}/g,t=n.exec(e);null!==t;)i.push(t[1]),t=n.exec(e);return i})(g.svgFile.data),function(e){o.config.mapping[e]=i[e]||null,o.config.deviceIds[e]=n[e]||t||null}),c=o.config.deviceIds,a=o.config.mapping,c)&&a&&_.forOwn(c,function(e,i){var e=l(e),n=r.generatePaths(e);f.list().then(function(e){e=_.sortBy(e,function(e){return e.keyPath.length}).reverse();null===a[i]&&(_.forEach(e,function(e){_.last(e.keyPath).toUpperCase()===i.toUpperCase()&&(a[i]=e)}),_.forEach(n,function(e){_.last(e).toUpperCase()===i.toUpperCase()&&(a[i]=r.createProperty(e))}))})}),e}function l(i){return _.find(g.devices,function(e){return i===e.id})}function v(e,i){g.device&&g.device.id!==i.id||_.assign(g.device,i)}function h(){e=o.config.deviceIds;var e=_(e).values().uniq().value().sort(function(e,i){return e-i}).join(",");e!==u&&(u.length&&i.stop(o.$id,"/managedobjects/".concat(u)),e.length&&(i.addListener(o.$id,"/managedobjects/".concat(e),"UPDATE",v),i.start(o.$id,"/managedobjects/".concat(e)),o.$on("$destroy",_.partial(i.stop,o.$id,"/managedobjects/".concat(e)))),u=e)}this.deviceDetail=function(e,i){var t;e&&(t=!i||!(e.name===i.name&&e.id===i.id),a.detail(e.id).then(function(e){g.device=e.data;e=_.union([g.device.id],_.map(_.union(g.device.childAssets.references,g.device.childDevices.references),function(e){return e.managedObject.id}));_.remove(e,_.isUndefined),n.all(_.map(e,function(e){return a.detail(e)})).then(function(e){var n;g.devices=_.map(e,function(e){e=e.data;return _.assign({},e,{name:c("truncate")(e.name)})}),n=t,_.forOwn(o.config.mapping,function(e,i){1!==g.devices.length&&!n||(o.config.deviceIds[i]=o.config.device.id),o.config.deviceIds[i]=o.config.deviceIds[i]||o.config.device.id}),g.updateComputedProperties(o.config.deviceIds,o.config.mapping)})}))},this.onSelectFile=function(i){g.loading=!0,t.upload(i,e).then(function(e){g.binaryFile=e.data,o.config.binaryId=e.data.id,g.retrieveSvgData(i),o.forms.componentForm.$setDirty()}).finally(function(){g.loading=!1})},this.updateComputedProperties=function(e,t){_.isEmpty(e)||_.isEmpty(t)||_.forOwn(e,function(e,i){var n=[t[i]];t[i]&&(i=l(e),d.attach(i,_.filter(n,function(e){return _.isObjectLike(e)})))})},this.onMappingChange=function(){g.updateComputedProperties(o.config.deviceIds,o.config.mapping)},this.onDeviceIdsChange=function(){g.updateComputedProperties(o.config.deviceIds,o.config.mapping),h()},this.retrieveSvgData=function(i){g.svgFile={name:i.name,size:i.length},(i instanceof Blob?t.readDataUri(i):t.downloadAsDataUri(i)).then(t.decodeDataUri).then(function(e){return new File([e],i.name,{type:i.type})}).then(function(e){return s.sanitizeFileByLevel(e,o.config.sanitization)}).then(function(e){return t.readDataUri(e)}).then(t.decodeDataUri).then(p)},g.binaryFile={},o.config.mapping=o.config.mapping||{},o.config.deviceIds=o.config.deviceIds||{},o.config.devices&&delete o.config.devices,(()=>{if(!o.config.binaryId)return n.when();g.loading=!0,o.config.sanitization||(o.config.sanitization="none"),t.detail(o.config.binaryId).then(function(e){g.binaryFile=e.data,g.retrieveSvgData(g.binaryFile),g.loading=!1}).then(function(){var e=[];e.push({file:g.svgFile}),g.files=e})})(),h(),o.$watch("config.device",this.deviceDetail,!0),o.$watch("config.mapping",this.onMappingChange,!0),o.$on("$destroy",function(){_.forEach(o.config.deviceIds,function(e){d.detach(l(e))})})}e.$inject=["$scope","$q","$filter","c8yDevices","c8yBinary","c8yRealtime","c8yComputedProperties","fieldbusPropertyGenerator","c8yPropertiesLibrary","c8ySanitizeHtml"],angular.module("c8y.parts.scadaWidget").controller("ScadaWidgetConfigCtrl",e)})();