import { proxyCustomElement, HTMLElement, getAssetPath, h, Host } from '@stencil/core/internal/client';
import { c as config } from './config.js';
import { v as validateControls } from './form-validation.js';
import { L as Locale } from './locale.js';

const mapAuthenticationTypeToComboBoxModel = (authenticationTypes) => {
    return authenticationTypes.map(authenticationType => {
        return {
            value: authenticationType.id,
            caption: authenticationType.name
        };
    });
};

const connectGxServerCss = "button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;block-size:100%;overflow:auto;grid-template-rows:1fr max-content}.main{display:grid;grid-template-areas:\"server-url server-url\" \"authentication-type .\" \"user-name user-password\";grid-auto-rows:max-content;grid-template-columns:1fr 1fr}.section{display:contents}.authentication-type{grid-area:authentication-type}.server-url{grid-area:server-url}.user-name{grid-area:user-name}.user-password{grid-area:user-password}.tooltip{position:absolute}";

var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) || function (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);
};
var __classPrivateFieldSet = (undefined && undefined.__classPrivateFieldSet) || function (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 _HTMLGxIdeConnectGxServer_componentLocale, _HTMLGxIdeConnectGxServer_usernameRef, _HTMLGxIdeConnectGxServer_passwordRef, _HTMLGxIdeConnectGxServer_serverUrlRef, _HTMLGxIdeConnectGxServer_shortcutsSrc, _HTMLGxIdeConnectGxServer_cancelClickHandler, _HTMLGxIdeConnectGxServer_connectClickHandler, _HTMLGxIdeConnectGxServer_evaluateTooltipRender, _HTMLGxIdeConnectGxServer_init, _HTMLGxIdeConnectGxServer_initializeValidatableControls, _HTMLGxIdeConnectGxServer_authenticationInputEventHandler, _HTMLGxIdeConnectGxServer_usernameInputEventHandler, _HTMLGxIdeConnectGxServer_passwordInputEventHandler;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/tooltip",
    "utils/form--full",
    "utils/layout",
    "utils/typography",
    "utils/spacing",
    "chameleon/scrollbar"
];
const HTMLGxIdeConnectGxServer = /*@__PURE__*/ proxyCustomElement(class HTMLGxIdeConnectGxServer extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        /**
         * The component hard-coded strings translations.
         */
        // eslint-disable-next-line @stencil-community/own-props-must-be-private
        _HTMLGxIdeConnectGxServer_componentLocale.set(this, void 0);
        _HTMLGxIdeConnectGxServer_usernameRef.set(this, void 0);
        _HTMLGxIdeConnectGxServer_passwordRef.set(this, void 0);
        _HTMLGxIdeConnectGxServer_serverUrlRef.set(this, void 0);
        _HTMLGxIdeConnectGxServer_shortcutsSrc.set(this, getAssetPath(`./gx-ide-assets/connect-gx-server/shortcuts.json`));
        _HTMLGxIdeConnectGxServer_cancelClickHandler.set(this, () => {
            this.cancelCallback();
        });
        _HTMLGxIdeConnectGxServer_connectClickHandler.set(this, async () => {
            this.connectCallback(this.gxServerConnectionData).then((formSubmitResult) => {
                this.validatableControls = validateControls(formSubmitResult, this.validatableControls);
            });
        });
        // TODO: evaluate if this should be a global helper function.
        // It is used on other dialogs as well.
        _HTMLGxIdeConnectGxServer_evaluateTooltipRender.set(this, (controlReference) => {
            var _a, _b, _c;
            return ((_b = (_a = this.validatableControls) === null || _a === void 0 ? void 0 : _a.get(controlReference === null || controlReference === void 0 ? void 0 : controlReference.id)) === null || _b === void 0 ? void 0 : _b.hasError) &&
                ((_c = this.validatableControls.get(controlReference.id)) === null || _c === void 0 ? void 0 : _c.message) && (h("ch-tooltip", { class: "tooltip", actionElement: controlReference, blockAlign: config.tooltipSettings.blockAlign, inlineAlign: config.tooltipSettings.inlineAlign, delay: config.tooltipSettings.delay }, this.validatableControls.get(controlReference.id).message));
        });
        _HTMLGxIdeConnectGxServer_init.set(this, () => {
            var _a, _b, _c, _d;
            this.authenticationTypesChanged(this.authenticationTypes);
            // set initial values:
            // server url
            this.gxServerConnectionData.serverUrl =
                (_a = this.defaultConnectionData) === null || _a === void 0 ? void 0 : _a.serverUrl;
            //server name
            this.gxServerConnectionData.serverName =
                (_b = this.defaultConnectionData) === null || _b === void 0 ? void 0 : _b.serverName;
            // authentication type
            this.gxServerConnectionData.authenticationType = {
                id: (_c = this.authenticationTypesComboBoxModel[0]) === null || _c === void 0 ? void 0 : _c.value,
                name: (_d = this.authenticationTypesComboBoxModel[0]) === null || _d === void 0 ? void 0 : _d.caption
            };
        });
        _HTMLGxIdeConnectGxServer_initializeValidatableControls.set(this, () => {
            // populate #controlsValidation with the controls that could have errors.
            const validatableControls = [
                __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_usernameRef, "f"),
                __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_passwordRef, "f"),
                __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_serverUrlRef, "f")
            ];
            validatableControls.forEach(validatableControl => {
                if (validatableControl.id) {
                    this.validatableControls.set(validatableControl.id, {
                        reference: validatableControl,
                        hasError: false,
                        message: undefined
                    });
                }
            });
        });
        _HTMLGxIdeConnectGxServer_authenticationInputEventHandler.set(this, (event) => {
            const authenticationTypeId = event.detail;
            const authenticationTypeIndex = this.authenticationTypes.findIndex(authType => {
                return authType.id === authenticationTypeId;
            });
            const authenticationTypeName = this.authenticationTypes[authenticationTypeIndex].name;
            this.gxServerConnectionData.authenticationType = {
                id: authenticationTypeId,
                name: authenticationTypeName
            };
        });
        _HTMLGxIdeConnectGxServer_usernameInputEventHandler.set(this, (event) => {
            this.gxServerConnectionData.userName = event.detail;
        });
        _HTMLGxIdeConnectGxServer_passwordInputEventHandler.set(this, (event) => {
            this.gxServerConnectionData.userPassword = event.detail;
        });
        this.authenticationTypesComboBoxModel = undefined;
        this.gxServerConnectionData = {
            authenticationType: {
                id: null,
                name: null
            },
            serverUrl: null,
            userName: null,
            userPassword: null,
            serverName: null
        };
        this.validatableControls = new Map();
        this.authenticationTypes = undefined;
        this.cancelCallback = undefined;
        this.connectCallback = undefined;
        this.defaultConnectionData = undefined;
        this.enableCustomServer = false;
    }
    authenticationTypesChanged(newAuthenticationTypes) {
        if (newAuthenticationTypes === null || newAuthenticationTypes === void 0 ? void 0 : newAuthenticationTypes.length) {
            this.authenticationTypesComboBoxModel =
                mapAuthenticationTypeToComboBoxModel(newAuthenticationTypes);
        }
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _HTMLGxIdeConnectGxServer_componentLocale, await Locale.getComponentStrings(this.el), "f");
        __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_init, "f").call(this);
    }
    async componentDidLoad() {
        __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_initializeValidatableControls, "f").call(this);
    }
    render() {
        var _a, _b, _c, _d;
        return (h(Host, { class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("ch-shortcuts", { src: __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_shortcutsSrc, "f") }), h("section", { class: "section" }, h("div", { class: "main field-group spacing-body" }, h("div", {
            // server urls
            class: "field field-block server-url"
        }, h("label", { class: "label", htmlFor: "server-url" }, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_componentLocale, "f").main.serverURLLabel), h("ch-edit", { type: "url", id: "server-url", class: "input", value: (_a = this.gxServerConnectionData) === null || _a === void 0 ? void 0 : _a.serverUrl, readonly: true, ref: (el) => (__classPrivateFieldSet(this, _HTMLGxIdeConnectGxServer_serverUrlRef, el, "f")) })), h("div", {
            // authentication types
            class: "field field-block authentication-type"
        }, h("label", { class: "label", htmlFor: "authentication-type" }, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_componentLocale, "f").main.authenticationTypeLabel), h("ch-combo-box-render", { id: "authentication-type", class: "combo-box", accessibleName: "Authentication Types", model: this.authenticationTypesComboBoxModel, placeholder: "Select an authentication type", value: (_b = this.gxServerConnectionData) === null || _b === void 0 ? void 0 : _b.authenticationType.id, onInput: __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_authenticationInputEventHandler, "f") })), h("div", {
            // username
            class: "field field-block user-name"
        }, h("label", { class: "label", htmlFor: "username" }, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_componentLocale, "f").main.usernameLabel), h("ch-edit", { type: "text", id: "username", class: "input", value: (_c = this.gxServerConnectionData) === null || _c === void 0 ? void 0 : _c.userName, onInput: __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_usernameInputEventHandler, "f"), ref: (el) => (__classPrivateFieldSet(this, _HTMLGxIdeConnectGxServer_usernameRef, el, "f")) }), __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_usernameRef, "f"))), h("div", {
            // user password
            class: "field field-block user-password"
        }, h("label", { class: "label", htmlFor: "password" }, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_componentLocale, "f").main.passwordLabel), h("ch-edit", { type: "password", id: "password", class: "input", value: (_d = this.gxServerConnectionData) === null || _d === void 0 ? void 0 : _d.userPassword, onInput: __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_passwordInputEventHandler, "f"), ref: (el) => (__classPrivateFieldSet(this, _HTMLGxIdeConnectGxServer_passwordRef, el, "f")), showPasswordButton: true }), __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_passwordRef, "f")))), h("footer", { class: "dialog-footer-with-border spacing-body" }, h("div", { class: "buttons-spacer" }, h("button", { id: "cancel-button",
            // cancel button
            class: "button-secondary", onClick: __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_cancelClickHandler, "f") }, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_componentLocale, "f").footer.backButtonCaption), h("button", { id: "connect-button",
            // connect button
            class: "button-primary", onClick: __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_connectClickHandler, "f") }, __classPrivateFieldGet(this, _HTMLGxIdeConnectGxServer_componentLocale, "f").footer.connectButtonCaption))))));
    }
    static get assetsDirs() { return ["gx-ide-assets/connect-gx-server"]; }
    get el() { return this; }
    static get watchers() { return {
        "authenticationTypes": ["authenticationTypesChanged"]
    }; }
    static get style() { return connectGxServerCss; }
}, [1, "gx-ide-connect-gx-server", {
        "authenticationTypes": [16],
        "cancelCallback": [16],
        "connectCallback": [16],
        "defaultConnectionData": [16],
        "enableCustomServer": [4, "enable-custom-server"],
        "authenticationTypesComboBoxModel": [32],
        "gxServerConnectionData": [32],
        "validatableControls": [32]
    }, undefined, {
        "authenticationTypes": ["authenticationTypesChanged"]
    }]);
