angular.module("c8y.modbusWidgets").controller("modbusWidgetCtrl",["$scope","$q","$timeout","c8yBase","c8yDevices","c8yDeviceDatabase","c8yModbusDevice","c8yDeviceControl","c8yAlert","c8yRealtime","modbusWidgetSvc","gettext","gettextCatalog",function(t,n,i,u,a,c,o,e,l,r,s,d,m){var g,f,h,v,y="/managedobjects",p=d("Value out of range. Allowed values from {{min | number}} to {{max | number}} with step {{step | number}}. Use up/down arrows to find nearest possible value."),b=[];function C(e){return"".concat(y,"/").concat(e.id)}function E(e){g=e,(t.device=g)&&g.id&&(r.addListener(t.$id,C(g),r.realtimeActions().UPDATE,S),r.getStatus(t.$id,C(g))||r.switchRealtime(t.$id,C(g)))}function S(e,t){g.id===t.id&&(g.c8y_CoilStatus=_.assign(g.c8y_CoilStatus||{},t.c8y_CoilStatus),g.c8y_RegisterStatus=_.assign(g.c8y_RegisterStatus||{},t.c8y_RegisterStatus),O(),I())}function V(e){return e?(f=e,t.deviceType=f,e):n.reject()}function w(){h=_(f.c8y_Coils).filter(D).map(c.addDiscreteIOMeta||c.addCoilMeta).value(),t.coils=h}function D(e){return s.isDiscreteIOSelectedInConfig(e,t.child.config)}function R(){v=_(f.c8y_Registers).filter($).map(c.addRegisterMeta).value(),t.registers=v}function $(e){return s.isRegisterSelectedInConfig(e,t.child.config)}function T(){var e=[].concat(h).concat(v);t.categories=_.groupBy(e,j)}function j(e){return e.category||""}function O(){_.forEach(h,function(e){U(e)||(e.valueObj=_.find(e.meta.enumValues,{value:o.getCurrentElementValue(g,e)}))})}function I(){_.forEach(v,function(e){U(e)||(e.meta.enumType?e.valueObj=_.find(e.meta.enumValues,{value:o.getCurrentElementValue(g,e)}):(e.valueObj=e.valueObj||{},e.valueObj.value=o.getCurrentElementValue(g,e)))})}function U(e){return o.isElementChangePending(e)||_.includes(b,e)}t.canUpdateElement=o.canUpdateElement,t.isElementChangePending=o.isElementChangePending,t.getCurrentElementValue=o.getCurrentElementValue,t.updateElement=function(e,t,n){o.updateElement(e,t,n)},t.shouldShowSwitch=function(e){return e.meta.enumType&&e.meta.enumValues&&2===e.meta.enumValues.length},t.shouldShowEnumSelect=function(e){return e.meta.enumType&&e.meta.enumValues&&(1===e.meta.enumValues.length||2<e.meta.enumValues.length)},t.shouldShowNumberInput=function(e){return!e.meta.enumType},t.validationHints=function(e,t){return o.canUpdateElement(g,t)?u.validationHints(e,{required:d("This field is required."),"value-in-register-range":m.getString(p,t&&t.meta&&t.meta.valuesRange.custom)}):""},t.focus=function(e){b.push(e)},t.blur=function(e){b=_.without(b,e),i(O,e=500),i(I,e)},t.$watch("child.config",function(){var e=a.detail(t.child.config.device.id).then(u.getResData);e.then(E),e.then(c.getDeviceTypeFromDevice).then(V).then(w).then(O).then(R).then(I).then(T)}),t.$on("$destroy",function(){g&&g.id&&r.destroySubscription(t.$id,C(g))})}]);