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 { createAutoRefreshHandlers, defineWidgetControls } from '@c8y/ngx-components/global-context';
import { hookWidgetConfig } from '@c8y/ngx-components/context-dashboard';

const autoRefreshHandlers = createAutoRefreshHandlers();
const datapointsGraphWidgetControls = defineWidgetControls({
    name: 'datapoints-graph-widget',
    supports: [
        'timeRange',
        'liveRefresh',
        'displayMode',
        'refreshInterval',
        'refreshOption',
        'dataAggregation',
        'source',
        'eventSourceId'
    ],
    handlers: {
        disableAutoRefresh: {
            handler: state => {
                const result = autoRefreshHandlers.disableAutoRefresh(state);
                return {
                    inlineControlSettings: result?.inlineControlSettings ?? {},
                    dashboardControlSettings: result?.dashboardControlSettings ?? {},
                    links: result?.links ?? {},
                    options: result?.options
                };
            }
        }
    },
    settings: {
        dashboard: {
            live: {
                inline: {
                    showAutoRefresh: true,
                    showTimeContext: true
                }
            },
            history: {
                inline: {
                    showTimeContext: true,
                    showAggregation: true
                }
            }
        },
        config: {
            live: {
                inline: {
                    showAutoRefresh: true,
                    showRefreshInterval: true
                },
                configuration: {
                    showAutoRefresh: true,
                    showRefreshInterval: true,
                    showTimeContext: true
                }
            },
            history: {
                inline: {},
                configuration: {
                    showTimeContext: true,
                    showAggregation: true
                }
            }
        },
        viewAndConfig: {
            live: {
                inline: {
                    showAutoRefresh: true,
                    showTimeContext: true,
                    showRefreshInterval: true
                },
                configuration: {
                    showAutoRefresh: true,
                    showRefreshInterval: true,
                    showTimeContext: true
                }
            },
            history: {
                inline: {
                    showTimeContext: true,
                    showAggregation: true
                },
                configuration: {
                    showTimeContext: true,
                    showAggregation: true
                }
            }
        },
        defaultLinks: {
            dashboard: {
                live: {
                    dateTimeContext: true,
                    isAutoRefreshEnabled: true
                },
                history: {
                    dateTimeContext: true,
                    aggregation: true
                }
            },
            config: {},
            viewAndConfig: {}
        }
    }
});

class DatapointGraphFactory {
    constructor() {
        this.previewFeatureService = inject(PreviewService);
        this.baseWidgetDefinition = {
            id: defaultWidgetIds.DATAPOINTS_GRAPH,
            label: gettext('Data points 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!@c8y/ngx-components/echart'),
                export: exportConfigWithTargets,
                import: importConfigWithTargets,
                widgetControls: datapointsGraphWidgetControls,
                settings: {
                    noNewWidgets: false,
                    widgetDefaults: {
                        _width: 8,
                        _height: 4
                    },
                    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.19", ngImport: i0, type: DatapointGraphFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: DatapointGraphFactory }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: DatapointGraphFactory, decorators: [{
            type: Injectable
        }] });

const datapointGraphWidgetproviders = [
    hookWidget(DatapointGraphFactory),
    hookPreview({
        key: 'ui.datapoint-graph.v2',
        label: 'Data points 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 point 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: 10,
        label: gettext('Time context'),
        initialState: {
            widgetControls: datapointsGraphWidgetControls
        },
        loadComponent: () => import('@c8y/ngx-components/context-dashboard').then(m => m.GlobalContextSectionComponent)
    }),
    hookWidgetConfig({
        widgetId: defaultWidgetIds.DATAPOINTS_GRAPH,
        priority: 10,
        label: gettext('Time context'),
        initialState: {
            widgetControls: datapointsGraphWidgetControls
        },
        loadComponent: () => import('@c8y/ngx-components/context-dashboard').then(m => m.GlobalContextSectionComponent)
    })
];

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

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