import { PreviewService, DynamicDatapointsResolver, hookWidget, hookPreview } from '@c8y/ngx-components';
import { gettext } from '@c8y/ngx-components/gettext';
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { map } from 'rxjs';
import { defaultWidgetIds } from '@c8y/ngx-components/widgets/definitions';
import cloneDeep from 'lodash/cloneDeep';
import { importConfigWithTargets, exportConfigWithTargets } from '@c8y/ngx-components/widgets/import-export-config';
import { hookWidgetConfig } from '@c8y/ngx-components/context-dashboard';
import { PRESET_NAME } from '@c8y/ngx-components/global-context';

class DatapointGraphFactory {
    constructor() {
        this.previewFeatureService = inject(PreviewService);
        this.baseWidgetDefinition = {
            id: defaultWidgetIds.DATAPOINTS_GRAPH,
            label: gettext('Data graph'),
            description: gettext('Displays a graph of selected data points'),
            loadComponent: () => import('@c8y/ngx-components/widgets/implementations/datapoints-graph').then(m => m.DatapointsGraphWidgetViewComponent),
            loadConfigComponent: () => import('@c8y/ngx-components/widgets/implementations/datapoints-graph').then(m => m.DatapointsGraphWidgetConfigComponent),
            previewImage: 'c8y-style-assets/datapoints-graph-widget.png',
            resolve: {
                datapoints: DynamicDatapointsResolver
            },
            data: {
                schema: () => import('c8y-schema-loader?interfaceName=DatapointsGraphWidgetConfig&type=widget-config!@c8y/ngx-components/echart'),
                export: exportConfigWithTargets,
                import: importConfigWithTargets,
                settings: {
                    noNewWidgets: false,
                    widgetDefaults: {
                        _width: 8,
                        _height: 6
                    },
                    noDeviceTarget: true,
                    groupsSelectable: false
                },
                displaySettings: {
                    globalTimeContext: true,
                    globalAggregationContext: true,
                    globalRealtimeContext: true
                }
            }
        };
    }
    get() {
        return this.previewFeatureService.getState$('ui.datapoint-graph.v2').pipe(map(state => {
            if (state) {
                const newWidgetDefinition = cloneDeep(this.baseWidgetDefinition);
                newWidgetDefinition.id = defaultWidgetIds.DATAPOINTS_GRAPH_NEW;
                newWidgetDefinition.data.settings.noNewWidgets = true;
                return [this.baseWidgetDefinition, newWidgetDefinition];
            }
            return [];
        }));
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: DatapointGraphFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: DatapointGraphFactory }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: DatapointGraphFactory, decorators: [{
            type: Injectable
        }] });

const DATAPOINT_SELECT_DEFAULT_OPTIONS = {
    showRange: true,
    showChart: true
};
const MIN_ACTIVE_COUNT = 1;

const datapointGraphWidgetproviders = [
    hookWidget(DatapointGraphFactory),
    hookPreview({
        key: 'ui.datapoint-graph.v2',
        label: 'Data graph',
        description: () => import('@c8y/style/markdown-files/datapoint-graph-preview.md').then(m => m.default),
        settings: {
            reload: true
        }
    }),
    /* We need to register the widget config for both the new and old widget IDs. This ensures compatibility for widgets
     created with the new data graph id (e.g. if a data point graph widget was created via community plugins)
      and widgets using the default one (toggled via preview feature)
    */
    hookWidgetConfig({
        widgetId: defaultWidgetIds.DATAPOINTS_GRAPH_NEW,
        priority: 90,
        label: gettext('Time context'),
        initialState: {
            controls: PRESET_NAME.DATAPOINTS_GRAPH_CONFIG
        },
        loadComponent: () => import('@c8y/ngx-components/context-dashboard').then(m => m.GlobalContextSectionComponent)
    }),
    hookWidgetConfig({
        widgetId: defaultWidgetIds.DATAPOINTS_GRAPH,
        priority: 90,
        label: gettext('Time context'),
        initialState: {
            controls: PRESET_NAME.DATAPOINTS_GRAPH_CONFIG
        },
        loadComponent: () => import('@c8y/ngx-components/context-dashboard').then(m => m.GlobalContextSectionComponent)
    }),
    hookWidgetConfig({
        widgetId: defaultWidgetIds.DATAPOINTS_GRAPH,
        label: gettext('Alarm selection'),
        loadComponent: () => import('@c8y/ngx-components/alarm-event-selector').then(m => m.WidgetAlarmSelectorComponent),
        initialState: {
            configStoreMode: 'MIXED'
        },
        priority: 70
    }),
    hookWidgetConfig({
        widgetId: defaultWidgetIds.DATAPOINTS_GRAPH,
        label: gettext('Event selection'),
        loadComponent: () => import('@c8y/ngx-components/alarm-event-selector').then(m => m.WidgetEventSelectorComponent),
        initialState: {
            configStoreMode: 'MIXED'
        },
        priority: 60
    }),
    hookWidgetConfig({
        widgetId: defaultWidgetIds.DATAPOINTS_GRAPH,
        label: gettext('Data point selection'),
        loadComponent: () => import('@c8y/ngx-components/datapoint-selector').then(m => m.WidgetDatapointsSelectorComponent),
        initialState: {
            minActiveCount: MIN_ACTIVE_COUNT,
            defaultFormOptions: DATAPOINT_SELECT_DEFAULT_OPTIONS,
            useAdvancedChartOptions: true
        },
        expanded: true,
        priority: 80
    })
];

/**
 * Generated bundle index. Do not edit.
 */

export { datapointGraphWidgetproviders };
//# sourceMappingURL=c8y-ngx-components-widgets-definitions-datapoints-graph.mjs.map
