(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/forms'), require('@iotize/tap/service/impl/target'), require('@iotize/common/utility'), require('rxjs'), require('@iotize/ionic'), require('@angular/common'), require('@ionic/angular')) :
    typeof define === 'function' && define.amd ? define('@iotize/ionic/target/common', ['exports', '@angular/core', '@angular/forms', '@iotize/tap/service/impl/target', '@iotize/common/utility', 'rxjs', '@iotize/ionic', '@angular/common', '@ionic/angular'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.iotize = global.iotize || {}, global.iotize.ionic = global.iotize.ionic || {}, global.iotize.ionic.target = global.iotize.ionic.target || {}, global.iotize.ionic.target.common = {}), global.ng.core, global.ng.forms, global.target, global.utility, global.rxjs, global.iotize.ionic, global.ng.common, global.i4));
}(this, (function (exports, i0, i1$1, target, utility, rxjs, i1, i3, i4) { 'use strict';

    /*! *****************************************************************************
    Copyright (c) Microsoft Corporation.

    Permission to use, copy, modify, and/or distribute this software for any
    purpose with or without fee is hereby granted.

    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
    REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
    INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
    LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
    OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
    PERFORMANCE OF THIS SOFTWARE.
    ***************************************************************************** */
    /* global Reflect, Promise */
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b)
                if (Object.prototype.hasOwnProperty.call(b, p))
                    d[p] = b[p]; };
        return extendStatics(d, b);
    };
    function __extends(d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    }
    var __assign = function () {
        __assign = Object.assign || function __assign(t) {
            for (var s, i = 1, n = arguments.length; i < n; i++) {
                s = arguments[i];
                for (var p in s)
                    if (Object.prototype.hasOwnProperty.call(s, p))
                        t[p] = s[p];
            }
            return t;
        };
        return __assign.apply(this, arguments);
    };
    function __rest(s, e) {
        var t = {};
        for (var p in s)
            if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
                t[p] = s[p];
        if (s != null && typeof Object.getOwnPropertySymbols === "function")
            for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
                if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                    t[p[i]] = s[p[i]];
            }
        return t;
    }
    function __decorate(decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
            r = Reflect.decorate(decorators, target, key, desc);
        else
            for (var i = decorators.length - 1; i >= 0; i--)
                if (d = decorators[i])
                    r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    }
    function __param(paramIndex, decorator) {
        return function (target, key) { decorator(target, key, paramIndex); };
    }
    function __metadata(metadataKey, metadataValue) {
        if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
            return Reflect.metadata(metadataKey, metadataValue);
    }
    function __awaiter(thisArg, _arguments, P, generator) {
        function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
        return new (P || (P = Promise))(function (resolve, reject) {
            function fulfilled(value) { try {
                step(generator.next(value));
            }
            catch (e) {
                reject(e);
            } }
            function rejected(value) { try {
                step(generator["throw"](value));
            }
            catch (e) {
                reject(e);
            } }
            function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
            step((generator = generator.apply(thisArg, _arguments || [])).next());
        });
    }
    function __generator(thisArg, body) {
        var _ = { label: 0, sent: function () { if (t[0] & 1)
                throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
        return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
        function verb(n) { return function (v) { return step([n, v]); }; }
        function step(op) {
            if (f)
                throw new TypeError("Generator is already executing.");
            while (_)
                try {
                    if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)
                        return t;
                    if (y = 0, t)
                        op = [op[0] & 2, t.value];
                    switch (op[0]) {
                        case 0:
                        case 1:
                            t = op;
                            break;
                        case 4:
                            _.label++;
                            return { value: op[1], done: false };
                        case 5:
                            _.label++;
                            y = op[1];
                            op = [0];
                            continue;
                        case 7:
                            op = _.ops.pop();
                            _.trys.pop();
                            continue;
                        default:
                            if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
                                _ = 0;
                                continue;
                            }
                            if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
                                _.label = op[1];
                                break;
                            }
                            if (op[0] === 6 && _.label < t[1]) {
                                _.label = t[1];
                                t = op;
                                break;
                            }
                            if (t && _.label < t[2]) {
                                _.label = t[2];
                                _.ops.push(op);
                                break;
                            }
                            if (t[2])
                                _.ops.pop();
                            _.trys.pop();
                            continue;
                    }
                    op = body.call(thisArg, _);
                }
                catch (e) {
                    op = [6, e];
                    y = 0;
                }
                finally {
                    f = t = 0;
                }
            if (op[0] & 5)
                throw op[1];
            return { value: op[0] ? op[1] : void 0, done: true };
        }
    }
    var __createBinding = Object.create ? (function (o, m, k, k2) {
        if (k2 === undefined)
            k2 = k;
        Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
    }) : (function (o, m, k, k2) {
        if (k2 === undefined)
            k2 = k;
        o[k2] = m[k];
    });
    function __exportStar(m, o) {
        for (var p in m)
            if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p))
                __createBinding(o, m, p);
    }
    function __values(o) {
        var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
        if (m)
            return m.call(o);
        if (o && typeof o.length === "number")
            return {
                next: function () {
                    if (o && i >= o.length)
                        o = void 0;
                    return { value: o && o[i++], done: !o };
                }
            };
        throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
    }
    function __read(o, n) {
        var m = typeof Symbol === "function" && o[Symbol.iterator];
        if (!m)
            return o;
        var i = m.call(o), r, ar = [], e;
        try {
            while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
                ar.push(r.value);
        }
        catch (error) {
            e = { error: error };
        }
        finally {
            try {
                if (r && !r.done && (m = i["return"]))
                    m.call(i);
            }
            finally {
                if (e)
                    throw e.error;
            }
        }
        return ar;
    }
    /** @deprecated */
    function __spread() {
        for (var ar = [], i = 0; i < arguments.length; i++)
            ar = ar.concat(__read(arguments[i]));
        return ar;
    }
    /** @deprecated */
    function __spreadArrays() {
        for (var s = 0, i = 0, il = arguments.length; i < il; i++)
            s += arguments[i].length;
        for (var r = Array(s), k = 0, i = 0; i < il; i++)
            for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
                r[k] = a[j];
        return r;
    }
    function __spreadArray(to, from, pack) {
        if (pack || arguments.length === 2)
            for (var i = 0, l = from.length, ar; i < l; i++) {
                if (ar || !(i in from)) {
                    if (!ar)
                        ar = Array.prototype.slice.call(from, 0, i);
                    ar[i] = from[i];
                }
            }
        return to.concat(ar || Array.prototype.slice.call(from));
    }
    function __await(v) {
        return this instanceof __await ? (this.v = v, this) : new __await(v);
    }
    function __asyncGenerator(thisArg, _arguments, generator) {
        if (!Symbol.asyncIterator)
            throw new TypeError("Symbol.asyncIterator is not defined.");
        var g = generator.apply(thisArg, _arguments || []), i, q = [];
        return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
        function verb(n) { if (g[n])
            i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
        function resume(n, v) { try {
            step(g[n](v));
        }
        catch (e) {
            settle(q[0][3], e);
        } }
        function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
        function fulfill(value) { resume("next", value); }
        function reject(value) { resume("throw", value); }
        function settle(f, v) { if (f(v), q.shift(), q.length)
            resume(q[0][0], q[0][1]); }
    }
    function __asyncDelegator(o) {
        var i, p;
        return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
        function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
    }
    function __asyncValues(o) {
        if (!Symbol.asyncIterator)
            throw new TypeError("Symbol.asyncIterator is not defined.");
        var m = o[Symbol.asyncIterator], i;
        return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
        function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
        function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); }
    }
    function __makeTemplateObject(cooked, raw) {
        if (Object.defineProperty) {
            Object.defineProperty(cooked, "raw", { value: raw });
        }
        else {
            cooked.raw = raw;
        }
        return cooked;
    }
    ;
    var __setModuleDefault = Object.create ? (function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v });
    }) : function (o, v) {
        o["default"] = v;
    };
    function __importStar(mod) {
        if (mod && mod.__esModule)
            return mod;
        var result = {};
        if (mod != null)
            for (var k in mod)
                if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
                    __createBinding(result, mod, k);
        __setModuleDefault(result, mod);
        return result;
    }
    function __importDefault(mod) {
        return (mod && mod.__esModule) ? mod : { default: mod };
    }
    function __classPrivateFieldGet(receiver, state, kind, f) {
        if (kind === "a" && !f)
            throw new TypeError("Private accessor was defined without a getter");
        if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
            throw new TypeError("Cannot read private member from an object whose class did not declare it");
        return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
    }
    function __classPrivateFieldSet(receiver, state, value, kind, f) {
        if (kind === "m")
            throw new TypeError("Private method is not writable");
        if (kind === "a" && !f)
            throw new TypeError("Private accessor was defined without a setter");
        if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
            throw new TypeError("Cannot write private member to an object whose class did not declare it");
        return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
    }

    var SERIAL_SETTINGS_FIELDS = [
        'baudRate',
        'dataBits',
        'bitParity',
        'stopBits',
        'hardwareFlowControl',
        'handshakeDelimiter',
        'timeout',
    ];
    var MODBUS_SETTINGS_FIELDS = __spread(SERIAL_SETTINGS_FIELDS, ['offsetFirstRegister', 'slave']);
    var TAPNPASS_SETTINGS_FIELDS = ['physicalPort'];
    var ALL_SETTINGS_FIELDS = __spread(SERIAL_SETTINGS_FIELDS, MODBUS_SETTINGS_FIELDS);
    var ProtocolConfigurationError = /** @class */ (function (_super) {
        __extends(ProtocolConfigurationError, _super);
        function ProtocolConfigurationError(msg, cause) {
            var _this = _super.call(this, msg) || this;
            _this.cause = cause;
            _this.code = 'ProtocolConfigurationError';
            return _this;
        }
        ProtocolConfigurationError.tapResponseError = function (err, msg) {
            var fullMsg = msg + " - " + err.message;
            return new ProtocolConfigurationError(fullMsg, err);
        };
        return ProtocolConfigurationError;
    }(Error));

    var TAG = 'SerialSettings';
    var ProtocolConfigurationService = /** @class */ (function () {
        function ProtocolConfigurationService(tapService) {
            this.tapService = tapService;
            this._s3pProtocolConfiguration = {
                mode: target.TargetS3PProtocolConfiguration.Mode.INDEXED,
                delay: 1,
            };
            this._defaultSettings = {
                serial: {
                    physicalPort: target.SerialSettings.PhysicalPort.AUTO,
                    stopBits: target.SerialSettings.StopBits.ONE,
                    bitParity: target.SerialSettings.BitParity.NONE,
                    dataBits: target.SerialSettings.DataBits._8,
                    hardwareFlowControl: target.SerialSettings.HardwareFlowControl.NONE,
                    handshakeDelimiter: target.SerialSettings.HandshakeDelimiter.NONE,
                    timeout: 50,
                    baudRate: 187500,
                },
                modbus: {
                    offsetFirstRegister: false,
                    slave: 0,
                },
            };
            this.pendingSettings$ = new rxjs.BehaviorSubject(this._defaultSettings); // displayed settings
            this.loading = new rxjs.BehaviorSubject(false);
            this.errors = new rxjs.Subject();
            this._tapSettings = undefined;
        }
        Object.defineProperty(ProtocolConfigurationService.prototype, "pendingSettings", {
            get: function () {
                return this.pendingSettings$.value;
            },
            set: function (settings) {
                this.pendingSettings$.next(settings);
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(ProtocolConfigurationService.prototype, "tap", {
            get: function () {
                return this.tapService.tap;
            },
            enumerable: false,
            configurable: true
        });
        ProtocolConfigurationService.prototype.refresh = function () {
            return __awaiter(this, void 0, void 0, function () {
                var tapSettings, err_1, error;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            this.loading.next(true);
                            _a.label = 1;
                        case 1:
                            _a.trys.push([1, 3, , 4]);
                            return [4 /*yield*/, this.tap.service.target.getSerialModbusConfiguration()];
                        case 2:
                            tapSettings = (_a.sent()).body();
                            this._tapSettings = tapSettings;
                            this.pendingSettings$.next(tapSettings);
                            // debug(TAG, 'Refreshed settings: ', this.pendingSettings);
                            this.loading.next(false);
                            return [3 /*break*/, 4];
                        case 3:
                            err_1 = _a.sent();
                            error = ProtocolConfigurationError.tapResponseError(err_1, 'Failed reading serial settings from Tap');
                            this.loading.next(false);
                            this.onError(error);
                            throw error;
                        case 4: return [2 /*return*/];
                    }
                });
            });
        };
        ProtocolConfigurationService.prototype.applyChanges = function (temporary, reconnectTarget) {
            if (temporary === void 0) { temporary = true; }
            if (reconnectTarget === void 0) { reconnectTarget = true; }
            return __awaiter(this, void 0, void 0, function () {
                var error_1;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            this.loading.next(true);
                            _a.label = 1;
                        case 1:
                            _a.trys.push([1, 5, , 6]);
                            return [4 /*yield*/, this.setSerialModbusConfiguration(temporary)];
                        case 2:
                            _a.sent();
                            if (!reconnectTarget) return [3 /*break*/, 4];
                            return [4 /*yield*/, this.reconnectTarget()];
                        case 3:
                            _a.sent();
                            _a.label = 4;
                        case 4:
                            this._tapSettings = Object.assign({}, this.pendingSettings);
                            this.loading.next(false);
                            return [3 /*break*/, 6];
                        case 5:
                            error_1 = _a.sent();
                            this.loading.next(false);
                            this.onError(error_1);
                            throw error_1;
                        case 6: return [2 /*return*/];
                    }
                });
            });
        };
        ProtocolConfigurationService.prototype.discardChanges = function () {
            var prevSettings = this._tapSettings
                ? this._tapSettings
                : this._defaultSettings;
            this.pendingSettings$.next(prevSettings);
        };
        ProtocolConfigurationService.prototype.hasChanges = function () {
            if (!this._tapSettings) {
                return true;
            }
            return !utility.deepEqual(this.pendingSettings, this._tapSettings);
        };
        ProtocolConfigurationService.prototype.setSerialModbusConfiguration = function (temporary) {
            if (temporary === void 0) { temporary = true; }
            return __awaiter(this, void 0, void 0, function () {
                var err_2;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            _a.trys.push([0, 5, , 6]);
                            if (!temporary) return [3 /*break*/, 2];
                            return [4 /*yield*/, this.tap.service.target.setSerialModbusConfiguration(this.pendingSettings)];
                        case 1:
                            (_a.sent()).successful();
                            return [3 /*break*/, 4];
                        case 2: return [4 /*yield*/, this.tap.service.target.putSerialModbusConfiguration(this.pendingSettings)];
                        case 3:
                            (_a.sent()).successful();
                            _a.label = 4;
                        case 4: return [3 /*break*/, 6];
                        case 5:
                            err_2 = _a.sent();
                            throw ProtocolConfigurationError.tapResponseError(err_2, 'Failed writing serial settings to Tap');
                        case 6: return [2 /*return*/];
                    }
                });
            });
        };
        ProtocolConfigurationService.prototype.reconnectTarget = function () {
            return __awaiter(this, void 0, void 0, function () {
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0: return [4 /*yield*/, this.tap.service.target.disconnect()];
                        case 1:
                            _a.sent();
                            return [4 /*yield*/, this.tap.service.target.connect()];
                        case 2:
                            _a.sent();
                            return [2 /*return*/];
                    }
                });
            });
        };
        ProtocolConfigurationService.prototype.onError = function (error) {
            this.errors.next(error);
        };
        return ProtocolConfigurationService;
    }());
    /** @nocollapse */ ProtocolConfigurationService.ɵfac = function ProtocolConfigurationService_Factory(t) { return new (t || ProtocolConfigurationService)(i0.ɵɵinject(i1.CurrentDeviceService)); };
    /** @nocollapse */ ProtocolConfigurationService.ɵprov = i0.ɵɵdefineInjectable({ token: ProtocolConfigurationService, factory: ProtocolConfigurationService.ɵfac, providedIn: 'root' });
    (function () {
        (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ProtocolConfigurationService, [{
                type: i0.Injectable,
                args: [{
                        providedIn: 'root',
                    }]
            }], function () { return [{ type: i1.CurrentDeviceService }]; }, null);
    })();

    function TapProtocolConfigurationFormComponent_ion_item_1_ion_input_3_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelement(0, "ion-input", 4);
        }
        if (rf & 2) {
            i0.ɵɵproperty("formControlName", "baudRate");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_1_ion_select_4_ion_select_option_1_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-select-option", 7);
            i0.ɵɵtext(1);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var baud_r12 = ctx.$implicit;
            i0.ɵɵproperty("value", baud_r12);
            i0.ɵɵadvance(1);
            i0.ɵɵtextInterpolate1(" ", baud_r12, " bauds ");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_1_ion_select_4_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-select", 5);
            i0.ɵɵtemplate(1, TapProtocolConfigurationFormComponent_ion_item_1_ion_select_4_ion_select_option_1_Template, 2, 2, "ion-select-option", 6);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var ctx_r10 = i0.ɵɵnextContext(2);
            i0.ɵɵproperty("formControlName", "baudRate");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngForOf", ctx_r10.baudRates);
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_1_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, " Baud rate ");
            i0.ɵɵelementEnd();
            i0.ɵɵtemplate(3, TapProtocolConfigurationFormComponent_ion_item_1_ion_input_3_Template, 1, 1, "ion-input", 2);
            i0.ɵɵtemplate(4, TapProtocolConfigurationFormComponent_ion_item_1_ion_select_4_Template, 2, 2, "ion-select", 3);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var ctx_r0 = i0.ɵɵnextContext();
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("ngIf", ctx_r0.customBaudRate);
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngIf", !ctx_r0.customBaudRate);
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_2_ion_select_option_4_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-select-option", 7);
            i0.ɵɵtext(1);
            i0.ɵɵpipe(2, "trim");
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var option_r14 = ctx.$implicit;
            i0.ɵɵproperty("value", option_r14.key);
            i0.ɵɵadvance(1);
            i0.ɵɵtextInterpolate1(" ", i0.ɵɵpipeBind2(2, 2, option_r14.value, "_"), " ");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_2_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Data bits");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(3, "ion-select", 8);
            i0.ɵɵtemplate(4, TapProtocolConfigurationFormComponent_ion_item_2_ion_select_option_4_Template, 3, 5, "ion-select-option", 6);
            i0.ɵɵpipe(5, "enum");
            i0.ɵɵelementEnd();
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var ctx_r1 = i0.ɵɵnextContext();
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "dataBits");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngForOf", i0.ɵɵpipeBind1(5, 2, ctx_r1.SerialSettings.DataBits));
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_3_ion_select_option_4_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-select-option", 7);
            i0.ɵɵtext(1);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var option_r16 = ctx.$implicit;
            i0.ɵɵproperty("value", option_r16.key);
            i0.ɵɵadvance(1);
            i0.ɵɵtextInterpolate1("", option_r16.value, " ");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_3_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Stop bit");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(3, "ion-select", 8);
            i0.ɵɵtemplate(4, TapProtocolConfigurationFormComponent_ion_item_3_ion_select_option_4_Template, 2, 2, "ion-select-option", 6);
            i0.ɵɵpipe(5, "enum");
            i0.ɵɵelementEnd();
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var ctx_r2 = i0.ɵɵnextContext();
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "stopBits");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngForOf", i0.ɵɵpipeBind1(5, 2, ctx_r2.SerialSettings.StopBits));
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_4_ion_select_option_4_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-select-option", 7);
            i0.ɵɵtext(1);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var option_r18 = ctx.$implicit;
            i0.ɵɵproperty("value", option_r18.key);
            i0.ɵɵadvance(1);
            i0.ɵɵtextInterpolate1(" ", option_r18.value, " ");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_4_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Parity");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(3, "ion-select", 8);
            i0.ɵɵtemplate(4, TapProtocolConfigurationFormComponent_ion_item_4_ion_select_option_4_Template, 2, 2, "ion-select-option", 6);
            i0.ɵɵpipe(5, "enum");
            i0.ɵɵelementEnd();
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var ctx_r3 = i0.ɵɵnextContext();
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "bitParity");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngForOf", i0.ɵɵpipeBind1(5, 2, ctx_r3.SerialSettings.BitParity));
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_5_ion_select_option_4_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-select-option", 7);
            i0.ɵɵtext(1);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var option_r20 = ctx.$implicit;
            i0.ɵɵproperty("value", option_r20.key);
            i0.ɵɵadvance(1);
            i0.ɵɵtextInterpolate1(" ", option_r20.value, " ");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_5_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Hardware flow control");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(3, "ion-select", 8);
            i0.ɵɵtemplate(4, TapProtocolConfigurationFormComponent_ion_item_5_ion_select_option_4_Template, 2, 2, "ion-select-option", 6);
            i0.ɵɵpipe(5, "enum");
            i0.ɵɵelementEnd();
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var ctx_r4 = i0.ɵɵnextContext();
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "hardwareFlowControl");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngForOf", i0.ɵɵpipeBind1(5, 2, ctx_r4.SerialSettings.HardwareFlowControl));
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_6_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Slave number");
            i0.ɵɵelementEnd();
            i0.ɵɵelement(3, "ion-input", 4);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "slave");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_7_ion_select_option_4_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-select-option", 7);
            i0.ɵɵtext(1);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var option_r22 = ctx.$implicit;
            i0.ɵɵproperty("value", option_r22.key);
            i0.ɵɵadvance(1);
            i0.ɵɵtextInterpolate1(" ", option_r22.value, "");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_7_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Physical port");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(3, "ion-select", 8);
            i0.ɵɵtemplate(4, TapProtocolConfigurationFormComponent_ion_item_7_ion_select_option_4_Template, 2, 2, "ion-select-option", 6);
            i0.ɵɵpipe(5, "enum");
            i0.ɵɵelementEnd();
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            var ctx_r6 = i0.ɵɵnextContext();
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "physicalPort");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngForOf", i0.ɵɵpipeBind1(5, 2, ctx_r6.SerialSettings.PhysicalPort));
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_8_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Timeout");
            i0.ɵɵelementEnd();
            i0.ɵɵelement(3, "ion-input", 4);
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "timeout");
        }
    }
    function TapProtocolConfigurationFormComponent_ion_item_9_Template(rf, ctx) {
        if (rf & 1) {
            i0.ɵɵelementStart(0, "ion-item");
            i0.ɵɵelementStart(1, "ion-label");
            i0.ɵɵtext(2, "Offset first register");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(3, "ion-select", 8);
            i0.ɵɵelementStart(4, "ion-select-option", 7);
            i0.ɵɵtext(5, "No");
            i0.ɵɵelementEnd();
            i0.ɵɵelementStart(6, "ion-select-option", 7);
            i0.ɵɵtext(7, "Yes");
            i0.ɵɵelementEnd();
            i0.ɵɵelementEnd();
            i0.ɵɵelementEnd();
        }
        if (rf & 2) {
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("formControlName", "offsetFirstRegister");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("value", false);
            i0.ɵɵadvance(2);
            i0.ɵɵproperty("value", true);
        }
    }
    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 i0.EventEmitter();
            // TODO add validators
            var formConfig = {
                baudRate: [187500, [i1$1.Validators.required, i1$1.Validators.min(0)]],
                bitParity: [''],
                dataBits: [''],
                hardwareFlowControl: [''],
                handshakeDelimiter: [target.SerialSettings.HandshakeDelimiter.NONE],
                physicalPort: [''],
                stopBits: [''],
                offsetFirstRegister: [''],
                // identification: [''],
                // ledStatus: [''],
                slave: [''],
                timeout: [''],
            };
            this.form = formBuilder.group(formConfig);
        }
        Object.defineProperty(TapProtocolConfigurationFormComponent.prototype, "values", {
            set: function (v) {
                if (v) {
                    this.updateFormValue(v);
                }
            },
            enumerable: false,
            configurable: true
        });
        TapProtocolConfigurationFormComponent.prototype.hasField = function (field) {
            return this.fields.indexOf(field) >= 0;
        };
        Object.defineProperty(TapProtocolConfigurationFormComponent.prototype, "SerialSettings", {
            get: function () {
                return target.SerialSettings;
            },
            enumerable: false,
            configurable: true
        });
        TapProtocolConfigurationFormComponent.prototype.updateFormValue = function (settings) {
            var newValues = Object.assign(Object.assign({}, settings.modbus), settings.serial);
            for (var key in newValues) {
                var field = this.form.get(key);
                if (field) {
                    field.setValue(newValues[key], {
                        emitEvent: false,
                    });
                }
            }
        };
        TapProtocolConfigurationFormComponent.prototype.ngOnInit = function () {
            var _this = this;
            // debug(TAG, 'ngOnInit');
            var settings = this.protocolConfigurationService.pendingSettings;
            this.updateFormValue(settings);
            this.form.valueChanges.subscribe(function (newValue) {
                // debug(TAG, 'Protocol configuration changed:', newValue);
                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(function (newSettings) {
                // debug(TAG, 'Refreshing form value', newSettings);
                if (_this.baudRates.indexOf(newSettings.serial.baudRate) === -1) {
                    _this.customBaudRate = true;
                }
                _this.updateFormValue(newSettings);
            });
        };
        return TapProtocolConfigurationFormComponent;
    }());
    /** @nocollapse */ TapProtocolConfigurationFormComponent.ɵfac = function TapProtocolConfigurationFormComponent_Factory(t) { return new (t || TapProtocolConfigurationFormComponent)(i0.ɵɵdirectiveInject(i1$1.FormBuilder), i0.ɵɵdirectiveInject(ProtocolConfigurationService)); };
    /** @nocollapse */ TapProtocolConfigurationFormComponent.ɵcmp = i0.ɵɵdefineComponent({ type: TapProtocolConfigurationFormComponent, selectors: [["tap-protocol-configuration-form"]], inputs: { customBaudRate: "customBaudRate", baudRates: "baudRates", fields: "fields", values: "values" }, outputs: { valueChanges: "valueChanges" }, decls: 10, vars: 10, consts: [[3, "formGroup"], [4, "ngIf"], ["slot", "end", "type", "number", 3, "formControlName", 4, "ngIf"], ["slot", "end", 3, "formControlName", 4, "ngIf"], ["slot", "end", "type", "number", 3, "formControlName"], ["slot", "end", 3, "formControlName"], [3, "value", 4, "ngFor", "ngForOf"], [3, "value"], [3, "formControlName"]], template: function TapProtocolConfigurationFormComponent_Template(rf, ctx) {
            if (rf & 1) {
                i0.ɵɵelementStart(0, "form", 0);
                i0.ɵɵtemplate(1, TapProtocolConfigurationFormComponent_ion_item_1_Template, 5, 2, "ion-item", 1);
                i0.ɵɵtemplate(2, TapProtocolConfigurationFormComponent_ion_item_2_Template, 6, 4, "ion-item", 1);
                i0.ɵɵtemplate(3, TapProtocolConfigurationFormComponent_ion_item_3_Template, 6, 4, "ion-item", 1);
                i0.ɵɵtemplate(4, TapProtocolConfigurationFormComponent_ion_item_4_Template, 6, 4, "ion-item", 1);
                i0.ɵɵtemplate(5, TapProtocolConfigurationFormComponent_ion_item_5_Template, 6, 4, "ion-item", 1);
                i0.ɵɵtemplate(6, TapProtocolConfigurationFormComponent_ion_item_6_Template, 4, 1, "ion-item", 1);
                i0.ɵɵtemplate(7, TapProtocolConfigurationFormComponent_ion_item_7_Template, 6, 4, "ion-item", 1);
                i0.ɵɵtemplate(8, TapProtocolConfigurationFormComponent_ion_item_8_Template, 4, 1, "ion-item", 1);
                i0.ɵɵtemplate(9, TapProtocolConfigurationFormComponent_ion_item_9_Template, 8, 3, "ion-item", 1);
                i0.ɵɵelementEnd();
            }
            if (rf & 2) {
                i0.ɵɵproperty("formGroup", ctx.form);
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("baudRate"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("dataBits"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("stopBits"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("bitParity"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("hardwareFlowControl"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("slave"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("physicalPort"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("timeout"));
                i0.ɵɵadvance(1);
                i0.ɵɵproperty("ngIf", ctx.hasField("offsetFirstRegister"));
            }
        }, directives: [i1$1.ɵangular_packages_forms_forms_ba, i1$1.NgControlStatusGroup, i1$1.FormGroupDirective, i3.NgIf, i4.IonItem, i4.IonLabel, i4.IonInput, i4.NumericValueAccessor, i1$1.NgControlStatus, i1$1.FormControlName, i4.IonSelect, i4.SelectValueAccessor, i3.NgForOf, i4.IonSelectOption], pipes: [i1.EnumPipe, i1.TrimPipe], styles: ["ion-input[_ngcontent-%COMP%]{text-align:right}"] });
    (function () {
        (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TapProtocolConfigurationFormComponent, [{
                type: i0.Component,
                args: [{
                        selector: 'tap-protocol-configuration-form',
                        templateUrl: './tap-protocol-configuration-form.component.html',
                        styleUrls: ['./tap-protocol-configuration-form.component.scss'],
                    }]
            }], function () { return [{ type: i1$1.FormBuilder }, { type: ProtocolConfigurationService }]; }, { customBaudRate: [{
                    type: i0.Input
                }], baudRates: [{
                    type: i0.Input
                }], fields: [{
                    type: i0.Input
                }], values: [{
                    type: i0.Input
                }], valueChanges: [{
                    type: i0.Output
                }] });
    })();

    var TapProtocolConfigurationModule = /** @class */ (function () {
        function TapProtocolConfigurationModule() {
        }
        return TapProtocolConfigurationModule;
    }());
    /** @nocollapse */ TapProtocolConfigurationModule.ɵfac = function TapProtocolConfigurationModule_Factory(t) { return new (t || TapProtocolConfigurationModule)(); };
    /** @nocollapse */ TapProtocolConfigurationModule.ɵmod = i0.ɵɵdefineNgModule({ type: TapProtocolConfigurationModule });
    /** @nocollapse */ TapProtocolConfigurationModule.ɵinj = i0.ɵɵdefineInjector({ providers: [ProtocolConfigurationService], imports: [[i3.CommonModule, i1.LibCommonModule, i1$1.FormsModule, i1$1.ReactiveFormsModule]] });
    (function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(TapProtocolConfigurationModule, { declarations: [TapProtocolConfigurationFormComponent], imports: [i3.CommonModule, i1.LibCommonModule, i1$1.FormsModule, i1$1.ReactiveFormsModule], exports: [TapProtocolConfigurationFormComponent] }); })();
    (function () {
        (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(TapProtocolConfigurationModule, [{
                type: i0.NgModule,
                args: [{
                        declarations: [TapProtocolConfigurationFormComponent],
                        exports: [TapProtocolConfigurationFormComponent],
                        imports: [i3.CommonModule, i1.LibCommonModule, i1$1.FormsModule, i1$1.ReactiveFormsModule],
                        providers: [ProtocolConfigurationService],
                    }]
            }], null, null);
    })();

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

    exports.ALL_SETTINGS_FIELDS = ALL_SETTINGS_FIELDS;
    exports.MODBUS_SETTINGS_FIELDS = MODBUS_SETTINGS_FIELDS;
    exports.ProtocolConfigurationError = ProtocolConfigurationError;
    exports.ProtocolConfigurationService = ProtocolConfigurationService;
    exports.SERIAL_SETTINGS_FIELDS = SERIAL_SETTINGS_FIELDS;
    exports.TAPNPASS_SETTINGS_FIELDS = TAPNPASS_SETTINGS_FIELDS;
    exports.TapProtocolConfigurationFormComponent = TapProtocolConfigurationFormComponent;
    exports.TapProtocolConfigurationModule = TapProtocolConfigurationModule;

    Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=iotize-ionic-target-common.umd.js.map
