import { PreviewService, DynamicManagedObjectResolver, DynamicComponentErrorStrategy, hookWidget, hookPreview } from '@c8y/ngx-components';
import { WidgetAssetSelectorComponent, quoteAndEscape, hookWidgetConfig } from '@c8y/ngx-components/context-dashboard';
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { gettext } from '@c8y/ngx-components/gettext';
import { from, switchMap, map } from 'rxjs';
import { defaultWidgetIds } from '@c8y/ngx-components/widgets/definitions';
import { importConfigWithDevice, exportConfigWithDevice } from '@c8y/ngx-components/widgets/import-export-config';
import { PRESET_NAME } from '@c8y/ngx-components/global-context';

class ScadaWidgetFactory {
    constructor() {
        this.betaPreviewService = inject(PreviewService);
        this.baseWidgetDefinition = {
            id: defaultWidgetIds.SCADA,
            label: gettext('SCADA'),
            description: gettext(`Displays a dynamically updated SVG image for real-time device monitoring.`),
            loadComponent: () => import('@c8y/ngx-components/widgets/implementations/scada').then(m => m.ScadaWidgetViewComponent),
            errorStrategy: DynamicComponentErrorStrategy.OVERLAY_ERROR,
            previewImage: 'c8y-style-assets/scada-widget-preview.png',
            data: {
                schema: () => import('c8y-schema-loader?interfaceName=ScadaWidgetConfig!@c8y/ngx-components/widgets/implementations/scada'),
                export: exportConfigWithDevice,
                import: importConfigWithDevice,
                settings: {
                    noNewWidgets: false,
                    widgetDefaults: {
                        _width: 8,
                        _height: 4
                    }
                }
            },
            resolve: {
                device: DynamicManagedObjectResolver
            }
        };
    }
    get() {
        return from(import('@c8y/ngx-components/widgets/implementations/scada').then(m => m.SCADA_WIDGET_FEATURE_FLAG_NAME)).pipe(switchMap(featureFlagName => this.betaPreviewService.getState$(featureFlagName)), map(active => {
            if (active) {
                return [this.baseWidgetDefinition];
            }
            return [];
        }));
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: ScadaWidgetFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: ScadaWidgetFactory, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: ScadaWidgetFactory, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }] });

class ScadaWidgetConfigFactory {
    constructor() {
        this.betaPreviewService = inject(PreviewService);
        this.baseWidgetConfigDefinition = [
            {
                widgetId: defaultWidgetIds.SCADA,
                label: gettext('Asset selection'),
                loadComponent: () => Promise.resolve(WidgetAssetSelectorComponent),
                providers: () => import('@c8y/ngx-components/widgets/implementations/scada').then(m => [
                    m.ScadaWidgetConfigService
                ]),
                priority: 100,
                initialState: {
                    isRequired: false,
                    groupsSelectable: true
                }
            },
            {
                widgetId: defaultWidgetIds.SCADA,
                label: gettext('SVG configuration'),
                loadComponent: () => import('@c8y/ngx-components/widgets/implementations/scada').then(m => m.ScadaWidgetSvgSelectorComponent),
                providers: () => import('@c8y/ngx-components/widgets/implementations/scada').then(m => [
                    m.ScadaWidgetConfigService
                ]),
                priority: 70
            },
            {
                widgetId: defaultWidgetIds.SCADA,
                label: gettext('Placeholder mappings'),
                loadComponent: () => import('@c8y/ngx-components/context-dashboard').then(m => m.AssetPropertyMappingsComponent),
                priority: 50,
                expanded: true,
                initialState: {
                    widgetConfigPath: ['config', 'mappings'],
                    allowAddingNewMappings: false,
                    allowRenamingMappings: true,
                    mappingNameFormatRegex: /^[a-zA-Z_$][a-zA-Z0-9_$]*$/,
                    mappingNameFormatErrorMessage: gettext('Name must be a valid identifier (only letters, digits, underscores, or dollar signs allowed, cannot start with a digit).'),
                    allowRelabellingMappings: false,
                    allowEditingMappings: true,
                    allowRemovingMappings: false,
                    showLabels: false,
                    showNames: true,
                    showAssets: true,
                    showValues: true,
                    emptyStateTitle: gettext('No placeholder mappings'),
                    emptyStateSubtitle: gettext('Add first placeholder in the preview or code.'),
                    feedbackMessage: gettext('{{ count }} placeholders'),
                    onBeforeCopyMappingToClipboard: (mapping, config) => {
                        const name = String(mapping.name);
                        if (config?.config?.htmlWidgetConfig?.config?.legacy) {
                            return `{{ ${name} }}`;
                        }
                        const nonAlphanumericRegex = /[^a-zA-Z0-9]/;
                        const nameQuotedEscaped = quoteAndEscape(name);
                        const key = nonAlphanumericRegex.test(name) ? `[${nameQuotedEscaped}]` : name;
                        return '${this.c8yScadaValues?.' + key + '}';
                    }
                }
            },
            {
                widgetId: defaultWidgetIds.SCADA,
                label: gettext('Time context'),
                initialState: {
                    controls: PRESET_NAME.AUTO_REFRESH_ONLY_CONFIG
                },
                loadComponent: () => import('@c8y/ngx-components/context-dashboard').then(m => m.GlobalContextSectionComponent),
                priority: 90
            },
            {
                widgetId: defaultWidgetIds.SCADA,
                label: gettext('Advanced editor'),
                loadComponent: () => import('@c8y/ngx-components/widgets/implementations/scada').then(m => m.ScadaWidgetAdvancedEditorComponent),
                providers: () => import('@c8y/ngx-components/widgets/implementations/scada').then(m => [
                    m.ScadaWidgetConfigService
                ]),
                priority: 40,
                expanded: false
            }
        ];
    }
    get() {
        return from(import('@c8y/ngx-components/widgets/implementations/scada').then(m => m.SCADA_WIDGET_FEATURE_FLAG_NAME)).pipe(switchMap(featureFlagName => this.betaPreviewService.getState$(featureFlagName)), map(active => {
            if (active) {
                return this.baseWidgetConfigDefinition;
            }
            return [];
        }));
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: ScadaWidgetConfigFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: ScadaWidgetConfigFactory, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: ScadaWidgetConfigFactory, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }] });

const scadaWidgetProviders = [
    hookWidget(ScadaWidgetFactory),
    hookWidgetConfig(ScadaWidgetConfigFactory),
    hookPreview({
        key: 'ui.scada-widget.v2',
        label: 'SCADA widget v2',
        description: () => import('@c8y/style/markdown-files/scada-widget-preview.md').then(m => m.default),
        settings: {
            reload: true
        }
    })
];

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

export { scadaWidgetProviders };
//# sourceMappingURL=c8y-ngx-components-widgets-definitions-scada.mjs.map
