import * as i0 from '@angular/core';
import { Injectable, Pipe, Inject, NgModule } from '@angular/core';
import { Subject, BehaviorSubject } from 'rxjs';
import { CommonModule } from '@angular/common';
import { shareReplay } from 'rxjs/operators';

class DefaultDashboardEventStreamService {
    // private _componentRefs: [IComponent, string][] = [];
    constructor() {
        this._events = new Subject();
        this.events = this._events.asObservable();
    }
    emit(data) {
        this._events.next(data);
    }
}
/** @nocollapse */ DefaultDashboardEventStreamService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DefaultDashboardEventStreamService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
/** @nocollapse */ DefaultDashboardEventStreamService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DefaultDashboardEventStreamService });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DefaultDashboardEventStreamService, decorators: [{
            type: Injectable
        }], ctorParameters: function () { return []; } });

const DATA_SOURCE_PROVIDER = 'DATA_SOURCE_PROVIDER';
const DASHBOARD_EVENT_STREAM_PROVIDER = 'DASHBOARD_EVENT_STREAM_PROVIDER';

class DataStreamIdToValueStreamPipe {
    constructor(dataSourceProvider) {
        this.dataSourceProvider = dataSourceProvider;
    }
    transform(streamId) {
        return this.dataSourceProvider.getStream(streamId);
    }
}
/** @nocollapse */ DataStreamIdToValueStreamPipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DataStreamIdToValueStreamPipe, deps: [{ token: DATA_SOURCE_PROVIDER }], target: i0.ɵɵFactoryTarget.Pipe });
/** @nocollapse */ DataStreamIdToValueStreamPipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "14.3.0", ngImport: i0, type: DataStreamIdToValueStreamPipe, name: "dashboardValue" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DataStreamIdToValueStreamPipe, decorators: [{
            type: Pipe,
            args: [{
                    name: 'dashboardValue',
                }]
        }], ctorParameters: function () { return [{ type: undefined, decorators: [{
                    type: Inject,
                    args: [DATA_SOURCE_PROVIDER]
                }] }]; } });

class DataStreamModule {
    static forRoot(settings) {
        return {
            ngModule: DataStreamModule,
            providers: DataStreamModule.createProviders(settings),
        };
    }
    static createProviders(settings) {
        return [
            {
                provide: DATA_SOURCE_PROVIDER,
                useExisting: settings.dataSourceProvider,
            },
            {
                provide: DASHBOARD_EVENT_STREAM_PROVIDER,
                useExisting: settings.dashboardEventStreamProvider,
            },
        ];
    }
}
/** @nocollapse */ DataStreamModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DataStreamModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
/** @nocollapse */ DataStreamModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.3.0", ngImport: i0, type: DataStreamModule, declarations: [DataStreamIdToValueStreamPipe], imports: [CommonModule], exports: [DataStreamIdToValueStreamPipe] });
/** @nocollapse */ DataStreamModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DataStreamModule, imports: [CommonModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DataStreamModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [DataStreamIdToValueStreamPipe],
                    exports: [DataStreamIdToValueStreamPipe],
                    imports: [CommonModule],
                }]
        }] });

class DefaultDataSourceProvider {
    constructor() {
        this._streamsChange = new BehaviorSubject({});
        console.log('DEBUG DefaultDataSourceProvider constructor');
    }
    getAvailableStreamIds() {
        return Object.keys(this._streamsChange.value);
    }
    /**
     * @deprecated
     * @param streamId
     * @returns
     */
    getEditableStream(streamId) {
        throw new Error('Method not implemented.');
    }
    getStream(streamId) {
        return this._getOrCreateStream(streamId).asObservable();
    }
    _getOrCreateStream(streamId) {
        const existingStream = this._streamsChange.value[streamId];
        if (!existingStream) {
            return this._createStream(streamId);
        }
        else {
            return existingStream;
        }
    }
    _createStream(streamId, value) {
        const streams = this._streamsChange.value;
        const newStream = arguments.length > 1
            ? new BehaviorSubject(value)
            : new BehaviorSubject(null); // TODO check convertion
        streams[streamId] = newStream;
        this._streamsChange.next(streams);
        return newStream;
    }
    notifyValue(streamId, value) {
        this._getOrCreateStream(streamId).next(value);
    }
}
/** @nocollapse */ DefaultDataSourceProvider.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DefaultDataSourceProvider, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
/** @nocollapse */ DefaultDataSourceProvider.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DefaultDataSourceProvider });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: DefaultDataSourceProvider, decorators: [{
            type: Injectable
        }], ctorParameters: function () { return []; } });

class LwM2MEditableDataStream {
    constructor(tap, config) {
        this.tap = tap;
        this.config = config;
        this._values = new Subject();
        // TODO error stream ?
        /**
         * Values observable
         */
        this.values = this._values.asObservable().pipe(shareReplay(1));
    }
    /**
     * Read value.
     * It may also trigger a value changed event
     */
    async read() {
        const result = await this.tap.lwm2m.execute(this.config.readCallFactory());
        const body = result.body();
        this._values.next(body);
        return body;
    }
    /**
     * Write given value
     * @param value
     */
    async write(value) {
        const result = await this.tap.lwm2m.execute(this.config.writeCallFactory(value));
        result.successful();
    }
}

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

export { DASHBOARD_EVENT_STREAM_PROVIDER, DATA_SOURCE_PROVIDER, DataStreamIdToValueStreamPipe, DataStreamModule, DefaultDashboardEventStreamService, DefaultDataSourceProvider, LwM2MEditableDataStream };
//# sourceMappingURL=iotize-ionic-data-stream.mjs.map
