import { inject } from '@angular/core';
import { ContextRouteService, hookRoute, ViewContext, hookPreview } from '@c8y/ngx-components';
import { canActivateDatapointExplorer } from '@c8y/ngx-components/datapoint-explorer';
import { WorkspaceConfigurationService, NameGeneratorService } from '@c8y/ngx-components/datapoint-explorer/view';
import { DatapointLibraryService } from '@c8y/ngx-components/datapoint-selector';
import { gettext } from '@c8y/ngx-components/gettext';

const settingsResolver = async (snapshot, _) => {
    const contextRouteService = inject(ContextRouteService);
    const datapointLibraryService = inject(DatapointLibraryService);
    const workspaceConfigurationService = inject(WorkspaceConfigurationService);
    const nameGeneratorService = inject(NameGeneratorService);
    const routeContext = contextRouteService.getContextData(snapshot);
    const datapoints = (await datapointLibraryService.getDatapointsOfAsset(routeContext.contextData))?.slice(0, 25) || [];
    let configurationId;
    if (datapoints.length) {
        datapoints.at(0).__active = true;
        workspaceConfigurationService.contextIdSignal.set(routeContext?.contextData?.id);
        configurationId = `dm-measurements-${routeContext?.contextData?.id}`;
        if (!(workspaceConfigurationService.getConfigurations() || []).some(c => c.id === configurationId)) {
            const configuration = {
                id: configurationId,
                label: nameGeneratorService.generateName(),
                config: { datapoints, alarmsEventsConfigs: [] }
            };
            workspaceConfigurationService.saveConfigurations([configuration], configuration.id);
            localStorage.setItem(workspaceConfigurationService.LOCAL_STORAGE_DEFAULT_ID_KEY, configuration.id);
        }
    }
    return {
        hideWorkspaceConfig: true,
        hideExportSelector: true,
        hideWidgetActions: true,
        defaultConfigurationId: configurationId,
        timeContext: { interval: 'hours' }
    };
};
const dataExplorerMeasurementsFeature = [
    hookRoute({
        context: ViewContext.Device,
        path: 'measurements-v2',
        priority: 800,
        icon: 'line-chart',
        label: gettext('Measurements'),
        loadComponent: () => import('@c8y/ngx-components/datapoint-explorer/view').then(m => m.DatapointExplorerComponent),
        featureId: 'dataExplorer',
        canActivate: [canActivateDatapointExplorer],
        resolve: {
            settings: settingsResolver
        }
    }),
    hookPreview({
        key: 'ui.datapoint-explorer.v2',
        label: 'Data point explorer',
        description: () => import('@c8y/style/markdown-files/datapoint-explorer-preview.md').then(m => m.default),
        settings: {
            reload: true
        }
    })
];

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

export { dataExplorerMeasurementsFeature, settingsResolver };
//# sourceMappingURL=c8y-ngx-components-datapoint-explorer-devicemanagement.mjs.map
