import * as i0 from '@angular/core';
import { Injectable, inject, NgModule } from '@angular/core';
import * as i1$1 from '@angular/router';
import { RouterModule } from '@angular/router';
import * as i1 from '@c8y/ngx-components';
import { NavigatorNode, gettext, hookNavigator, HookProviderTypes, hookTab } from '@c8y/ngx-components';
import { map, shareReplay, distinctUntilChanged, switchMap, filter } from 'rxjs/operators';
import { of, merge } from 'rxjs';
import { StoreBrandingService } from '@c8y/ngx-components/branding/shared/data';

class SupportsBrandingService {
    constructor(appState) {
        this.appState = appState;
        this.supportsBranding$ = this.appState.currentAppsOfUser.pipe(map(apps => {
            const brandingFeatureApp = apps.find(app => app.name === 'feature-branding' && app.owner?.tenant?.id === 'management');
            return !!brandingFeatureApp;
        }), shareReplay(1));
        this.node = new NavigatorNode({
            featureId: 'branding-editor',
            label: 'Branding',
            path: 'branding-editor',
            icon: 'palette',
            parent: 'Settings',
            routerLinkExact: false
        });
    }
    get() {
        return this.supportsBranding$.pipe(map(supported => {
            if (supported) {
                return this.node;
            }
            return [];
        }));
    }
    canActivate() {
        return this.supportsBranding$;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: SupportsBrandingService, deps: [{ token: i1.AppStateService }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: SupportsBrandingService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: SupportsBrandingService, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }], ctorParameters: () => [{ type: i1.AppStateService }] });

const lightThemeTabName = gettext('Light theme');
const genericTabName = gettext('Generic');
class BrandingTabFactory {
    get(activatedRoute) {
        const routes = activatedRoute.pathFromRoot.map(route => route.url);
        return this.shouldShowTab$().pipe(distinctUntilChanged(), switchMap(value => {
            if (!value) {
                return of([]);
            }
            return merge(...routes).pipe(filter(urlSegments => urlSegments.some(urlSegment => urlSegment.path === 'branding-editor')), switchMap(() => this.shouldShowTab$()), filter(shouldShow => !!shouldShow), switchMap(() => activatedRoute.parent?.params.pipe(map(params => params.name), distinctUntilChanged(), filter(name => !!name), map(name => this.getTabsForVersion(name)))));
        }));
    }
    shouldShowTab$() {
        return of(true);
    }
    getPathForBranding(brandingName, subPath) {
        return `branding-editor/${brandingName}/edit/${subPath}`;
    }
    getTabsForVersion(name) {
        return [
            {
                path: this.getPathForBranding(name, 'generic'),
                label: genericTabName,
                icon: 'palette',
                priority: 600
            },
            {
                path: this.getPathForBranding(name, 'light'),
                label: lightThemeTabName,
                icon: 'sun',
                priority: 500
            }
        ];
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BrandingTabFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BrandingTabFactory, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BrandingTabFactory, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }] });

const BRANDING_EDIT_CHILD_ROUTES = [
    {
        path: '',
        pathMatch: 'full',
        redirectTo: 'generic'
    },
    {
        path: 'generic',
        loadComponent: () => import('@c8y/ngx-components/branding/shared/lazy').then(m => m.BrandingFormComponent),
        data: {
            tabName: genericTabName
        }
    },
    {
        path: 'light',
        loadComponent: () => import('@c8y/ngx-components/branding/shared/lazy').then(m => m.BrandingThemeFormComponent),
        data: {
            tabName: lightThemeTabName
        }
    }
];
const BRANDING_ROUTING = [
    {
        path: 'branding-editor',
        canActivate: [SupportsBrandingService],
        children: [
            {
                path: '',
                pathMatch: 'full',
                loadComponent: () => import('@c8y/ngx-components/branding/shared/lazy').then(m => m.BrandingComponent)
            },
            {
                path: ':name',
                resolve: {
                    branding: ((route, _state) => {
                        return inject(StoreBrandingService).getBrandingOptionsForVersion(route.params.name);
                    }),
                    tags: (async (route, _state) => {
                        const store = inject(StoreBrandingService);
                        const { variants } = await store.loadBrandingVariants();
                        return variants.find(v => v?.tags?.includes(route.params.name))?.tags;
                    }),
                    fallbackBranding: (async (_route, _state) => {
                        try {
                            const store = inject(StoreBrandingService);
                            const fallbackBranding = await store.getBrandingOptionsForVersion('fallback');
                            return fallbackBranding;
                        }
                        catch (e) {
                            return {};
                        }
                    })
                },
                children: [
                    {
                        path: '',
                        pathMatch: 'full',
                        redirectTo: 'edit'
                    },
                    {
                        path: 'edit',
                        loadComponent: () => import('@c8y/ngx-components/branding/shared/lazy').then(m => m.EditBrandingRouterOutletComponent),
                        children: BRANDING_EDIT_CHILD_ROUTES
                    }
                ]
            }
        ]
    },
    // Redirects for legacy paths from old angularJS implementation
    // allows to keep old links working and to use the old branding manager (as long as we still ship it) when the new plugins are removed from admin app
    {
        path: 'enterprise/branding',
        pathMatch: 'full',
        redirectTo: 'branding-editor'
    },
    {
        path: 'branding',
        pathMatch: 'full',
        redirectTo: 'branding-editor'
    }
];
class SharedBrandingModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: SharedBrandingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.13", ngImport: i0, type: SharedBrandingModule, imports: [i1$1.RouterModule] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: SharedBrandingModule, providers: [
            // providerType: HookProviderTypes.ExistingProvider is used to ensure that the hook is only applied once even if the module is imported multiple times
            hookNavigator(SupportsBrandingService, { providerType: HookProviderTypes.ExistingProvider }),
            hookTab(BrandingTabFactory)
        ], imports: [RouterModule.forChild(BRANDING_ROUTING)] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: SharedBrandingModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [RouterModule.forChild(BRANDING_ROUTING)],
                    providers: [
                        // providerType: HookProviderTypes.ExistingProvider is used to ensure that the hook is only applied once even if the module is imported multiple times
                        hookNavigator(SupportsBrandingService, { providerType: HookProviderTypes.ExistingProvider }),
                        hookTab(BrandingTabFactory)
                    ]
                }]
        }] });

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

export { BRANDING_EDIT_CHILD_ROUTES, BRANDING_ROUTING, BrandingTabFactory, SharedBrandingModule, genericTabName, lightThemeTabName };
//# sourceMappingURL=c8y-ngx-components-branding-shared.mjs.map
