import { Component, Output, EventEmitter, Input } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { CurrentDeviceService } from '../../current-device.service';
import { Platform } from '@ionic/angular';
import { getFormFieldOrError } from '../../utility';
import * as i0 from "@angular/core";
import * as i1 from "../../current-device.service";
import * as i2 from "@angular/forms";
import * as i3 from "@ionic/angular";
import * as i4 from "@angular/common";
import * as i5 from "../../lib-common/remember-value-control";
export class TapConnectSocketComponent {
    constructor(currentDevice, formBuilder, platform) {
        this.currentDevice = currentDevice;
        this.formBuilder = formBuilder;
        this.platform = platform;
        this.rememberGroupName = 'TapConnectSocket';
        this.onProtocolSelected = new EventEmitter();
        this.protocols = [
            {
                title: 'Websocket',
                value: 'ws',
            },
        ];
        if (this.isSocketSupported()) {
            this.protocols.push({
                title: 'Socket',
                value: 'tcp',
            });
        }
        const defaultPort = this.isSocketSupported() ? '2000' : '2080';
        const defaultProtocol = this.isSocketSupported() ? 'tcp' : 'ws';
        this.form = this.formBuilder.group({
            host: ['localhost', Validators.required],
            port: [defaultPort, Validators.required],
            protocol: [defaultProtocol, Validators.required],
        });
    }
    get protocol() {
        return getFormFieldOrError(this.form, 'protocol');
    }
    get host() {
        return getFormFieldOrError(this.form, 'host');
    }
    get port() {
        return getFormFieldOrError(this.form, 'port');
    }
    isSocketSupported() {
        return this.platform.is('cordova') || this.platform.is('electron');
    }
    async connect() {
        let url;
        switch (this.protocol.value) {
            case 'ws':
                url = `ws://${this.host.value}:${this.port.value}`;
                const metaWs = {
                    type: 'websocket',
                    info: {
                        url: url,
                    },
                };
                this.onProtocolSelected.emit({
                    meta: metaWs,
                });
                break;
            case 'tcp':
                url = `tcp://${this.host.value}:${this.port.value}`;
                const meta = {
                    type: 'socket',
                    info: {
                        url: url,
                    },
                };
                this.onProtocolSelected.emit({
                    meta: meta,
                });
                break;
            default:
                console.warn(`Invalid protocol: ${this.protocol.value}`);
        }
    }
}
/** @nocollapse */ /** @nocollapse */ TapConnectSocketComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.0", ngImport: i0, type: TapConnectSocketComponent, deps: [{ token: i1.CurrentDeviceService }, { token: i2.FormBuilder }, { token: i3.Platform }], target: i0.ɵɵFactoryTarget.Component });
/** @nocollapse */ /** @nocollapse */ TapConnectSocketComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.0", type: TapConnectSocketComponent, selector: "tap-connect-socket", inputs: { protocols: "protocols" }, outputs: { onProtocolSelected: "onProtocolSelected" }, ngImport: i0, template: "<form (ngSubmit)=\"connect()\" [formGroup]=\"form\">\n  <ion-item>\n    <ion-select\n      name=\"protocol\"\n      formControlName=\"protocol\"\n      slot=\"start\"\n      required\n    >\n      <ion-select-option\n        *ngFor=\"let protocol of protocols\"\n        [value]=\"protocol.value\"\n        >{{ protocol.title }}</ion-select-option\n      >\n    </ion-select>\n    <ion-input\n      [rememberValueControl]=\"rememberGroupName\"\n      name=\"host\"\n      formControlName=\"host\"\n      placeholder=\"Hostname\"\n      autocapitalize=\"none\"\n      spellcheck=\"false\"\n      autocorrect=\"off\"\n      required\n      type=\"text\"\n    ></ion-input>\n    <ion-input\n      [rememberValueControl]=\"rememberGroupName\"\n      name=\"port\"\n      formControlName=\"port\"\n      placeholder=\"Port\"\n      required\n      type=\"number\"\n      slot=\"end\"\n    ></ion-input>\n  </ion-item>\n  <div class=\"ion-padding\">\n    <ion-button\n      [disabled]=\"form['invalid']\"\n      expand=\"block\"\n      size=\"large\"\n      type=\"submit\"\n    >\n      <ion-icon name=\"log-in-outline\"></ion-icon> &nbsp; Connect\n    </ion-button>\n  </div>\n</form>\n", styles: [""], components: [{ type: i3.IonItem, selector: "ion-item", inputs: ["button", "color", "counter", "detail", "detailIcon", "disabled", "download", "fill", "href", "lines", "mode", "rel", "routerAnimation", "routerDirection", "shape", "target", "type"] }, { type: i3.IonSelect, selector: "ion-select", inputs: ["cancelText", "compareWith", "disabled", "interface", "interfaceOptions", "mode", "multiple", "name", "okText", "placeholder", "selectedText", "value"] }, { type: i3.IonSelectOption, selector: "ion-select-option", inputs: ["disabled", "value"] }, { type: i3.IonInput, selector: "ion-input", inputs: ["accept", "autocapitalize", "autocomplete", "autocorrect", "autofocus", "clearInput", "clearOnEdit", "color", "debounce", "disabled", "enterkeyhint", "inputmode", "max", "maxlength", "min", "minlength", "mode", "multiple", "name", "pattern", "placeholder", "readonly", "required", "size", "spellcheck", "step", "type", "value"] }, { type: i3.IonButton, selector: "ion-button", inputs: ["buttonType", "color", "disabled", "download", "expand", "fill", "href", "mode", "rel", "routerAnimation", "routerDirection", "shape", "size", "strong", "target", "type"] }, { type: i3.IonIcon, selector: "ion-icon", inputs: ["color", "flipRtl", "icon", "ios", "lazy", "md", "mode", "name", "sanitize", "size", "src"] }], directives: [{ type: i2.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { type: i2.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { type: i2.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { type: i3.SelectValueAccessor, selector: "ion-range, ion-select, ion-radio-group, ion-segment, ion-datetime" }, { type: i2.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { type: i2.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { type: i2.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { type: i4.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i3.TextValueAccessor, selector: "ion-input:not([type=number]),ion-textarea,ion-searchbar" }, { type: i5.RememberValueControlDirective, selector: "[rememberValueControl]", inputs: ["formControlName", "rememberValueControl"] }, { type: i3.NumericValueAccessor, selector: "ion-input[type=number]" }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.0", ngImport: i0, type: TapConnectSocketComponent, decorators: [{
            type: Component,
            args: [{ selector: 'tap-connect-socket', template: "<form (ngSubmit)=\"connect()\" [formGroup]=\"form\">\n  <ion-item>\n    <ion-select\n      name=\"protocol\"\n      formControlName=\"protocol\"\n      slot=\"start\"\n      required\n    >\n      <ion-select-option\n        *ngFor=\"let protocol of protocols\"\n        [value]=\"protocol.value\"\n        >{{ protocol.title }}</ion-select-option\n      >\n    </ion-select>\n    <ion-input\n      [rememberValueControl]=\"rememberGroupName\"\n      name=\"host\"\n      formControlName=\"host\"\n      placeholder=\"Hostname\"\n      autocapitalize=\"none\"\n      spellcheck=\"false\"\n      autocorrect=\"off\"\n      required\n      type=\"text\"\n    ></ion-input>\n    <ion-input\n      [rememberValueControl]=\"rememberGroupName\"\n      name=\"port\"\n      formControlName=\"port\"\n      placeholder=\"Port\"\n      required\n      type=\"number\"\n      slot=\"end\"\n    ></ion-input>\n  </ion-item>\n  <div class=\"ion-padding\">\n    <ion-button\n      [disabled]=\"form['invalid']\"\n      expand=\"block\"\n      size=\"large\"\n      type=\"submit\"\n    >\n      <ion-icon name=\"log-in-outline\"></ion-icon> &nbsp; Connect\n    </ion-button>\n  </div>\n</form>\n", styles: [""] }]
        }], ctorParameters: function () { return [{ type: i1.CurrentDeviceService }, { type: i2.FormBuilder }, { type: i3.Platform }]; }, propDecorators: { onProtocolSelected: [{
                type: Output
            }], protocols: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 