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

const namePickerCss = "button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;block-size:100%;grid-template-rows:1fr max-content}.field-group{grid-auto-rows:max-content}.main{overflow:auto}.section{display:contents}.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 _GxIdeNamePicker_componentLocale, _GxIdeNamePicker_inputValueEl, _GxIdeNamePicker_shortcutsSrc, _GxIdeNamePicker_handleInputValueChange, _GxIdeNamePicker_cancelClickHandler, _GxIdeNamePicker_confirmClickHandler, _GxIdeNamePicker_initializeValidatableControls, _GxIdeNamePicker_evaluateTooltipRender;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "utils/form",
    "utils/spacing",
    "utils/layout",
    "chameleon/scrollbar",
    "components/button",
    "components/edit",
    "components/tooltip"
];
const GxIdeNamePicker$1 = /*@__PURE__*/ proxyCustomElement(class GxIdeNamePicker 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
        _GxIdeNamePicker_componentLocale.set(this, void 0);
        _GxIdeNamePicker_inputValueEl.set(this, void 0);
        _GxIdeNamePicker_shortcutsSrc.set(this, getAssetPath(`./gx-ide-assets/name-picker/shortcuts.json`));
        _GxIdeNamePicker_handleInputValueChange.set(this, (event) => {
            this.inputValue = event.detail;
            clearControlError(this.validatableControls, "input-name");
        });
        _GxIdeNamePicker_cancelClickHandler.set(this, async () => {
            this.cancelCallback();
        });
        _GxIdeNamePicker_confirmClickHandler.set(this, async () => {
            const formSubmitResult = await this.confirmCallback(this.inputValue);
            this.validatableControls = validateControls(formSubmitResult, this.validatableControls);
        });
        _GxIdeNamePicker_initializeValidatableControls.set(this, () => {
            // populate validatableControls with the controls that could have errors.
            const validatableControls = [__classPrivateFieldGet(this, _GxIdeNamePicker_inputValueEl, "f")];
            validatableControls.forEach(validatableControl => {
                if (validatableControl.id) {
                    this.validatableControls.set(validatableControl.id, {
                        reference: validatableControl,
                        hasError: false,
                        message: undefined
                    });
                }
            });
        });
        // TODO: evaluate if this should be a global helper function.
        // It is used on other dialogs as well.
        _GxIdeNamePicker_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));
        });
        this.inputValue = undefined;
        this.validatableControls = new Map();
        this.cancelCallback = undefined;
        this.confirmCallback = undefined;
        this.defaultInputValue = undefined;
    }
    defaultInputValueChanged(newDefaultInputValue) {
        this.inputValue = newDefaultInputValue;
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeNamePicker_componentLocale, await Locale.getComponentStrings(this.el), "f");
        this.defaultInputValueChanged(this.defaultInputValue);
    }
    componentDidLoad() {
        __classPrivateFieldGet(this, _GxIdeNamePicker_initializeValidatableControls, "f").call(this);
    }
    render() {
        const confirmButtonDisabled = !this.inputValue || this.inputValue.length === 0;
        return (h(Host, { class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("ch-shortcuts", { src: __classPrivateFieldGet(this, _GxIdeNamePicker_shortcutsSrc, "f") }), h("section", { class: "section" }, h("div", { class: "main scrollable field-group spacing-body" }, h("div", { class: "field field-block" }, h("label", { class: "label", htmlFor: "input-name" }, __classPrivateFieldGet(this, _GxIdeNamePicker_componentLocale, "f").labels.name), h("ch-edit", { type: "text", id: "input-name", class: "input", placeholder: __classPrivateFieldGet(this, _GxIdeNamePicker_componentLocale, "f").placeHolders.name, value: this.inputValue, onInput: __classPrivateFieldGet(this, _GxIdeNamePicker_handleInputValueChange, "f"), ref: (el) => (__classPrivateFieldSet(this, _GxIdeNamePicker_inputValueEl, el, "f")) }), __classPrivateFieldGet(this, _GxIdeNamePicker_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeNamePicker_inputValueEl, "f")))), h("footer", { class: "dialog-footer-with-border spacing-body" }, h("div", { class: "buttons-spacer" }, h("button", { id: "cancel-button", class: "button-secondary", onClick: __classPrivateFieldGet(this, _GxIdeNamePicker_cancelClickHandler, "f") }, __classPrivateFieldGet(this, _GxIdeNamePicker_componentLocale, "f").buttons.cancel), h("button", { id: "confirm-button", class: "button-primary", onClick: __classPrivateFieldGet(this, _GxIdeNamePicker_confirmClickHandler, "f"), disabled: confirmButtonDisabled }, __classPrivateFieldGet(this, _GxIdeNamePicker_componentLocale, "f").buttons.confirm))))));
    }
    static get assetsDirs() { return ["gx-ide-assets/name-picker"]; }
    get el() { return this; }
    static get watchers() { return {
        "defaultInputValue": ["defaultInputValueChanged"]
    }; }
    static get style() { return namePickerCss; }
}, [1, "gx-ide-name-picker", {
        "cancelCallback": [16],
        "confirmCallback": [16],
        "defaultInputValue": [1, "default-input-value"],
        "inputValue": [32],
        "validatableControls": [32]
    }, undefined, {
        "defaultInputValue": ["defaultInputValueChanged"]
    }]);
_GxIdeNamePicker_componentLocale = new WeakMap(), _GxIdeNamePicker_inputValueEl = new WeakMap(), _GxIdeNamePicker_shortcutsSrc = new WeakMap(), _GxIdeNamePicker_handleInputValueChange = new WeakMap(), _GxIdeNamePicker_cancelClickHandler = new WeakMap(), _GxIdeNamePicker_confirmClickHandler = new WeakMap(), _GxIdeNamePicker_initializeValidatableControls = new WeakMap(), _GxIdeNamePicker_evaluateTooltipRender = new WeakMap();
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["gx-ide-name-picker"];
    components.forEach(tagName => { switch (tagName) {
        case "gx-ide-name-picker":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, GxIdeNamePicker$1);
            }
            break;
    } });
}

const GxIdeNamePicker = GxIdeNamePicker$1;
const defineCustomElement = defineCustomElement$1;

export { GxIdeNamePicker, defineCustomElement };

//# sourceMappingURL=gx-ide-name-picker.js.map