(()=>{function e(i,e,a,c,o,t,n){var r=c.cleanFields,s="devicecontrol/operations",u="\\/devicecontrol\\/operations\\/(\\d+)",l={headers:{"Content-Type":"application/json",Accept:"application/json"}},d={PENDING:n("PENDING"),SUCCESSFUL:n("SUCCESSFUL"),EXECUTING:n("EXECUTING"),FAILED:n("FAILED")},p=_.keys(d),f=["creationTime","deviceExternalIDs","id","self"],v=["deviceId","deviceName","bulkOperationId"],E={},g=f.concat(["deviceId","deviceName","bulkOperationId"]),I={failureReason:n("Failure reason"),description:n("Description"),status:n("Status")},y=e.$new(!0);function S(e){var t=c.url(s),e=c.pageSizeNoTotalFilter(e),n={params:e},e=c.cleanListCallback("operations",S,e);return i.get(t,n).then(e)}function h(e){var t=c.url(s),e=r(e,f),n=_.cloneDeep(l);if(e.deviceId)return i.post(t,e,n).then(C).finally(function(e){return y.$emit("create"),e});throw new Error("c8yDeviceControl: data must have a deviceId property")}function C(e){var t=new RegExp(u),e=e.headers("Location").match(t);return parseInt(e[1],10)}function D(e){var t="".concat(c.url(s),"/").concat(e.id),e=r(e,f.concat(v)),n=_.cloneDeep(l);return i.put(t,e,n).finally(function(){y.$emit("update")})}return E[d.PENDING]={icon:"clock-o",cls:""},E[d.EXECUTING]={icon:"refresh",cls:"text-info"},E[d.SUCCESSFUL]={icon:"check-circle",cls:"text-success"},E[d.FAILED]={icon:"exclamation-circle",cls:"text-danger"},t.enhanceWithKeysUtil({target:{list:S,listPaged:function(e){function n(e){var t=!_.isUndefined(e.paging.next)&&e.length>=c.pageSize;return i.notify(e),t||i.resolve(!0),!!r||!t||e.paging.next().then(n)}var i=a.defer(),c=_.assign(e||{},{pageSize:1e3}),r=!1;return _.assign(i.promise,{cancel:function(){r=!0,i.reject("canceled")}}),S(c).then(n),i.promise},detail:function(e){return e=(e=e).id||e,e=c.url("".concat(s,"/").concat(e)),i.get(e)},create:h,createWithNotifications:function(r){var e=a.resolve(r),t=h(r),n=t.then(function(e){r.id=e;var n=a.defer(),i="operation".concat(r.id),c="/operations/".concat(r.deviceId);return o.addListener(i,c,"UPDATE",function(e,t){t.id==r.id&&(t.status===d.SUCCESSFUL||t.status===d.FAILED?(o.destroySubscription(i,c),n.resolve(t)):n.notify(t))}),o.start(i,c),n.promise});return a.when({operationConfig:e,created:t,completed:n})},update:D,cancel:function(e){var t=D({id:e.id,status:d.FAILED,failureReason:n("Operation cancelled by user.")});return t.then(function(){e.status=d.FAILED}),t},save:function(e){return(e.id?D:h)(e)},status:d,statusList:p,getStyle:function(e){return e=_.isString(e)?e:e.status,E[e.toUpperCase()]},events:y},reservedKeys:g,standardKeys:I})}e.$inject=["$http","$rootScope","$q","c8yBase","c8yRealtime","c8yUtil","gettext"],angular.module("c8y.core").factory("c8yDeviceControl",e)})();