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

const curlInspectorCss = "button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;grid-template-rows:max-content 1fr max-content;block-size:100%;overflow:auto}.section{display:contents}.main{display:grid;overflow:auto}.field-group-name-description-module{align-items:center;grid-template-areas:\"name-label name-input\" \"description-label description-input\" \"module-label module-entity-selector\";grid-template-columns:max-content 1fr}.name-label{grid-area:name-label}.name-input{grid-area:name-input}.description-label{grid-area:description-label}.description-input{grid-area:description-input}.module-label{grid-area:module-label}.module-entity-selector{grid-area:module-entity-selector}.tooltip{height:0}";

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 _GxIdeCurlInspector_componentLocale, _GxIdeCurlInspector_chShortcutsEl, _GxIdeCurlInspector_descriptionEl, _GxIdeCurlInspector_moduleEntitySelector, _GxIdeCurlInspector_nameEl, _GxIdeCurlInspector_editorEl, _GxIdeCurlInspector_curlContentChangeHandler, _GxIdeCurlInspector_keydownHandler, _GxIdeCurlInspector_initializeValidatableControls, _GxIdeCurlInspector_evaluateTooltipRender, _GxIdeCurlInspector_validateForm, _GxIdeCurlInspector_handleEditorChange, _GxIdeCurlInspector_handleSlotChange, _GxIdeCurlInspector_createHandler;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/tooltip",
    "utils/form--full",
    "utils/layout",
    "utils/typography",
    "utils/spacing",
    "chameleon/scrollbar"
];
const GxIdeCurlInspector$1 = /*@__PURE__*/ proxyCustomElement(class GxIdeCurlInspector extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        _GxIdeCurlInspector_componentLocale.set(this, void 0);
        _GxIdeCurlInspector_chShortcutsEl.set(this, void 0);
        _GxIdeCurlInspector_descriptionEl.set(this, void 0);
        _GxIdeCurlInspector_moduleEntitySelector.set(this, void 0);
        _GxIdeCurlInspector_nameEl.set(this, void 0);
        _GxIdeCurlInspector_editorEl.set(this, void 0);
        _GxIdeCurlInspector_curlContentChangeHandler.set(this, void 0);
        _GxIdeCurlInspector_keydownHandler.set(this, void 0);
        _GxIdeCurlInspector_initializeValidatableControls.set(this, () => {
            const validatableControls = [
                __classPrivateFieldGet(this, _GxIdeCurlInspector_nameEl, "f"),
                __classPrivateFieldGet(this, _GxIdeCurlInspector_moduleEntitySelector, "f")
            ];
            validatableControls.forEach(validatableControl => {
                if (validatableControl.id) {
                    this.validatableControls.set(validatableControl.id, {
                        reference: validatableControl,
                        hasError: false,
                        message: undefined
                    });
                }
            });
        });
        _GxIdeCurlInspector_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));
        });
        _GxIdeCurlInspector_validateForm.set(this, () => {
            var _a, _b, _c, _d, _e;
            const name = ((_b = (_a = __classPrivateFieldGet(this, _GxIdeCurlInspector_nameEl, "f")) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.trim()) || "";
            const parentId = ((_d = (_c = __classPrivateFieldGet(this, _GxIdeCurlInspector_moduleEntitySelector, "f")) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.id) || "";
            const curl = ((_e = this.curlContent) === null || _e === void 0 ? void 0 : _e.trim()) || "";
            const isNameValid = this.validateNameCallback
                ? this.validateNameCallback(name)
                : Boolean(name);
            const isParentValid = Boolean(parentId);
            const isCurlValid = Boolean(curl);
            this.isFormValid = isNameValid && isParentValid && isCurlValid;
        });
        _GxIdeCurlInspector_handleEditorChange.set(this, () => {
            if (__classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f")) {
                this.curlContent = __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").text;
                __classPrivateFieldGet(this, _GxIdeCurlInspector_validateForm, "f").call(this);
            }
        });
        _GxIdeCurlInspector_handleSlotChange.set(this, (e) => {
            const elements = e.target.assignedElements();
            if (elements.length) {
                __classPrivateFieldSet(this, _GxIdeCurlInspector_editorEl, elements[0], "f");
                __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").addEventListener("input", __classPrivateFieldGet(this, _GxIdeCurlInspector_handleEditorChange, "f"));
                __classPrivateFieldSet(this, _GxIdeCurlInspector_curlContentChangeHandler, ((event) => {
                    this.curlContent = event.detail.content;
                    __classPrivateFieldGet(this, _GxIdeCurlInspector_handleEditorChange, "f").call(this);
                }), "f");
                __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").addEventListener("curl-content-change", __classPrivateFieldGet(this, _GxIdeCurlInspector_curlContentChangeHandler, "f"));
                __classPrivateFieldSet(this, _GxIdeCurlInspector_keydownHandler, (event) => {
                    if ((event.ctrlKey || event.metaKey) && event.key === "v") {
                        setTimeout(() => {
                            __classPrivateFieldGet(this, _GxIdeCurlInspector_handleEditorChange, "f").call(this);
                        }, 0);
                    }
                }, "f");
                __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").addEventListener("keydown", __classPrivateFieldGet(this, _GxIdeCurlInspector_keydownHandler, "f"));
                this.curlContent = __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").text;
                __classPrivateFieldGet(this, _GxIdeCurlInspector_validateForm, "f").call(this);
            }
        });
        _GxIdeCurlInspector_createHandler.set(this, () => {
            var _a;
            const data = {
                name: __classPrivateFieldGet(this, _GxIdeCurlInspector_nameEl, "f").value,
                description: __classPrivateFieldGet(this, _GxIdeCurlInspector_descriptionEl, "f").value,
                parentId: (_a = __classPrivateFieldGet(this, _GxIdeCurlInspector_moduleEntitySelector, "f").value) === null || _a === void 0 ? void 0 : _a.id,
                curl: this.curlContent
            };
            this.confirmCallback(data).then((formSubmitResult) => {
                this.validatableControls = validateControls(formSubmitResult, this.validatableControls);
            });
        });
        this.showTooltipError = false;
        this.isFormValid = false;
        this.curlContent = "";
        this.validatableControls = new Map();
        this.cancelCallback = undefined;
        this.confirmCallback = undefined;
        this.defaultParent = undefined;
        this.parent = undefined;
        this.selectModuleCallback = undefined;
        this.validateNameCallback = undefined;
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeCurlInspector_componentLocale, await Locale.getComponentStrings(this.el), "f");
    }
    componentDidLoad() {
        __classPrivateFieldGet(this, _GxIdeCurlInspector_initializeValidatableControls, "f").call(this);
    }
    disconnectedCallback() {
        if (__classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f") && __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").removeEventListener) {
            __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").removeEventListener("input", __classPrivateFieldGet(this, _GxIdeCurlInspector_handleEditorChange, "f"));
            __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").removeEventListener("curl-content-change", __classPrivateFieldGet(this, _GxIdeCurlInspector_curlContentChangeHandler, "f"));
            __classPrivateFieldGet(this, _GxIdeCurlInspector_editorEl, "f").removeEventListener("keydown", __classPrivateFieldGet(this, _GxIdeCurlInspector_keydownHandler, "f"));
        }
    }
    /**
     * Suspends or reactivates the shortcuts
     */
    async suspendShortcuts(suspendShortcuts) {
        __classPrivateFieldGet(this, _GxIdeCurlInspector_chShortcutsEl, "f").suspend = suspendShortcuts;
    }
    render() {
        return (h(Host, { class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("ch-shortcuts", { src: "./gx-ide-assets/curl-inspector/shortcuts.json", ref: (el) => (__classPrivateFieldSet(this, _GxIdeCurlInspector_chShortcutsEl, el, "f")) }), h("section", { class: "section" }, h("header", { class: "header field-group spacing-body dialog-header" }, h("div", { class: "field-group field-group-name-description-module" }, h("label", { class: "label name-label", htmlFor: "input-name" }, __classPrivateFieldGet(this, _GxIdeCurlInspector_componentLocale, "f").header.nameFieldLabel), h("ch-edit", { class: "input name-input", id: "input-name", ref: (el) => (__classPrivateFieldSet(this, _GxIdeCurlInspector_nameEl, el, "f")), onInput: __classPrivateFieldGet(this, _GxIdeCurlInspector_validateForm, "f") }), __classPrivateFieldGet(this, _GxIdeCurlInspector_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeCurlInspector_nameEl, "f")), h("label", { class: "label description-label", htmlFor: "input-description" }, __classPrivateFieldGet(this, _GxIdeCurlInspector_componentLocale, "f").header.descriptionFieldLabel), h("ch-edit", { class: "input description-input", id: "input-description", ref: (el) => (__classPrivateFieldSet(this, _GxIdeCurlInspector_descriptionEl, el, "f")) }), h("label", { class: "label module-label", htmlFor: "input-module" }, __classPrivateFieldGet(this, _GxIdeCurlInspector_componentLocale, "f").header.moduleFolderLabel), h("gx-ide-entity-selector", { class: "module module-entity-selector", id: "module", value: this.defaultParent, ref: (el) => (__classPrivateFieldSet(this, _GxIdeCurlInspector_moduleEntitySelector, el, "f")), labelPosition: "none", defaultValue: this.defaultParent, selectEntityCallback: this.selectModuleCallback }), __classPrivateFieldGet(this, _GxIdeCurlInspector_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeCurlInspector_moduleEntitySelector, "f")))), h("div", { class: "main field-group scrollable" }, h("slot", { name: "CurlTextEditor", onSlotchange: __classPrivateFieldGet(this, _GxIdeCurlInspector_handleSlotChange, "f") })), h("footer", { class: "dialog-footer dialog-footer spacing-body-block-end spacing-body-inline" }, h("div", { class: "buttons-spacer" }, h("button", { class: "button-secondary", id: "button-cancel", onClick: this.cancelCallback, part: "button-cancel" }, __classPrivateFieldGet(this, _GxIdeCurlInspector_componentLocale, "f").footer.btnCancel), h("button", { class: "button-primary", id: "button-ok", onClick: __classPrivateFieldGet(this, _GxIdeCurlInspector_createHandler, "f"), part: "button-ok", disabled: !this.isFormValid }, __classPrivateFieldGet(this, _GxIdeCurlInspector_componentLocale, "f").footer.btnCreate))))));
    }
    static get assetsDirs() { return ["gx-ide-assets/curl-inspector"]; }
    get el() { return this; }
    static get style() { return curlInspectorCss; }
}, [1, "gx-ide-curl-inspector", {
        "cancelCallback": [16],
        "confirmCallback": [16],
        "defaultParent": [16],
        "parent": [16],
        "selectModuleCallback": [16],
        "validateNameCallback": [16],
        "showTooltipError": [32],
        "isFormValid": [32],
        "curlContent": [32],
        "validatableControls": [32],
        "suspendShortcuts": [64]
    }]);
