angular.module("c8y.deviceNetwork").controller("deviceNetworkCtrl",["$scope","c8yBase","c8yDeviceControl","c8ySettings","c8yAlert","gettext",function(a,e,r,t,n,s){var i=[{value:"pap",label:"PAP"},{value:"chap",label:"CHAP"},{value:"none",label:s("None")}],o=[{substr:{start:0,count:1},leadingBits:"0",netmask:"255.0.0.0"},{substr:{start:0,count:2},leadingBits:"10",netmask:"255.255.0.0"},{substr:{start:0,count:3},leadingBits:"110",netmask:"255.255.255.0"}];function c(e){return _.isBoolean(e)?e:Boolean(e)}function d(e,t){var n=e.c8y_Network.c8y_WAN,e={deviceId:e.id,description:s("Configure WAN settings"),deliveryType:t,c8y_Network:{c8y_WAN:{apn:n.apn,username:n.username,password:n.password,authType:n.authType}}};return r.create(e).then(_.partial(l,s("Sent operation to apply WAN settings."),a.wanForm))}function l(e,t){n.success(e),t&&t.$setPristine()}a.patterns={MAC:/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/},a.authTypes=i,a.invalid=angular.bind(e,e.invalid,a),a.onLanIpChanged=function(e){e.netmask=(e=>{var t,n;return e&&(e=e.split("."),n=function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:"0",e="".concat(e);return e.length>=t?e:new Array(t-e.length+1).join(n)+e}(parseInt(e[0],10).toString(2),8),t=null,_.forEach(o,function(e){n.substr(e.substr.start,e.substr.count)===e.leadingBits&&(t=e.netmask)})),t})(e.ip)},a.saveWANviaIP=function(e){return d(e)},a.saveWANviaSMS=function(e){return d(e,"SMS")},a.saveLANandDHCP=function(e){var t=e.c8y_Network.c8y_LAN,t={deviceId:e.id,description:s("Configure LAN settings"),c8y_Network:{c8y_LAN:{enabled:t.enabled?1:0,ip:t.ip,netmask:t.netmask}}},n=e.c8y_Network.c8y_DHCP,e={deviceId:e.id,description:s("Configure DHCP settings"),c8y_Network:{c8y_DHCP:{enabled:n.enabled?1:0,addressRange:n.addressRange,dns1:n.dns1,dns2:n.dns2,domainName:n.domainName}}};Promise.all(r.create(t),r.create(e)).then(_.partial(l,n.enabled?s("Sent operation to apply LAN and DHCP settings."):s("Sent operation to apply LAN settings."),a.lanDhcpForm))},a.$watch("device",function(e){_.update(e,"c8y_Network.c8y_LAN.enabled",c),_.update(e,"c8y_Network.c8y_DHCP.enabled",c)}),t.getSystemOptionValue({category:"messaging",key:"provider"},!1).then(function(e){a.smsEnabled=!!e})}]);