angular.module("c8y.cockpit.dataPointExplorer").controller("DataPointWidgetConfigCtrl",["$scope","c8yDataPointSvc","c8yWidgetConfigService",function(o,s,e){o.noActions=!0,o.dontSave=!0,o.config=o.config||{},o.config.datapoints=o.config.datapoints||[],o.config.alarmsEventsConfigs=o.config.alarmsEventsConfigs||[],e.addOnBeforeSave&&e.addOnBeforeSave(function(e){e=e||o.config;return e.datapoints=o.data.datapoints,e.alarmsEventsConfigs=o.data.alarmsEventsConfigs,!0});o.onRealtimeAlarmFn=function(e){o.onAlarm=e},o.onRealtimeEventFn=function(e){o.onEvent=e};function a(e,t){var o=e.datapoints,a=e.alarmsEventsConfigs,n=_.pick(t,"dateFrom","dateTo","aggregation","displayDateSelection","isAutoRefreshEnabled","refreshInterval","refreshOption","displayMode","interval","realtime","dateTimeContext","widgetInstanceGlobalTimeContext","canDecoupleGlobalTimeContext"),i=e.dateFrom&&e.dateTo?_.omit(s.getDefaultFilters(),["dateFrom","dateTo","date"]):s.getDefaultFilters();_.assign(e,i,n),e.datapoints=o,e.alarmsEventsConfigs=a,t.dateTimeContext&&(e.date=[t.dateTimeContext.dateFrom,t.dateTimeContext.dateTo],e.dateFrom=t.dateTimeContext.dateFrom,e.dateTo=t.dateTimeContext.dateTo,e.interval=t.dateTimeContext.interval)}function t(e,t){o.config[e]=t,o.data[e]=t}o.onBoxChanged=function(e){o.chartBox=e},o.widgetControls={supports:["dateTimeContext","isAutoRefreshEnabled","displayMode","refreshInterval","refreshOption","aggregation"],configSettings:{dashboard:{live:{},history:{}},config:{live:{showAutoRefresh:!0,showRefreshInterval:!0,showTimeContext:!0},history:{showAggregation:!0,showTimeContext:!0}},view_and_config:{live:{showAutoRefresh:!0,showRefreshInterval:!0,showTimeContext:!0},history:{showAggregation:!0,showTimeContext:!0}}},settings:{dashboard:{live:{showAutoRefresh:!0,showTimeContext:!0,showRefreshInterval:!0},history:{showTimeContext:!0,showAggregation:!0}},config:{live:{showAutoRefresh:!0},history:{showTimeContext:!0}},view_and_config:{live:{showAutoRefresh:!0,showRefresh:!1,showRefreshInterval:!0,showTimeContext:!0},history:{showTimeContext:!0,showAggregation:!0}}},defaultLinks:{dashboard:{live:{dateTimeContext:!0,isAutoRefreshEnabled:!0},history:{dateTimeContext:!0,aggregation:!0}},config:{},view_and_config:{}},stateHandlers:{}},o.data={datapoints:_.cloneDeep(o.config.datapoints),alarmsEventsConfigs:_.cloneDeep(o.config.alarmsEventsConfigs)},o.showInWidgetConfig=!1,a(o.data,o.config),o.wrapperConfig=_.cloneDeep(o.data);var n=e.currentConfig$.subscribe(function(e){e&&(e.dateTimeContext&&(t("dateTimeContext",e.dateTimeContext),t("dateFrom",e.dateTimeContext.dateFrom),t("dateTo",e.dateTimeContext.dateTo),t("interval",e.dateTimeContext.interval)),e.displayMode&&t("displayMode",e.displayMode),void 0!==e.isAutoRefreshEnabled&&t("isAutoRefreshEnabled",e.isAutoRefreshEnabled),void 0!==e.refreshInterval&&t("refreshInterval",e.refreshInterval),void 0!==e.refreshOption&&t("refreshOption",e.refreshOption),void 0!==e.aggregation&&t("aggregation",e.aggregation),o.wrapperConfig=_.cloneDeep(o.data))});o.$watch("data",function(e,t){_.isEqual(e,t)||(a(o.config,o.data),o.config.datapoints=o.data.datapoints,o.config.alarmsEventsConfigs=o.data.alarmsEventsConfigs)},!0),o.$on("$destroy",function(){n&&n.unsubscribe()})}]);