import * as i0 from '@angular/core';
import { Component, Optional, Input, NgModule } from '@angular/core';
import * as i1 from '@c8y/ngx-components';
import { DynamicComponentAlert, gettext, CoreModule } from '@c8y/ngx-components';
import * as i2 from '@c8y/client';
import * as i3 from '@ngx-translate/core';
import { BehaviorSubject } from 'rxjs';
import { map, shareReplay } from 'rxjs/operators';
import * as i4 from '@c8y/ngx-components/context-dashboard';
import * as i6 from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import * as i5 from '@angular/common';

class DeviceControlMessageWidgetViewComponent {
    constructor(alert, operation, translate, dashboard) {
        this.alert = alert;
        this.operation = operation;
        this.translate = translate;
        this.dashboard = dashboard;
        this.messageToBeSent = '';
        this.operationAttribute = 'c8y_Message';
        this.currentDevice = new BehaviorSubject(null);
        this.operationSupportedByDevice$ = this.currentDevice.pipe(map(device => {
            if (!device) {
                return false;
            }
            const supportedOperations = device.c8y_SupportedOperations;
            if (supportedOperations && Array.isArray(supportedOperations)) {
                return supportedOperations.includes(this.operationAttribute);
            }
            return false;
        }), shareReplay({ refCount: true, bufferSize: 1 }));
    }
    ngOnInit() {
        if (this.dashboard?.isDeviceTypeDashboard && this.dashboard?.context?.id) {
            this.currentDevice.next(this.dashboard.context);
        }
        this.operationSupportedByDeviceSubscription = this.operationSupportedByDevice$.subscribe(supported => {
            if (!supported) {
                this.alerts.addAlerts(new DynamicComponentAlert({
                    type: 'warning',
                    text: gettext('Operation not supported by this device')
                }));
            }
            else {
                this.alerts.clear();
            }
        });
    }
    ngOnDestroy() {
        this.operationSupportedByDeviceSubscription?.unsubscribe();
    }
    ngOnChanges() {
        if (!this.dashboard?.isDeviceTypeDashboard && this.config.device) {
            this.currentDevice.next(this.config.device);
        }
    }
    async sendMessage() {
        const msg = this.messageToBeSent;
        const operationDescription = this.translate.instant(gettext('Send message "{{msg}}"'), { msg });
        try {
            await this.operation.create({
                deviceId: this.currentDevice.value?.id,
                description: operationDescription,
                [this.operationAttribute]: { text: msg }
            });
            this.alert.success(gettext('Message will be sent.'));
            this.messageToBeSent = '';
        }
        catch (e) {
            this.alert.addServerFailure(e);
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: DeviceControlMessageWidgetViewComponent, deps: [{ token: i1.AlertService }, { token: i2.OperationService }, { token: i3.TranslateService }, { token: i4.ContextDashboardComponent, optional: true }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: DeviceControlMessageWidgetViewComponent, isStandalone: true, selector: "c8y-device-control-message-widget-view", inputs: { config: "config" }, usesOnChanges: true, ngImport: i0, template: "<div *ngIf=\"operationSupportedByDevice$ | async\" class=\"input-group p-16\">\n  <input\n    type=\"text\"\n    class=\"form-control\"\n    data-cy=\"c8y-device-control-message-widget-view--message-textbox\"\n    [(ngModel)]=\"messageToBeSent\"\n    placeholder=\"{{ 'Message' | translate }}\"\n  />\n  <span class=\"input-group-btn\">\n    <button\n      title=\"{{ 'Send' | translate }}\"\n      class=\"btn btn-primary\"\n      data-cy=\"c8y-device-control-message-widget-view--send-button\"\n      (click)=\"sendMessage()\"\n      [disabled]=\"!messageToBeSent\"\n      translate\n    >\n      Send\n    </button>\n  </span>\n</div>\n", dependencies: [{ kind: "ngmodule", type: CoreModule }, { kind: "pipe", type: i1.C8yTranslatePipe, name: "translate" }, { kind: "directive", type: i1.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i5.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "pipe", type: i5.AsyncPipe, name: "async" }, { kind: "directive", type: i6.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i6.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i6.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "ngmodule", type: ReactiveFormsModule }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: DeviceControlMessageWidgetViewComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-device-control-message-widget-view', standalone: true, imports: [CoreModule, ReactiveFormsModule], template: "<div *ngIf=\"operationSupportedByDevice$ | async\" class=\"input-group p-16\">\n  <input\n    type=\"text\"\n    class=\"form-control\"\n    data-cy=\"c8y-device-control-message-widget-view--message-textbox\"\n    [(ngModel)]=\"messageToBeSent\"\n    placeholder=\"{{ 'Message' | translate }}\"\n  />\n  <span class=\"input-group-btn\">\n    <button\n      title=\"{{ 'Send' | translate }}\"\n      class=\"btn btn-primary\"\n      data-cy=\"c8y-device-control-message-widget-view--send-button\"\n      (click)=\"sendMessage()\"\n      [disabled]=\"!messageToBeSent\"\n      translate\n    >\n      Send\n    </button>\n  </span>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.AlertService }, { type: i2.OperationService }, { type: i3.TranslateService }, { type: i4.ContextDashboardComponent, decorators: [{
                    type: Optional
                }] }], propDecorators: { config: [{
                type: Input
            }] } });

/**
 * @deprecated
 */
class DeviceControlMessageWidgetModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: DeviceControlMessageWidgetModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.13", ngImport: i0, type: DeviceControlMessageWidgetModule, imports: [DeviceControlMessageWidgetViewComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: DeviceControlMessageWidgetModule, imports: [DeviceControlMessageWidgetViewComponent] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: DeviceControlMessageWidgetModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [DeviceControlMessageWidgetViewComponent]
                }]
        }] });

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

export { DeviceControlMessageWidgetModule, DeviceControlMessageWidgetViewComponent };
//# sourceMappingURL=c8y-ngx-components-widgets-implementations-device-control-message.mjs.map
