(()=>{function e(i,n,o,e,t,s,l,a,r,c,u,d,f,g,m){var y=this,p="YYYY-MM-DDTHH:mm:ssZZ",h={EVENT:"logsViewer",LOGS:{ACTIONS:{REQUEST:"request",DELETE:"delete",CANCEL:"cancel",DOWNLOAD:"download"}}};function L(){i.filters={dateFrom:moment().subtract(1,"day"),dateTo:moment(),logFile:i.logTypes[0]&&i.logTypes[0].name||null,maximumLines:1e3},y.filters=i.filters}function E(e){e=_.assign({deviceId:n.deviceId,fragmentType:"c8y_LogfileRequest",dateFrom:new Date(0).toISOString(),revert:!0,withTotalPages:!0},e);return a.list(e).then(C)}function C(e,t){i.operations&&!t||(i.operations=[]),e.forEach(function(e){i.operations.push(e)}),i.paging=e.paging}function v(){_.get(i.paging,"refresh")&&(i.realtimeAnimation=!1,i.refreshLoading=!0,i.paging.refresh().then(_.partialRight(C,!0)).finally(function(){i.refreshLoading=!1}),i.paging=null)}function T(e){if(e.c8y_LogfileRequest.file){e=s.getIdFromUrl(e.c8y_LogfileRequest.file);if(e)return s.removeBinary(e)}return!1}function R(e){e={id:e.id,status:e.status,c8y_LogfileRequest:null};return a.save(e)}function S(e){D(e)?i.selectedLog=e:i.selectedLog=void 0}function D(e){return e.c8y_LogfileRequest&&e.c8y_LogfileRequest.file}function q(e){return e&&"SUCCESSFUL"===e.status}function F(e){return e&&"FAILED"===e.status}function I(e,t){t&&!e?(y.shouldClose="disabled",o(function(){y.shouldClose="outsideClick"})):y.shouldClose="outsideClick"}i.PRODUCT_EXPERIENCE=h,i.requestLogfile=function(e){var t={deviceId:n.deviceId,description:d("Log file request"),c8y_LogfileRequest:{dateFrom:moment(e.dateFrom).format(p),dateTo:moment(e.dateTo).format(p),logFile:e.logFile,searchText:e.searchText||"",maximumLines:e.maximumLines}};a.createWithNotifications(t).then(function(e){e.created.then(v),e.completed.then(function(e){e.status===a.status.SUCCESSFUL?(_.assign(t,e),S(t)):e.status===a.status.FAILED&&"Operation cancelled by user."!==e.failureReason&&(e.failureReason?u.danger(f('Could not load file: "{{failureReason | translate}}".',e)):u.danger(d("Could not load file.")))},_.noop,function(e){_.assign(t,e),S(t)}).then(v)})},i.hasLogFile=D,i.selectLogfile=S,i.isBinary=function(e){return e&&e.c8y_LogfileRequest&&e.c8y_LogfileRequest.file&&(!!s.getIdFromUrl(e.c8y_LogfileRequest.file)||t.isEventBinary(e.c8y_LogfileRequest.file))},i.download=g.download,i.cancel=function(e){return"PENDING"===e.status&&a.cancel(e).then(function(){return u.success(d("Log file request cancelled."))}).then(v)},i.remove=function(e){return c({title:d("Delete log file"),body:d("You are about to delete this log file. Do you want to proceed?"),status:"danger",labels:{ok:d("Delete")}}).then(_.partial(T,e)).then(_.partial(R,e)).then(function(){m.triggerEvent(h.EVENT,{action:h.LOGS.ACTIONS.DELETE}),u.success(d("Log file deleted."))}).then(v)},i.statusIcon=function(e){return a.getStyle(e).icon},i.statusClass=function(e){return a.getStyle(e).cls},i.isSuccessful=q,i.isPending=function(e){return e&&"PENDING"===e.status},i.isFailed=F,i.isCompleted=function(e){return q(e)||F(e)},i.refresh=v,i.loadNext=function(e){i.paging.loading=!0,(e?E({pageSize:e}):i.paging.next().then(C)).finally(function(){i.paging.loading=!1})},i.getOperationDesc=function(e){return e.description||d("no description available")},i.$watch("vm.datePickerToOpen",I),i.$watch("vm.datePickerFromOpen",I),i.$watch("vm.logtypeSelect.open",function(e){e||(y.shouldClose="disabled",o(function(){y.shouldClose="outsideClick"}))}),y.shouldClose="outsideClick",y.datePickerToOpen=!1,y.datePickerFromOpen=!1,l.detail(n.deviceId).then(e.getResData).then(function(e){i.logTypes=_.map(e.c8y_SupportedLogs,function(e){return{name:e}}),y.logTypes=i.logTypes}).then(L),E();var O=i.$id,w="/operations/".concat(n.deviceId),N=r.realtimeActions();_.forOwn(N,function(e){r.addListener(O,w,e,function(e,t){var n;_.has(t,"c8y_LogfileRequest")&&(v(),n=i.selectedLog)&&n.id===t.id&&e===N.UPDATE&&(n.c8y_LogfileRequest=t.c8y_LogfileRequest)})}),r.start(O,w),i.$on("$destroy",function(){r.stop(O,w),r.destroySubscription(O,w)})}e.$inject=["$scope","$routeParams","$timeout","c8yBase","c8yEvents","c8yBinary","c8yDevices","c8yDeviceControl","c8yRealtime","c8yModal","c8yAlert","gettext","gettextCatalog","logViewer","c8yGainsightService"],angular.module("c8y.logViewer").controller("logViewerCtrl",e)})();