import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import * as i1 from '@c8y/client';
import * as i2 from '@c8y/ngx-components';
import { BuiltInActionType, gettext, Status, NavigatorNode } from '@c8y/ngx-components';
import { agentName } from '@c8y/ngx-components/protocol-lwm2m/model';
import { Lwm2mClientService } from '@c8y/ngx-components/protocol-lwm2m/services';
import * as i3 from '@ngx-translate/core';
import * as i1$1 from '@c8y/ngx-components/protocol-lwm2m/guards';

class Lwm2mDeviceGridFactory extends Lwm2mClientService {
    constructor(client, tenantService, modal, translateService, alertService) {
        super(client);
        this.tenantService = tenantService;
        this.modal = modal;
        this.translateService = translateService;
        this.alertService = alertService;
        this.listUrl = 'deviceRegistration';
    }
    get() {
        return {
            actionControls: {
                type: BuiltInActionType.Delete,
                priority: -Infinity,
                showIf: (mo) => super.isLwm2mDevice(mo),
                callback: async (mo, reload) => {
                    await this.onDelete(mo);
                    reload();
                }
            },
            matchesGrid: (_, context) => {
                return context?.key === 'device-grid-all';
            }
        };
    }
    async onDelete(device) {
        if (this.tenantService.isMicroserviceSubscribedInCurrentTenant(agentName)) {
            try {
                await this.modal.confirm(gettext('Delete LWM2M device'), this.translateService.instant(gettext(`You are about to delete device "{{ name }}". Do you want to proceed?`), device), Status.DANGER, { ok: gettext('Delete'), cancel: gettext('Cancel') });
                await this.delete(device);
                this.alertService.success(gettext('Device deleted.'));
                return Promise.resolve();
            }
            catch (ex) {
                if (ex) {
                    this.alertService.addServerFailure(ex);
                }
                return Promise.reject();
            }
        }
        else {
            this.alertService.danger(this.translateService.instant(gettext(`You can't delete this device. The service "{{ name }}" is not subscribed.`), { name: agentName }));
            return Promise.reject();
        }
    }
    delete(entityOrId) {
        return super.delete(entityOrId);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: Lwm2mDeviceGridFactory, deps: [{ token: i1.FetchClient }, { token: i2.TenantUiService }, { token: i2.ModalService }, { token: i3.TranslateService }, { token: i2.AlertService }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: Lwm2mDeviceGridFactory, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: Lwm2mDeviceGridFactory, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root'
                }]
        }], ctorParameters: () => [{ type: i1.FetchClient }, { type: i2.TenantUiService }, { type: i2.ModalService }, { type: i3.TranslateService }, { type: i2.AlertService }] });

class LWM2MPostOpertaionsNavigationFactory {
    constructor(guard) {
        this.guard = guard;
        this.node = new NavigatorNode({
            label: gettext('LWM2M post-operations'),
            path: 'post-operations',
            icon: 'terminal',
            parent: {
                label: gettext('Device types'),
                icon: 'c8y-device-protocols'
            },
            priority: 0
        });
    }
    get() {
        return this.guard.canActivate() ? this.node : [];
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: LWM2MPostOpertaionsNavigationFactory, deps: [{ token: i1$1.Lwm2mMicroserviceGuard }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: LWM2MPostOpertaionsNavigationFactory }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: LWM2MPostOpertaionsNavigationFactory, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: i1$1.Lwm2mMicroserviceGuard }] });

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

export { LWM2MPostOpertaionsNavigationFactory, Lwm2mDeviceGridFactory };
//# sourceMappingURL=c8y-ngx-components-protocol-lwm2m-factories.mjs.map