_HTMLGxIdeConnectGxServer_componentLocale = new WeakMap(), _HTMLGxIdeConnectGxServer_usernameRef = new WeakMap(), _HTMLGxIdeConnectGxServer_passwordRef = new WeakMap(), _HTMLGxIdeConnectGxServer_serverUrlRef = new WeakMap(), _HTMLGxIdeConnectGxServer_shortcutsSrc = new WeakMap(), _HTMLGxIdeConnectGxServer_cancelClickHandler = new WeakMap(), _HTMLGxIdeConnectGxServer_connectClickHandler = new WeakMap(), _HTMLGxIdeConnectGxServer_evaluateTooltipRender = new WeakMap(), _HTMLGxIdeConnectGxServer_init = new WeakMap(), _HTMLGxIdeConnectGxServer_initializeValidatableControls = new WeakMap(), _HTMLGxIdeConnectGxServer_authenticationInputEventHandler = new WeakMap(), _HTMLGxIdeConnectGxServer_usernameInputEventHandler = new WeakMap(), _HTMLGxIdeConnectGxServer_passwordInputEventHandler = new WeakMap();
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["gx-ide-connect-gx-server"];
    components.forEach(tagName => { switch (tagName) {
        case "gx-ide-connect-gx-server":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, HTMLGxIdeConnectGxServer);
            }
            break;
    } });
}

const GxIdeConnectGxServer = HTMLGxIdeConnectGxServer;
const defineCustomElement = defineCustomElement$1;

export { GxIdeConnectGxServer, defineCustomElement };

//# sourceMappingURL=gx-ide-connect-gx-server.js.map