import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Optional, Inject, NgModule } from '@angular/core';
import * as i2 from '@angular/common';
import { flatten, isUndefined, has } from 'lodash-es';
import { Subject } from 'rxjs';
import * as i1 from '@c8y/client';
import { hookGeneric } from '@c8y/ngx-components';

const baseUrl = 'devicecontrol/bulk/creation/';
/**
 * @deprecated Consider using the `hookListBulkType` function instead.
 */
const HOOK_LIST_BULK_TYPE = new InjectionToken('HOOK_LIST_BULK_TYPE');
/**
 * You can either provide a single `OperationType` as parameter:
 * ```typescript
 *  hookListBulkType(...)
 * ```
 *
 * Or an array to directly register multiple:
 * ```typescript
 *  hookListBulkType([...])
 * ```
 *
 * Or you provide an Service that implements `ExtensionFactory<OperationType>`
 * ```typescript
 *  export class MyListBulkTypeFactory implements ExtensionFactory<OperationType> {...}
 *  ...
 *  hookListBulkType(MyListBulkTypeFactory)
 * ```
 * A typed alternative to `HOOK_LIST_BULK_TYPE`.
 * @param type The `OperationType`'s or `ExtensionFactory` to be provided.
 * @returns An `Provider` to be provided in your module.
 */
function hookListBulkType(type, options) {
    return hookGeneric(type, HOOK_LIST_BULK_TYPE, options);
}
class BulkOperationsService {
    constructor(operationBulkService, operationService, inventoryService, location, bulkTypes) {
        this.operationBulkService = operationBulkService;
        this.operationService = operationService;
        this.inventoryService = inventoryService;
        this.location = location;
        this.DD_LOW_COUNT = 10;
        this.firmwareId = new Subject();
        this.bulkTypes = flatten(bulkTypes);
        this.bulkTypes = this.bulkTypes.map(type => {
            if (isUndefined(type.selected)) {
                type.selected = false;
            }
            return type;
        });
    }
    getBulkOperations(customFilter = {}) {
        const filter = {
            withTotalPages: true,
            withDeleted: true,
            pageSize: 50,
            ...customFilter
        };
        return this.operationBulkService.list(filter);
    }
    getBulkOperationById(bulkOperationId) {
        return this.operationBulkService.detail(bulkOperationId);
    }
    createBulkOperation(bulkOperation) {
        return this.operationBulkService.create(bulkOperation);
    }
    deleteBulkOperation(bulkOperationId) {
        return this.operationBulkService.delete(bulkOperationId);
    }
    updateBulkOperation(bulkOperation) {
        return this.operationBulkService.update(bulkOperation);
    }
    getOperation(id) {
        return this.operationService.detail(id);
    }
    returnToBulkOperationOverview() {
        this.location.back();
    }
    setBulkTypes(list) {
        this.bulkTypes = list;
    }
    getBulkTypes() {
        return this.bulkTypes;
    }
    setFirmwareId(id) {
        this.firmwareId.next(id);
    }
    createGroup(deviceQueryDataString) {
        const dynamicGroup = {
            name: 'Bulk operations group',
            type: 'c8y_DynamicGroup',
            c8y_IsDynamicGroup: { invisible: {} },
            c8y_DeviceQueryString: deviceQueryDataString
        };
        return this.inventoryService.create(dynamicGroup);
    }
    async scheduleBulkOperation(deviceQueryString, details) {
        const dynamicGroup = await this.createGroup(deviceQueryString);
        const bulkOperation = {
            groupId: dynamicGroup.data.id,
            operationPrototype: details.prototype,
            creationRamp: details.schedule.delayInSeconds,
            startDate: details.schedule.scheduledDate.toISOString(),
            note: details.note
        };
        await this.createBulkOperation(bulkOperation);
    }
    getSingleOperationsByStatus(status, bulkOperationId) {
        const filter = {
            withTotalPages: true,
            bulkOperationId,
            status: (status && status.toUpperCase()) || ''
        };
        return this.operationService.list(filter);
    }
    updateSingleOperation(partialUpdateObject) {
        return this.operationService.update(partialUpdateObject);
    }
    retrieveBulkOperationType(operation) {
        let type;
        this.bulkTypes.some(t => {
            if (t.fragments.some(fragment => has(operation, fragment))) {
                type = t.type;
                return true;
            }
        });
        return type;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BulkOperationsService, deps: [{ token: i1.OperationBulkService }, { token: i1.OperationService }, { token: i1.InventoryService }, { token: i2.Location }, { token: HOOK_LIST_BULK_TYPE, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BulkOperationsService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BulkOperationsService, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: i1.OperationBulkService }, { type: i1.OperationService }, { type: i1.InventoryService }, { type: i2.Location }, { type: Array, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [HOOK_LIST_BULK_TYPE]
                }] }] });

/**
 * This module provides a shared bulk operations service.
 */
class BulkOperationsServiceModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BulkOperationsServiceModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.13", ngImport: i0, type: BulkOperationsServiceModule }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BulkOperationsServiceModule, providers: [BulkOperationsService] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BulkOperationsServiceModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [],
                    providers: [BulkOperationsService]
                }]
        }] });

var BulkOperationType;
(function (BulkOperationType) {
    BulkOperationType["SOFTWARE"] = "software";
    BulkOperationType["FIRMWARE"] = "firmware";
    BulkOperationType["CONFIGURATION"] = "configuration";
    BulkOperationType["DEVICE_PROFILE"] = "device-profile";
})(BulkOperationType || (BulkOperationType = {}));

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

export { BulkOperationType, BulkOperationsService, BulkOperationsServiceModule, HOOK_LIST_BULK_TYPE, baseUrl, hookListBulkType };
//# sourceMappingURL=c8y-ngx-components-operations-bulk-operations-service.mjs.map
