import { proxyCustomElement, HTMLElement, createEvent, getAssetPath, h, Host } from '@stencil/core/internal/client';
import { g as getIconPath } from './mer-animated-dots.js';
import 'lit';
import 'lit/directives/when.js';
import 'lit/directives/if-defined.js';
import { c as config } from './config.js';
import { v as validateControls } from './form-validation.js';
import { L as Locale } from './locale.js';

const shareKbCss = "button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;block-size:100%;grid-template-rows:max-content 1fr max-content}.section{display:contents}header{display:grid;grid-template-rows:max-content;grid-template-columns:1fr 1fr max-content}.button-change{align-self:center;justify-self:end}.main{display:grid;grid-template-columns:1fr;grid-template-rows:max-content 1fr max-content max-content}.search-kbs{inline-size:50%}.versions-container{grid-auto-rows:max-content}.versions-container__input{display:grid;grid-template-columns:1fr max-content;gap:var(--spacing-gap-m)}";

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 __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 _GxIdeShareKb_showMoreIcon, _GxIdeShareKb_versionsSelect, _GxIdeShareKb__componentLocale, _GxIdeShareKb_kbVersions, _GxIdeShareKb_renderedFirstTime, _GxIdeShareKb_chShortcutsEl, _GxIdeShareKb_shortcutsSrc, _GxIdeShareKb_selectedKbId, _GxIdeShareKb_versionSelectedTypeEl, _GxIdeShareKb_KBNameEl, _GxIdeShareKb_cancelHandler, _GxIdeShareKb_changeConnectionHandler, _GxIdeShareKb_shareKBHandler, _GxIdeShareKb_evaluateTooltipRender, _GxIdeShareKb_initializeValidatableControls, _GxIdeShareKb_kbNameInputHandler, _GxIdeShareKb_selectedVersionHandler, _GxIdeShareKb_versionsChangedHandler, _GxIdeShareKb_renderFooter;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "utils/form--full",
    "utils/layout",
    "utils/typography",
    "utils/spacing",
    "components/list-box",
    "components/tooltip",
    "chameleon/scrollbar"
];
const GxIdeShareKb$1 = /*@__PURE__*/ proxyCustomElement(class GxIdeShareKb extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        this.componentDidRenderFirstTime = createEvent(this, "componentDidRenderFirstTime", 7);
        _GxIdeShareKb_showMoreIcon.set(this, getIconPath({
            category: "gemini-tools",
            name: "show-more-horizontal",
            colorType: "on-surface"
        }));
        _GxIdeShareKb_versionsSelect.set(this, [
            { value: "Trunk", caption: "Trunk Version" },
            { value: "All", caption: "All Versions" },
            { value: "Select", caption: "Select Version" }
        ]);
        _GxIdeShareKb__componentLocale.set(this, void 0);
        _GxIdeShareKb_kbVersions.set(this, []);
        _GxIdeShareKb_renderedFirstTime.set(this, false);
        _GxIdeShareKb_chShortcutsEl.set(this, void 0);
        _GxIdeShareKb_shortcutsSrc.set(this, getAssetPath(`./gx-ide-assets/create-kb-from-server/shortcuts.json`));
        _GxIdeShareKb_selectedKbId.set(this, void 0);
        _GxIdeShareKb_versionSelectedTypeEl.set(this, void 0);
        _GxIdeShareKb_KBNameEl.set(this, void 0);
        _GxIdeShareKb_cancelHandler.set(this, () => {
            this.cancelCallback();
        });
        _GxIdeShareKb_changeConnectionHandler.set(this, async () => {
            this.changeConnectionCallback({
                authenticationType: this.connectionData.authenticationType,
                serverUrl: this.connectionData.serverUrl,
                userName: this.connectionData.userName,
                userPassword: this.connectionData.userPassword
            });
        });
        _GxIdeShareKb_shareKBHandler.set(this, async () => {
            if (this.versionSelectedType !== "Select") {
                __classPrivateFieldSet(this, _GxIdeShareKb_kbVersions, [], "f");
            }
            this.shareKBCallback({
                name: this.kbName,
                serverUrl: this.connectionData.serverUrl,
                versionList: __classPrivateFieldGet(this, _GxIdeShareKb_kbVersions, "f"),
                allVersions: this.versionSelectedType === "All"
            }).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.
        _GxIdeShareKb_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));
        });
        _GxIdeShareKb_initializeValidatableControls.set(this, () => {
            // populate validatableControls with the controls that could have errors.
            const validatableControls = [__classPrivateFieldGet(this, _GxIdeShareKb_KBNameEl, "f")];
            validatableControls.forEach(validatableControl => {
                if (validatableControl.id) {
                    this.validatableControls.set(validatableControl.id, {
                        reference: validatableControl,
                        hasError: false,
                        message: undefined
                    });
                }
            });
        });
        _GxIdeShareKb_kbNameInputHandler.set(this, (event) => {
            this.kbName = event.detail;
        });
        _GxIdeShareKb_selectedVersionHandler.set(this, async () => {
            if (this.selectKBVersionsCallback) {
                await this.selectKBVersionsCallback(__classPrivateFieldGet(this, _GxIdeShareKb_selectedKbId, "f")).then(result => {
                    if (result === null || result === void 0 ? void 0 : result.length) {
                        __classPrivateFieldSet(this, _GxIdeShareKb_kbVersions, result.map(kbVersion => ({
                            id: kbVersion.id,
                            name: kbVersion.name
                        })), "f");
                        if (result.length === 1 && __classPrivateFieldGet(this, _GxIdeShareKb_kbVersions, "f")[0].id === 0) {
                            this.localKbNameInputValue = __classPrivateFieldGet(this, _GxIdeShareKb_selectedKbId, "f");
                            this.versionsValueInput =
                                __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb.versionsPlaceholder;
                        }
                        else if (result.length > 1) {
                            this.localKbNameInputValue = __classPrivateFieldGet(this, _GxIdeShareKb_selectedKbId, "f");
                            this.versionsValueInput = `${result.length.toString()} ${__classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb.versionSelectedFieldValue}`;
                        }
                        else {
                            this.localKbNameInputValue = `${__classPrivateFieldGet(this, _GxIdeShareKb_selectedKbId, "f")}_${__classPrivateFieldGet(this, _GxIdeShareKb_kbVersions, "f")[0].name}`;
                            this.versionsValueInput = `${result[0].name} ${__classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb.oneVersionSelectedFieldValue}`;
                        }
                    }
                });
            }
        });
        _GxIdeShareKb_versionsChangedHandler.set(this, (event) => {
            this.versionSelectedType = event.detail;
            if (this.versionSelectedType === "Select") {
                this.enableSelectVersionsButton = true;
            }
            else {
                this.enableSelectVersionsButton = false;
            }
        });
        _GxIdeShareKb_renderFooter.set(this, () => {
            const disableShareKbButton = !this.kbName.length;
            return (h("footer", { class: "footer dialog-footer-with-border spacing-body-inline spacing-body-block-end" }, h("div", { class: "buttons-spacer" }, h("button", {
                // cancel button
                class: "button-secondary", onClick: __classPrivateFieldGet(this, _GxIdeShareKb_cancelHandler, "f"), part: "button button-cancel"
            }, __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").footer.cancel), h("button", {
                // share kb button
                class: "button-primary", onClick: !disableShareKbButton && __classPrivateFieldGet(this, _GxIdeShareKb_shareKBHandler, "f"), disabled: disableShareKbButton, part: "button button-create-kb"
            }, __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").footer.shareKB))));
        });
        this.enableSelectVersionsButton = false;
        this.validatableControls = new Map();
        this.versionSelectedType = undefined;
        this.versionsValueInput = "";
        this.connectionData = undefined;
        this.cancelCallback = undefined;
        this.changeConnectionCallback = undefined;
        this.kbName = undefined;
        this.shareKBCallback = undefined;
        this.selectKBVersionsCallback = undefined;
    }
    componentDidLoad() {
        this.versionSelectedType = __classPrivateFieldGet(this, _GxIdeShareKb_versionSelectedTypeEl, "f")
            .value;
        __classPrivateFieldGet(this, _GxIdeShareKb_initializeValidatableControls, "f").call(this);
    }
    componentDidRender() {
        if (!__classPrivateFieldGet(this, _GxIdeShareKb_renderedFirstTime, "f")) {
            this.componentDidRenderFirstTime.emit(__classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").componentName);
            __classPrivateFieldSet(this, _GxIdeShareKb_renderedFirstTime, true, "f");
        }
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeShareKb__componentLocale, await Locale.getComponentStrings(this.el), "f");
    }
    /**
     * Suspends or reactivates the shortcuts
     */
    async suspendShortcuts(suspendShortcuts) {
        if (suspendShortcuts) {
            __classPrivateFieldGet(this, _GxIdeShareKb_chShortcutsEl, "f").suspend = true;
        }
        else {
            __classPrivateFieldGet(this, _GxIdeShareKb_chShortcutsEl, "f").suspend = false;
        }
    }
    render() {
        return (h(Host, { class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("ch-shortcuts", { src: __classPrivateFieldGet(this, _GxIdeShareKb_shortcutsSrc, "f"), ref: (el) => (__classPrivateFieldSet(this, _GxIdeShareKb_chShortcutsEl, el, "f")) }), h("section", { class: "section" }, h("header", { class: "header dialog-header-with-border space-body spacing-body-inline spacing-body-block-start" }, h("div", { class: "field field-block" }, h("label", { class: "label" }, __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").header.serverUrl), h("p", null, this.connectionData.serverUrl)), h("div", { class: "field field-block" }, h("label", { class: "label" }, __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").header.user), h("p", null, this.connectionData.userName)), h("button", { id: "change-connection-button", class: "button-tertiary button-change", onClick: __classPrivateFieldGet(this, _GxIdeShareKb_changeConnectionHandler, "f") }, __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").header.change)), h("section", { class: "main field-group spacing-body" }, h("div", { class: "field field-block" }, h("label", { class: "label", htmlFor: "alias" }, __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb.aliasLabel), h("ch-edit", {
            // alias
            class: "input", id: "kb-name", value: this.kbName, onInput: __classPrivateFieldGet(this, _GxIdeShareKb_kbNameInputHandler, "f"), placeholder: __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb.localKbNamePlaceholder, ref: (el) => (__classPrivateFieldSet(this, _GxIdeShareKb_KBNameEl, el, "f"))
        }), __classPrivateFieldGet(this, _GxIdeShareKb_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeShareKb_KBNameEl, "f"))), h("div", { class: "versions-container field-group" }, h("ch-radio-group-render", { class: "radio-group", model: __classPrivateFieldGet(this, _GxIdeShareKb_versionsSelect, "f"), onChange: __classPrivateFieldGet(this, _GxIdeShareKb_versionsChangedHandler, "f"), value: __classPrivateFieldGet(this, _GxIdeShareKb_versionsSelect, "f")[0].value, ref: (el) => (__classPrivateFieldSet(this, _GxIdeShareKb_versionSelectedTypeEl, el, "f")) }), this.versionSelectedType === "Select" && (h("div", { class: "versions-container__item field-group" }, h("div", { class: "field field-block" }, h("label", { class: "label", htmlFor: "control-id-1" }, __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb.versionsLabel), h("div", { class: "versions-container__input" }, h("ch-edit", { class: "input", readonly: true, placeholder: this.enableSelectVersionsButton
                ? __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb.versionsPlaceholder
                : __classPrivateFieldGet(this, _GxIdeShareKb__componentLocale, "f").kb
                    .versionsPlaceholderDisabled, value: this.versionsValueInput }), h("button", { id: "select-version-button", class: "button-tertiary button-icon-only", onClick: __classPrivateFieldGet(this, _GxIdeShareKb_selectedVersionHandler, "f"), disabled: !this.enableSelectVersionsButton }, h("ch-image", { src: __classPrivateFieldGet(this, _GxIdeShareKb_showMoreIcon, "f"), class: "icon-xs" })))))))), __classPrivateFieldGet(this, _GxIdeShareKb_renderFooter, "f").call(this))));
    }
    static get assetsDirs() { return ["gx-ide-assets/share-kb"]; }
    get el() { return this; }
    static get style() { return shareKbCss; }
}, [1, "gx-ide-share-kb", {
        "connectionData": [16],
        "cancelCallback": [16],
        "changeConnectionCallback": [16],
        "kbName": [1025, "kb-name"],
        "shareKBCallback": [16],
        "selectKBVersionsCallback": [16],
        "enableSelectVersionsButton": [32],
        "validatableControls": [32],
        "versionSelectedType": [32],
        "versionsValueInput": [32],
        "suspendShortcuts": [64]
    }]);
