/**
 * @fileoverview added by tsickle
 * Generated from: lib/tap-protocol-configuration-form/tap-protocol-configuration-form.component.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
import { __assign } from "tslib";
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { SerialSettings } from '@iotize/tap/service/impl/target';
// import { debug } from '../../logger';
import { ALL_SETTINGS_FIELDS } from '../definitions';
import { ProtocolConfigurationService } from '../protocol-configuration.service';
/** @type {?} */
var TAG = 'TapProtocolConfiguration';
var TapProtocolConfigurationFormComponent = /** @class */ (function () {
    function TapProtocolConfigurationFormComponent(formBuilder, protocolConfigurationService) {
        this.protocolConfigurationService = protocolConfigurationService;
        this.customBaudRate = false;
        this.baudRates = [
            300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
            187500, 230400, 460800, 921600
        ];
        this.fields = ALL_SETTINGS_FIELDS;
        this.valueChanges = new EventEmitter();
        // TODO add validators
        /** @type {?} */
        var formConfig = {
            baudRate: ['', [Validators.required]],
            bitParity: [''],
            dataBits: [''],
            hardwareFlowControl: [''],
            handshakeDelimiter: [
                SerialSettings.HandshakeDelimiter.NONE
            ],
            physicalPort: [''],
            stopBits: [''],
            offsetFirstRegister: [''],
            // identification: [''],
            // ledStatus: [''],
            slave: [''],
            timeout: [''],
        };
        this.form = formBuilder.group(formConfig);
    }
    Object.defineProperty(TapProtocolConfigurationFormComponent.prototype, "values", {
        set: /**
         * @param {?} v
         * @return {?}
         */
        function (v) {
            if (v) {
                this.updateFormValue(v);
            }
        },
        enumerable: true,
        configurable: true
    });
    /**
     * @param {?} field
     * @return {?}
     */
    TapProtocolConfigurationFormComponent.prototype.hasField = /**
     * @param {?} field
     * @return {?}
     */
    function (field) {
        return this.fields.indexOf(field) >= 0;
    };
    Object.defineProperty(TapProtocolConfigurationFormComponent.prototype, "SerialSettings", {
        get: /**
         * @return {?}
         */
        function () {
            return SerialSettings;
        },
        enumerable: true,
        configurable: true
    });
    /**
     * @param {?} settings
     * @return {?}
     */
    TapProtocolConfigurationFormComponent.prototype.updateFormValue = /**
     * @param {?} settings
     * @return {?}
     */
    function (settings) {
        /** @type {?} */
        var newValues = __assign(__assign({}, settings.modbus), settings.serial);
        for (var key in newValues) {
            /** @type {?} */
            var field = this.form.get(key);
            if (field) {
                field.setValue(newValues[key], {
                    emitEvent: false
                });
            }
        }
    };
    /**
     * @return {?}
     */
    TapProtocolConfigurationFormComponent.prototype.ngOnInit = /**
     * @return {?}
     */
    function () {
        var _this = this;
        // debug(TAG, 'ngOnInit');
        /** @type {?} */
        var settings = this.protocolConfigurationService.pendingSettings;
        this.updateFormValue(settings);
        this.form.valueChanges
            .subscribe((/**
         * @param {?} newValue
         * @return {?}
         */
        function (newValue) {
            // debug(TAG, 'Protocol configuration changed:', newValue);
            /** @type {?} */
            var netProtocolConfig = {
                modbus: {
                    offsetFirstRegister: newValue.offsetFirstRegister,
                    slave: newValue.slave
                },
                serial: {
                    baudRate: newValue.baudRate,
                    bitParity: newValue.bitParity,
                    dataBits: newValue.dataBits,
                    handshakeDelimiter: newValue.handshakeDelimiter,
                    hardwareFlowControl: newValue.hardwareFlowControl,
                    physicalPort: newValue.physicalPort,
                    stopBits: newValue.stopBits,
                    timeout: newValue.timeout
                }
            };
            _this.valueChanges.emit(netProtocolConfig);
            _this.protocolConfigurationService.pendingSettings = netProtocolConfig;
        }));
        this.protocolConfigurationService
            .pendingSettings$
            .subscribe((/**
         * @param {?} newSettings
         * @return {?}
         */
        function (newSettings) {
            // debug(TAG, 'Refreshing form value', newSettings);
            if (_this.baudRates.indexOf(newSettings.serial.baudRate) === -1) {
                _this.customBaudRate = true;
            }
            _this.form.setValue(newSettings, {
                emitEvent: false
            });
        }));
    };
    TapProtocolConfigurationFormComponent.decorators = [
        { type: Component, args: [{
                    selector: 'tap-protocol-configuration-form',
                    template: "<form [formGroup]=\"form\">\r\n    <ion-item *ngIf=\"hasField('baudRate')\">\r\n        <ion-label>\r\n            Baud rate\r\n        </ion-label>\r\n        <!-- <ion-button fill=\"clear\" slot=\"end\" (click)=\"customBaudRate = !customBaudRate\">\r\n                    <ion-icon name=\"swap\"></ion-icon>\r\n                </ion-button> -->\r\n        <ion-input slot=\"end\" *ngIf=\"customBaudRate\" type=\"number\" [formControlName]=\"'baudRate'\"></ion-input>\r\n        <ion-select slot=\"end\" *ngIf=\"!customBaudRate\" [formControlName]=\"'baudRate'\">\r\n            <ion-select-option *ngFor=\"let baud of baudRates\" [value]=\"baud\">\r\n                {{baud}}\r\n                bauds\r\n            </ion-select-option>\r\n        </ion-select>\r\n    </ion-item>\r\n    <ion-item *ngIf=\"hasField('dataBits')\">\r\n        <ion-label>Data bits</ion-label>\r\n        <ion-select [formControlName]=\"'dataBits'\">\r\n            <ion-select-option *ngFor=\"let option of SerialSettings.DataBits | enum\" [value]=\"option.key\">\r\n                {{option.value | trim:'_'}}\r\n            </ion-select-option>\r\n        </ion-select>\r\n    </ion-item>\r\n    <ion-item *ngIf=\"hasField('stopBits')\">\r\n        <ion-label>Stop bit</ion-label>\r\n        <ion-select [formControlName]=\"'stopBits'\">\r\n            <ion-select-option *ngFor=\"let option of SerialSettings.StopBits | enum\" [value]=\"option.key\">{{option.value}}\r\n            </ion-select-option>\r\n        </ion-select>\r\n    </ion-item>\r\n    <ion-item *ngIf=\"hasField('bitParity')\">\r\n        <ion-label>Parity</ion-label>\r\n        <ion-select [formControlName]=\"'bitParity'\">\r\n            <ion-select-option *ngFor=\"let option of SerialSettings.BitParity | enum\" [value]=\"option.key\">\r\n                {{option.value}}\r\n            </ion-select-option>\r\n        </ion-select>\r\n    </ion-item>\r\n\r\n    <ion-item *ngIf=\"hasField('hardwareFlowControl')\">\r\n        <ion-label>Hardware flow control</ion-label>\r\n        <ion-select [formControlName]=\"'hardwareFlowControl'\">\r\n            <ion-select-option *ngFor=\"let option of SerialSettings.HardwareFlowControl | enum\" [value]=\"option.key\">\r\n                {{option.value}}\r\n            </ion-select-option>\r\n        </ion-select>\r\n    </ion-item>\r\n<!-- \r\n    <ion-item *ngIf=\"hasField('handshakeDelimiter')\">\r\n        <ion-label>Handshake delimiter</ion-label>\r\n        <ion-select [formControlName]=\"'handshakeDelimiter'\">\r\n            <ion-select-option *ngFor=\"let option of SerialSettings.HandshakeDelimiter | enum\" [value]=\"option.key\">\r\n                {{option.value}}</ion-select-option>\r\n        </ion-select>\r\n    </ion-item> -->\r\n\r\n    <!-- TAPNPASS -->\r\n    <ion-item *ngIf=\"hasField('slave')\">\r\n        <ion-label>Slave number</ion-label>\r\n        <ion-input slot=\"end\" type=\"number\" [formControlName]=\"'slave'\" ></ion-input>\r\n    </ion-item>\r\n\r\n    <ion-item *ngIf=\"hasField('physicalPort')\">\r\n        <ion-label>Physical port</ion-label>\r\n        <ion-select [formControlName]=\"'physicalPort'\">\r\n            <ion-select-option *ngFor=\"let option of SerialSettings.PhysicalPort | enum\" [value]=\"option.key\">\r\n                {{option.value}}</ion-select-option>\r\n        </ion-select>\r\n    </ion-item>\r\n\r\n    <ion-item *ngIf=\"hasField('timeout')\">\r\n        <ion-label>Timeout</ion-label>\r\n        <ion-input slot=\"end\" type=\"number\" [formControlName]=\"'timeout'\"></ion-input>\r\n    </ion-item>\r\n\r\n    <ion-item *ngIf=\"hasField('offsetFirstRegister')\">\r\n        <ion-label>Offset first register</ion-label>\r\n        <ion-select [formControlName]=\"'offsetFirstRegister'\">\r\n            <ion-select-option [value]=\"false\">No</ion-select-option>\r\n            <ion-select-option [value]=\"true\">Yes</ion-select-option>\r\n        </ion-select>\r\n    </ion-item>\r\n</form>\r\n",
                    styles: ["ion-input{text-align:right}"]
                }] }
    ];
    /** @nocollapse */
    TapProtocolConfigurationFormComponent.ctorParameters = function () { return [
        { type: FormBuilder },
        { type: ProtocolConfigurationService }
    ]; };
    TapProtocolConfigurationFormComponent.propDecorators = {
        customBaudRate: [{ type: Input }],
        baudRates: [{ type: Input }],
        fields: [{ type: Input }],
        values: [{ type: Input }],
        valueChanges: [{ type: Output }]
    };
    return TapProtocolConfigurationFormComponent;
}());
export { TapProtocolConfigurationFormComponent };
if (false) {
    /** @type {?} */
    TapProtocolConfigurationFormComponent.prototype.customBaudRate;
    /** @type {?} */
    TapProtocolConfigurationFormComponent.prototype.baudRates;
    /** @type {?} */
    TapProtocolConfigurationFormComponent.prototype.fields;
    /** @type {?} */
    TapProtocolConfigurationFormComponent.prototype.valueChanges;
    /** @type {?} */
    TapProtocolConfigurationFormComponent.prototype.form;
    /** @type {?} */
    TapProtocolConfigurationFormComponent.prototype.protocolConfigurationService;
}
//# sourceMappingURL=data:application/json;base64, 