import { h } from "@stencil/core";
var EndpointStatus;
(function (EndpointStatus) {
    EndpointStatus[EndpointStatus["LOADING"] = 0] = "LOADING";
    EndpointStatus[EndpointStatus["OK"] = 1] = "OK";
    EndpointStatus[EndpointStatus["NOK"] = 2] = "NOK";
})(EndpointStatus || (EndpointStatus = {}));
export class PickerEndpointStatus {
    constructor() {
        this.endpointStatus = EndpointStatus.LOADING;
    }
    onEndPointChange(newEndpoint, oldEndpoint) {
        console.log(newEndpoint);
        console.log(oldEndpoint);
        if (newEndpoint !== oldEndpoint) {
            this.setEndpointStatus(newEndpoint);
        }
    }
    componentWillLoad() {
        // Watch is not called the first time the prop is set. Therefor we will call the setEndpoint here the first time
        //
        this.setEndpointStatus(this.endpoint);
    }
    async setEndpointStatus(endpoint) {
        var _a;
        this.endpointStatus = EndpointStatus.LOADING;
        let status = EndpointStatus.NOK;
        try {
            const fetchResponse = await fetch(endpoint + '/status');
            const response = await fetchResponse.json();
            status = ((_a = response.status) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'ok' ? EndpointStatus.OK : EndpointStatus.NOK;
        }
        catch (error) {
            console.error(`[eva-config-endpoint-status] Failed to do fetch response getting status of endpoint ${endpoint}`, error);
        }
        finally {
            this.endpointStatus = status;
        }
        return status;
    }
    render() {
        if (this.endpointStatus === EndpointStatus.LOADING) {
            return h("eva-config-picker-spinner", { size: 20 });
        }
        if (this.endpointStatus === EndpointStatus.OK) {
            return h("div", { class: "feedback success" });
        }
        if (this.endpointStatus === EndpointStatus.NOK) {
            return h("div", { class: "feedback error" });
        }
    }
    static get is() { return "eva-config-picker-endpoint-status"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() { return {
        "$": ["picker-endpoint-status.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["picker-endpoint-status.css"]
    }; }
    static get properties() { return {
        "endpoint": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": ""
            },
            "attribute": "endpoint",
            "reflect": false
        }
    }; }
    static get states() { return {
        "endpointStatus": {}
    }; }
    static get watchers() { return [{
            "propName": "endpoint",
            "methodName": "onEndPointChange"
        }]; }
}