_GxIdeCurlInspector_componentLocale = new WeakMap(), _GxIdeCurlInspector_chShortcutsEl = new WeakMap(), _GxIdeCurlInspector_descriptionEl = new WeakMap(), _GxIdeCurlInspector_moduleEntitySelector = new WeakMap(), _GxIdeCurlInspector_nameEl = new WeakMap(), _GxIdeCurlInspector_editorEl = new WeakMap(), _GxIdeCurlInspector_curlContentChangeHandler = new WeakMap(), _GxIdeCurlInspector_keydownHandler = new WeakMap(), _GxIdeCurlInspector_initializeValidatableControls = new WeakMap(), _GxIdeCurlInspector_evaluateTooltipRender = new WeakMap(), _GxIdeCurlInspector_validateForm = new WeakMap(), _GxIdeCurlInspector_handleEditorChange = new WeakMap(), _GxIdeCurlInspector_handleSlotChange = new WeakMap(), _GxIdeCurlInspector_createHandler = new WeakMap();
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["gx-ide-curl-inspector", "gx-ide-entity-selector"];
    components.forEach(tagName => { switch (tagName) {
        case "gx-ide-curl-inspector":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, GxIdeCurlInspector$1);
            }
            break;
        case "gx-ide-entity-selector":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}

const GxIdeCurlInspector = GxIdeCurlInspector$1;
const defineCustomElement = defineCustomElement$1;

export { GxIdeCurlInspector, defineCustomElement };

//# sourceMappingURL=gx-ide-curl-inspector.js.map