import * as i0 from '@angular/core';
import { Injectable, NgModule } from '@angular/core';
import * as i1 from '@c8y/ngx-components';
import { Permissions, gettext, CoreModule, hookService, hookDataGridActionControls } from '@c8y/ngx-components';
import { isNil } from 'lodash-es';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

const REQUIRED_IDENTITY_ROLES = [
    Permissions.ROLE_IDENTITY_READ,
    Permissions.ROLE_IDENTITY_ADMIN
];
const REQUIRED_INVENTORY_ROLES = [
    Permissions.ROLE_INVENTORY_READ,
    Permissions.ROLE_INVENTORY_ADMIN
];
const REQUIRED_MANAGED_OBJECT_ROLES = [
    Permissions.ROLE_MANAGED_OBJECT_READ,
    Permissions.ROLE_MANAGED_OBJECT_ADMIN
];
class ReplaceDeviceService {
    constructor(permissions, breadcrumbService, drawerService) {
        this.permissions = permissions;
        this.breadcrumbService = breadcrumbService;
        this.drawerService = drawerService;
        this.destroyed$ = new Subject();
    }
    getButtonInfo() {
        return {
            title: gettext('Replace device'),
            icon: 'replace',
            label: gettext('Replace'),
            action: device => this.openDrawer(device)
        };
    }
    canShowButton(device) {
        return ((this.permissions.hasAllRoles(REQUIRED_INVENTORY_ROLES) ||
            this.permissions.hasAllRoles(REQUIRED_MANAGED_OBJECT_ROLES)) &&
            this.permissions.hasAllRoles(REQUIRED_IDENTITY_ROLES) &&
            !isNil(device.c8y_IsDevice) &&
            isNil(device.c8y_IsLwm2mDevice));
    }
    async openDrawer(deviceToReplace, closeCallback) {
        const { ReplaceDeviceWizardComponent } = await import('@c8y/ngx-components/replace-device/replace-device-wizard');
        const drawerRef = this.drawerService.openDrawer(ReplaceDeviceWizardComponent, {
            initialState: { deviceToReplace }
        });
        this.appendBreadcrumbs();
        drawerRef.onClosed$.pipe(takeUntil(this.destroyed$)).subscribe(() => {
            this.removeBreadcrumbs();
            if (typeof closeCallback === 'function') {
                closeCallback();
            }
        });
    }
    ngOnDestroy() {
        this.destroyed$.next();
        this.destroyed$.complete();
    }
    appendBreadcrumbs() {
        this.breadcrumbService.state.forEach(bc => (bc.items = [...bc.items, { label: gettext('Replace device'), path: undefined }]));
        this.breadcrumbService.refresh();
    }
    removeBreadcrumbs() {
        this.breadcrumbService.state.forEach(bc => bc.items?.pop());
        this.breadcrumbService.refresh();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceService, deps: [{ token: i1.Permissions }, { token: i1.BreadcrumbService }, { token: i1.BottomDrawerService }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceService, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }], ctorParameters: () => [{ type: i1.Permissions }, { type: i1.BreadcrumbService }, { type: i1.BottomDrawerService }] });

class ReplaceDeviceGridActionFactory {
    constructor(replaceDeviceService) {
        this.replaceDeviceService = replaceDeviceService;
    }
    get() {
        return {
            actionControls: {
                type: 'replace',
                icon: 'replace',
                showOnHover: true,
                text: gettext('Replace device'),
                priority: 500,
                callback: (mo, reload) => {
                    this.replaceDeviceService.openDrawer(mo, reload);
                },
                showIf: (mo) => this.replaceDeviceService.canShowButton(mo)
            },
            matchesGrid(_, context) {
                return ['sub-assets-grid', 'device-grid-all'].indexOf(context?.key) !== -1;
            }
        };
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceGridActionFactory, deps: [{ token: ReplaceDeviceService }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceGridActionFactory }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceGridActionFactory, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: ReplaceDeviceService }] });

class ReplaceDeviceModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceModule, imports: [CoreModule] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceModule, providers: [
            hookService('additionalDeviceStatusButtons', ReplaceDeviceService),
            hookDataGridActionControls(ReplaceDeviceGridActionFactory)
        ], imports: [CoreModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ReplaceDeviceModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CoreModule],
                    providers: [
                        hookService('additionalDeviceStatusButtons', ReplaceDeviceService),
                        hookDataGridActionControls(ReplaceDeviceGridActionFactory)
                    ]
                }]
        }] });

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

export { REQUIRED_IDENTITY_ROLES, REQUIRED_INVENTORY_ROLES, REQUIRED_MANAGED_OBJECT_ROLES, ReplaceDeviceModule, ReplaceDeviceService };
//# sourceMappingURL=c8y-ngx-components-replace-device.mjs.map