_GxIdeShareKb_showMoreIcon = new WeakMap(), _GxIdeShareKb_versionsSelect = new WeakMap(), _GxIdeShareKb__componentLocale = new WeakMap(), _GxIdeShareKb_kbVersions = new WeakMap(), _GxIdeShareKb_renderedFirstTime = new WeakMap(), _GxIdeShareKb_chShortcutsEl = new WeakMap(), _GxIdeShareKb_shortcutsSrc = new WeakMap(), _GxIdeShareKb_selectedKbId = new WeakMap(), _GxIdeShareKb_versionSelectedTypeEl = new WeakMap(), _GxIdeShareKb_KBNameEl = new WeakMap(), _GxIdeShareKb_cancelHandler = new WeakMap(), _GxIdeShareKb_changeConnectionHandler = new WeakMap(), _GxIdeShareKb_shareKBHandler = new WeakMap(), _GxIdeShareKb_evaluateTooltipRender = new WeakMap(), _GxIdeShareKb_initializeValidatableControls = new WeakMap(), _GxIdeShareKb_kbNameInputHandler = new WeakMap(), _GxIdeShareKb_selectedVersionHandler = new WeakMap(), _GxIdeShareKb_versionsChangedHandler = new WeakMap(), _GxIdeShareKb_renderFooter = new WeakMap();
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["gx-ide-share-kb"];
    components.forEach(tagName => { switch (tagName) {
        case "gx-ide-share-kb":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, GxIdeShareKb$1);
            }
            break;
    } });
}

const GxIdeShareKb = GxIdeShareKb$1;
const defineCustomElement = defineCustomElement$1;

export { GxIdeShareKb, defineCustomElement };

//# sourceMappingURL=gx-ide-share-kb.js.map