import { Component, EventEmitter, Output } from '@angular/core';
import { AudioErrorCase } from '../models';
import { ConversionHelper } from '@tilde-nlp/ngx-common';
import * as i0 from "@angular/core";
import * as i1 from "../services/audio-record.service";
import * as i2 from "../../tld-common/services";
import * as i3 from "../services/tld-voice-input.service";
import * as i4 from "@tilde-nlp/ngx-common";
import * as i5 from "@angular/common";
import * as i6 from "@angular/material/tooltip";
import * as i7 from "@ngbracket/ngx-layout/flex";
import * as i8 from "@angular/material/button";
import * as i9 from "@ngx-translate/core";
export class TldRecordComponent {
    audioRecorderService;
    systemManagementService;
    configService;
    voiceProcessingService;
    alert;
    get isRecording() {
        return this.audioRecorderService.isRecording;
    }
    get recordTimeDate() { return this.audioRecorderService.recordTimeDate; }
    get isProcessing() { return this.voiceProcessingService.isProcessing; }
    hasAnyMicrophone;
    /**
     * Successfuly recorded BLOB
     */
    record = new EventEmitter();
    recordErrorSubscription;
    errorTranslationSubKeys = [
        'AUDIO',
    ];
    constructor(audioRecorderService, systemManagementService, configService, voiceProcessingService, alert) {
        this.audioRecorderService = audioRecorderService;
        this.systemManagementService = systemManagementService;
        this.configService = configService;
        this.voiceProcessingService = voiceProcessingService;
        this.alert = alert;
    }
    ngOnInit() {
        this.recordErrorSubscription = this.audioRecorderService.onError.subscribe((error) => {
            if (error === AudioErrorCase.MAX_SIZE_EXCEEDED) {
                this.alert.unhandeledError({ ErrorCode: error }, this.errorTranslationSubKeys, { maxSizeMb: ConversionHelper.bytesToMb(this.configService.audioConfig.audioMaxSizeBytes) });
                this.stopRecording();
            }
            else {
                this.alert.unhandeledError({ ErrorCode: error });
            }
        });
        this.findMic();
    }
    ngOnDestroy() {
        this.recordErrorSubscription.unsubscribe();
    }
    startRecording() {
        this.audioRecorderService.startRecording(this.configService.audioConfig.dataUpdateTimeFrame, this.configService.audioConfig.audioMaxSizeBytes);
    }
    stopRecording() {
        this.audioRecorderService.stopRecording().then((output) => {
            this.record.next(output);
            this.processVoiceRecord(output);
        });
    }
    cancel() {
        if (this.audioRecorderService.isRecording) {
            this.audioRecorderService.stopRecording();
        }
        if (this.voiceProcessingService.isProcessing) {
            this.voiceProcessingService.stopProcessing();
        }
    }
    processVoiceRecord(file) {
        const language = this.systemManagementService.getActiveSystemObj().sourceLanguage;
        this.voiceProcessingService.startTranscribtion({ file, language });
    }
    findMic() {
        navigator.mediaDevices.enumerateDevices().then((devices) => {
            this.hasAnyMicrophone = false;
            for (const device of devices) {
                if (device.kind === "audioinput") {
                    this.hasAnyMicrophone = true;
                    return;
                }
            }
        });
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: TldRecordComponent, deps: [{ token: i1.AudioRecordService }, { token: i2.TldTranslateSystemManagementService }, { token: i2.TldTranslateConfigService }, { token: i3.TldVoiceInputService }, { token: i4.AlertService }], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: TldRecordComponent, selector: "tld-record", outputs: { record: "record" }, ngImport: i0, template: "<div fxLayout=\"row\" fxFlex>\r\n    <ng-container *ngIf=\"!(isRecording || isProcessing)\">\r\n        <button [disabled]=\"!hasAnyMicrophone\" mat-button mat-icon-button fxLayoutAlign=\"center center\"\r\n            (click)=\"startRecording()\" color=\"accent\"  [attr.aria-label]=\"(hasAnyMicrophone?'AUDIO.START':'AUDIO.MIC_NOT_FOUND') | translate\">\r\n            <span class=\"material-icons\" *ngIf=\"hasAnyMicrophone\" color=\"accent\" \r\n            [matTooltip]=\"'AUDIO.START' | translate\">\r\n                mic\r\n            </span>\r\n            <span class=\"material-icons\" [matTooltip]=\"'AUDIO.MIC_NOT_FOUND' | translate\"\r\n                *ngIf=\"!hasAnyMicrophone\">\r\n                mic_off\r\n            </span>\r\n        </button>\r\n    </ng-container>\r\n    <ng-container *ngIf=\"isRecording || isProcessing\">\r\n        <div fxLayout=\"column\">\r\n            <button mat-button mat-icon-button fxLayoutAlign=\"center center\" [disabled]=\"!isRecording\"\r\n                (click)=\"stopRecording()\" color=\"accent\" [matTooltip]=\"'AUDIO.STOP' | translate\" [attr.aria-label]=\"'AUDIO.STOP' | translate\">\r\n                <span class=\"material-icons\">\r\n                    stop\r\n                </span>\r\n            </button>\r\n            <div class=\"tld-audio-timer\" fxLayoutAlign=\"center center\">\r\n                {{recordTimeDate | date:'mm:ss'}}\r\n            </div>\r\n        </div>\r\n        <div fxLayout=\"row\" fxFlex fxLayoutAlign=\"center center\">\r\n            <div fxFlex fxLayoutAlign=\"center center\" data-test-id=\"voice-input-info\">\r\n                <span *ngIf=\"isRecording\">{{'AUDIO.DESCRIBE_STATE_RECORDING' | translate}}</span>\r\n                <span *ngIf=\"isProcessing\">{{'AUDIO.DESCRIBE_STATE_PROCESSING' | translate}}</span>\r\n            </div>\r\n            <button mat-flat-button color=\"accent\" (click)=\"cancel()\">{{'AUDIO.CANCEL' | translate}}</button>\r\n        </div>\r\n    </ng-container>\r\n</div>", styles: ["button{border:1px solid rgba(0,0,0,.12)}.tld-audio-timer{font-size:10px}\n"], dependencies: [{ kind: "directive", type: i5.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i6.MatTooltip, selector: "[matTooltip]", inputs: ["matTooltipPosition", "matTooltipPositionAtOrigin", "matTooltipDisabled", "matTooltipShowDelay", "matTooltipHideDelay", "matTooltipTouchGestures", "matTooltip", "matTooltipClass"], exportAs: ["matTooltip"] }, { kind: "directive", type: i7.DefaultLayoutDirective, selector: "  [fxLayout], [fxLayout.xs], [fxLayout.sm], [fxLayout.md],  [fxLayout.lg], [fxLayout.xl], [fxLayout.lt-sm], [fxLayout.lt-md],  [fxLayout.lt-lg], [fxLayout.lt-xl], [fxLayout.gt-xs], [fxLayout.gt-sm],  [fxLayout.gt-md], [fxLayout.gt-lg]", inputs: ["fxLayout", "fxLayout.xs", "fxLayout.sm", "fxLayout.md", "fxLayout.lg", "fxLayout.xl", "fxLayout.lt-sm", "fxLayout.lt-md", "fxLayout.lt-lg", "fxLayout.lt-xl", "fxLayout.gt-xs", "fxLayout.gt-sm", "fxLayout.gt-md", "fxLayout.gt-lg"] }, { kind: "directive", type: i7.DefaultLayoutAlignDirective, selector: "  [fxLayoutAlign], [fxLayoutAlign.xs], [fxLayoutAlign.sm], [fxLayoutAlign.md],  [fxLayoutAlign.lg], [fxLayoutAlign.xl], [fxLayoutAlign.lt-sm], [fxLayoutAlign.lt-md],  [fxLayoutAlign.lt-lg], [fxLayoutAlign.lt-xl], [fxLayoutAlign.gt-xs], [fxLayoutAlign.gt-sm],  [fxLayoutAlign.gt-md], [fxLayoutAlign.gt-lg]", inputs: ["fxLayoutAlign", "fxLayoutAlign.xs", "fxLayoutAlign.sm", "fxLayoutAlign.md", "fxLayoutAlign.lg", "fxLayoutAlign.xl", "fxLayoutAlign.lt-sm", "fxLayoutAlign.lt-md", "fxLayoutAlign.lt-lg", "fxLayoutAlign.lt-xl", "fxLayoutAlign.gt-xs", "fxLayoutAlign.gt-sm", "fxLayoutAlign.gt-md", "fxLayoutAlign.gt-lg"] }, { kind: "directive", type: i7.DefaultFlexDirective, selector: "  [fxFlex], [fxFlex.xs], [fxFlex.sm], [fxFlex.md],  [fxFlex.lg], [fxFlex.xl], [fxFlex.lt-sm], [fxFlex.lt-md],  [fxFlex.lt-lg], [fxFlex.lt-xl], [fxFlex.gt-xs], [fxFlex.gt-sm],  [fxFlex.gt-md], [fxFlex.gt-lg]", inputs: ["fxFlex", "fxFlex.xs", "fxFlex.sm", "fxFlex.md", "fxFlex.lg", "fxFlex.xl", "fxFlex.lt-sm", "fxFlex.lt-md", "fxFlex.lt-lg", "fxFlex.lt-xl", "fxFlex.gt-xs", "fxFlex.gt-sm", "fxFlex.gt-md", "fxFlex.gt-lg"] }, { kind: "component", type: i8.MatButton, selector: "    button[mat-button], button[mat-raised-button], button[mat-flat-button],    button[mat-stroked-button]  ", exportAs: ["matButton"] }, { kind: "component", type: i8.MatIconButton, selector: "button[mat-icon-button]", exportAs: ["matButton"] }, { kind: "pipe", type: i5.DatePipe, name: "date" }, { kind: "pipe", type: i9.TranslatePipe, name: "translate" }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: TldRecordComponent, decorators: [{
            type: Component,
            args: [{ selector: 'tld-record', template: "<div fxLayout=\"row\" fxFlex>\r\n    <ng-container *ngIf=\"!(isRecording || isProcessing)\">\r\n        <button [disabled]=\"!hasAnyMicrophone\" mat-button mat-icon-button fxLayoutAlign=\"center center\"\r\n            (click)=\"startRecording()\" color=\"accent\"  [attr.aria-label]=\"(hasAnyMicrophone?'AUDIO.START':'AUDIO.MIC_NOT_FOUND') | translate\">\r\n            <span class=\"material-icons\" *ngIf=\"hasAnyMicrophone\" color=\"accent\" \r\n            [matTooltip]=\"'AUDIO.START' | translate\">\r\n                mic\r\n            </span>\r\n            <span class=\"material-icons\" [matTooltip]=\"'AUDIO.MIC_NOT_FOUND' | translate\"\r\n                *ngIf=\"!hasAnyMicrophone\">\r\n                mic_off\r\n            </span>\r\n        </button>\r\n    </ng-container>\r\n    <ng-container *ngIf=\"isRecording || isProcessing\">\r\n        <div fxLayout=\"column\">\r\n            <button mat-button mat-icon-button fxLayoutAlign=\"center center\" [disabled]=\"!isRecording\"\r\n                (click)=\"stopRecording()\" color=\"accent\" [matTooltip]=\"'AUDIO.STOP' | translate\" [attr.aria-label]=\"'AUDIO.STOP' | translate\">\r\n                <span class=\"material-icons\">\r\n                    stop\r\n                </span>\r\n            </button>\r\n            <div class=\"tld-audio-timer\" fxLayoutAlign=\"center center\">\r\n                {{recordTimeDate | date:'mm:ss'}}\r\n            </div>\r\n        </div>\r\n        <div fxLayout=\"row\" fxFlex fxLayoutAlign=\"center center\">\r\n            <div fxFlex fxLayoutAlign=\"center center\" data-test-id=\"voice-input-info\">\r\n                <span *ngIf=\"isRecording\">{{'AUDIO.DESCRIBE_STATE_RECORDING' | translate}}</span>\r\n                <span *ngIf=\"isProcessing\">{{'AUDIO.DESCRIBE_STATE_PROCESSING' | translate}}</span>\r\n            </div>\r\n            <button mat-flat-button color=\"accent\" (click)=\"cancel()\">{{'AUDIO.CANCEL' | translate}}</button>\r\n        </div>\r\n    </ng-container>\r\n</div>", styles: ["button{border:1px solid rgba(0,0,0,.12)}.tld-audio-timer{font-size:10px}\n"] }]
        }], ctorParameters: () => [{ type: i1.AudioRecordService }, { type: i2.TldTranslateSystemManagementService }, { type: i2.TldTranslateConfigService }, { type: i3.TldVoiceInputService }, { type: i4.AlertService }], propDecorators: { record: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 