import * as i0 from '@angular/core';
import { Component } from '@angular/core';
import * as i2 from '@angular/router';
import { gettext } from '@c8y/ngx-components/gettext';
import * as i1 from '@c8y/ngx-components';
import { CoreModule } from '@c8y/ngx-components';
import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { ReactiveFormsModule } from '@angular/forms';
import * as i3 from '@angular/common';

const PRODUCT_EXPERIENCE_HELP_AND_SERVICE_WIDGET = {
    HELP_AND_SERVICE_LINKS: {
        EVENTS: { HELP_AND_SERVICE_LINKS: 'helpAndServiceLinks' },
        COMPONENTS: { HELP_AND_SERVICE_COMPONENT: 'right-drawer' },
        ACTIONS: {},
        RESULTS: {}
    }
};

class HelpAndServiceViewComponent {
    constructor(docs, appStateService, navigatorService, router) {
        this.docs = docs;
        this.appStateService = appStateService;
        this.navigatorService = navigatorService;
        this.router = router;
        this.PRODUCT_EXPERIENCE = PRODUCT_EXPERIENCE_HELP_AND_SERVICE_WIDGET;
        this.links$ = combineLatest([this.docs.items$, this.navigatorService.items$]).pipe(map(([links, navigatorNodes]) => this.handleDocLinks([...links], navigatorNodes)));
    }
    openLink(link, $event) {
        if (link.click) {
            $event.preventDefault();
            link.click();
        }
    }
    handleDocLinks(links, navigatorNodes) {
        const groupLink = this.createAddGroupDocLink(navigatorNodes);
        if (groupLink) {
            links.push(groupLink);
        }
        return this.replaceDocsLinksWithMainOne(links);
    }
    createAddGroupDocLink(navigatorNodes) {
        let docLink;
        const groupsNodeLabel = gettext('Groups');
        const groupsNode = this.findNavigatorNode(groupsNodeLabel, navigatorNodes);
        if (groupsNode) {
            docLink = {
                type: 'quicklink',
                icon: 'c8y-icon c8y-icon-group-add',
                label: gettext('Add group'),
                url: '/group',
                click: () => this.router.navigate([docLink.url], { queryParams: { showAddGroup: true } })
            };
        }
        return docLink;
    }
    replaceDocsLinksWithMainOne(links) {
        const DOCS_PATH = '/docs/';
        let firstDocsLink = true;
        return links
            .map(link => {
            const isDocsLink = link.url && /\/docs\/(?!legal-notices)/.test(link.url);
            if (isDocsLink) {
                if (firstDocsLink) {
                    firstDocsLink = false;
                    // Replace the first /docs/ link with the main one
                    return {
                        icon: 'book-shelf',
                        label: gettext('User documentation'),
                        url: this.docs.getUserGuideLink(DOCS_PATH),
                        type: 'doc',
                        target: '_blank'
                    };
                }
                else {
                    // Filter out subsequent /docs/ links
                    return null;
                }
            }
            else {
                return {
                    ...link,
                    target: this.isCurrentApp(link) ? null : '_blank'
                };
            }
        })
            .filter(link => link !== null);
    }
    isCurrentApp(link) {
        const currentApp = this.appStateService.state.app;
        const destinationAppPath = (link.url?.split('/apps/')[1] || '').split('/')[0];
        return currentApp.contextPath === destinationAppPath;
    }
    findNavigatorNode(nodeName, navNodes = []) {
        return navNodes.find((node) => node.label === nodeName);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: HelpAndServiceViewComponent, deps: [{ token: i1.DocsService }, { token: i1.AppStateService }, { token: i1.NavigatorService }, { token: i2.Router }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.16", type: HelpAndServiceViewComponent, isStandalone: true, selector: "c8y-help-and-service-view", ngImport: i0, template: "<div class=\"card-group-block\">\n  <a\n    class=\"card card--btn\"\n    title=\"{{ link.label | translate }}\"\n    role=\"button\"\n    [attr.target]=\"link.target\"\n    *ngFor=\"let link of links$ | async\"\n    rel=\"noopener noreferrer\"\n    [href]=\"link.url ? link.url : '#'\"\n    (click)=\"openLink(link, $event)\"\n    c8yProductExperience\n    [actionName]=\"PRODUCT_EXPERIENCE.HELP_AND_SERVICE_LINKS.EVENTS.HELP_AND_SERVICE_LINKS\"\n    [actionData]=\"{\n      component: PRODUCT_EXPERIENCE.HELP_AND_SERVICE_LINKS.COMPONENTS.HELP_AND_SERVICE_COMPONENT,\n      action: link.label\n    }\"\n  >\n    <div class=\"card-block text-center\">\n      <div class=\"icon-32\">\n        <i\n          class=\"c8y-icon-duocolor\"\n          [c8yIcon]=\"link.icon\"\n        ></i>\n      </div>\n      <small class=\"text-muted\">{{ link.label | translate }}</small>\n    </div>\n  </a>\n</div>\n", dependencies: [{ kind: "ngmodule", type: CoreModule }, { kind: "directive", type: i1.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i3.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i1.ProductExperienceDirective, selector: "[c8yProductExperience]", inputs: ["actionName", "actionData", "inherit", "suppressDataOverriding"] }, { kind: "ngmodule", type: ReactiveFormsModule }, { kind: "pipe", type: i1.C8yTranslatePipe, name: "translate" }, { kind: "pipe", type: i3.AsyncPipe, name: "async" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: HelpAndServiceViewComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-help-and-service-view', standalone: true, imports: [CoreModule, ReactiveFormsModule], template: "<div class=\"card-group-block\">\n  <a\n    class=\"card card--btn\"\n    title=\"{{ link.label | translate }}\"\n    role=\"button\"\n    [attr.target]=\"link.target\"\n    *ngFor=\"let link of links$ | async\"\n    rel=\"noopener noreferrer\"\n    [href]=\"link.url ? link.url : '#'\"\n    (click)=\"openLink(link, $event)\"\n    c8yProductExperience\n    [actionName]=\"PRODUCT_EXPERIENCE.HELP_AND_SERVICE_LINKS.EVENTS.HELP_AND_SERVICE_LINKS\"\n    [actionData]=\"{\n      component: PRODUCT_EXPERIENCE.HELP_AND_SERVICE_LINKS.COMPONENTS.HELP_AND_SERVICE_COMPONENT,\n      action: link.label\n    }\"\n  >\n    <div class=\"card-block text-center\">\n      <div class=\"icon-32\">\n        <i\n          class=\"c8y-icon-duocolor\"\n          [c8yIcon]=\"link.icon\"\n        ></i>\n      </div>\n      <small class=\"text-muted\">{{ link.label | translate }}</small>\n    </div>\n  </a>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.DocsService }, { type: i1.AppStateService }, { type: i1.NavigatorService }, { type: i2.Router }] });

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

export { HelpAndServiceViewComponent };
//# sourceMappingURL=c8y-ngx-components-widgets-implementations-help-and-service-widget.mjs.map
