import { proxyCustomElement, HTMLElement, 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 { L as Locale } from './locale.js';
import { v as validateControls } from './form-validation.js';
import { d as defineCustomElement$2 } from './entity-selector.js';

const jsonImportCss = "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;grid-template-rows:1fr max-content;overflow:auto}.field-group-name-description-module{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}.radio-file-input-wrapper{grid-template-columns:max-content 1fr}div.field-file-input{grid-template-columns:1fr max-content}.json-area{display:grid;grid-template-rows:1fr max-content;overflow:auto}.slotted-container{overflow:auto;block-size:100%}.assume-varchar-checkbox{margin-inline-start:16px;margin-block-end:16px;inline-size:max-content}.input-file{display:grid;grid-template-columns:1fr max-content}.radio-group{gap:30px !important}.tooltip{height:0px}";

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 _GxIdeJsonImport_componentLocale, _GxIdeJsonImport_data, _GxIdeJsonImport_radioOptionsModel, _GxIdeJsonImport_shortcutsSrc, _GxIdeJsonImport_checkBoxEl, _GxIdeJsonImport_chShortcutsEl, _GxIdeJsonImport_descriptionEl, _GxIdeJsonImport_fileInputHiddenEl, _GxIdeJsonImport_moduleEntitySelector, _GxIdeJsonImport_nameEl, _GxIdeJsonImport_radioGroup, _GxIdeJsonImport_initializeValidatableControls, _GxIdeJsonImport_evaluateTooltipRender, _GxIdeJsonImport_validateForm, _GxIdeJsonImport_nameChangedHandler, _GxIdeJsonImport_changeRadioHandler, _GxIdeJsonImport_createHandler, _GxIdeJsonImport_removeFileSelection, _GxIdeJsonImport_selectFileInputChangedHandler, _GxIdeJsonImport_selectFileInputHandler, _GxIdeJsonImport_handleEditorChange, _GxIdeJsonImport_handleSlotChange;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/tab",
    "components/tooltip",
    "utils/form--full",
    "utils/layout",
    "utils/typography",
    "utils/spacing",
    "chameleon/scrollbar"
];
const FILE_ICON = getIconPath({
    category: "gemini-tools",
    name: "file",
    colorType: "primary"
});
const MENU_DELETE = getIconPath({
    category: "menus",
    name: "delete",
    colorType: "primary"
});
const GxIdeJsonImport$1 = /*@__PURE__*/ proxyCustomElement(class GxIdeJsonImport extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        _GxIdeJsonImport_componentLocale.set(this, void 0);
        _GxIdeJsonImport_data.set(this, {
            name: "",
            description: "",
            assumeVarcharForNull: false,
            parentId: "",
            json: ""
        });
        _GxIdeJsonImport_radioOptionsModel.set(this, []);
        _GxIdeJsonImport_shortcutsSrc.set(this, getAssetPath(`./gx-ide-assets/json-import/shortcuts.json`));
        _GxIdeJsonImport_checkBoxEl.set(this, void 0);
        _GxIdeJsonImport_chShortcutsEl.set(this, void 0);
        _GxIdeJsonImport_descriptionEl.set(this, void 0);
        _GxIdeJsonImport_fileInputHiddenEl.set(this, void 0);
        _GxIdeJsonImport_moduleEntitySelector.set(this, void 0);
        _GxIdeJsonImport_nameEl.set(this, void 0);
        _GxIdeJsonImport_radioGroup.set(this, void 0);
        _GxIdeJsonImport_initializeValidatableControls.set(this, () => {
            const validatableControls = [
                __classPrivateFieldGet(this, _GxIdeJsonImport_nameEl, "f"),
                __classPrivateFieldGet(this, _GxIdeJsonImport_moduleEntitySelector, "f")
            ];
            validatableControls.forEach(validatableControl => {
                if (validatableControl.id) {
                    this.validatableControls.set(validatableControl.id, {
                        reference: validatableControl,
                        hasError: false,
                        message: undefined
                    });
                }
            });
        });
        _GxIdeJsonImport_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));
        });
        _GxIdeJsonImport_validateForm.set(this, () => {
            var _a, _b;
            const name = ((_b = (_a = __classPrivateFieldGet(this, _GxIdeJsonImport_nameEl, "f")) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.trim()) || "";
            const json = this.radioGroupValue === "file" ? this.fileName : this.jsonContent;
            const isNameValid = this.validateNameCallback
                ? this.validateNameCallback(name)
                : Boolean(name);
            const isJsonValid = this.radioGroupValue === "file"
                ? Boolean(this.fileName)
                : this.validateJSONCallback
                    ? this.validateJSONCallback(json)
                    : false;
            this.isFormValid = isNameValid && isJsonValid;
        });
        _GxIdeJsonImport_nameChangedHandler.set(this, () => {
            __classPrivateFieldGet(this, _GxIdeJsonImport_validateForm, "f").call(this);
        });
        _GxIdeJsonImport_changeRadioHandler.set(this, () => {
            var _a;
            this.radioGroupValue = __classPrivateFieldGet(this, _GxIdeJsonImport_radioGroup, "f").value;
            if (this.radioGroupValue === "file") {
                __classPrivateFieldGet(this, _GxIdeJsonImport_checkBoxEl, "f").value = "false";
            }
            else {
                __classPrivateFieldGet(this, _GxIdeJsonImport_data, "f").json = null;
                __classPrivateFieldGet(this, _GxIdeJsonImport_removeFileSelection, "f").call(this);
                __classPrivateFieldGet(this, _GxIdeJsonImport_validateForm, "f").call(this);
            }
            (_a = this.modeChangeCallback) === null || _a === void 0 ? void 0 : _a.call(this, this.radioGroupValue);
        });
        _GxIdeJsonImport_createHandler.set(this, () => {
            var _a;
            __classPrivateFieldGet(this, _GxIdeJsonImport_data, "f").name = __classPrivateFieldGet(this, _GxIdeJsonImport_nameEl, "f").value;
            __classPrivateFieldGet(this, _GxIdeJsonImport_data, "f").description = __classPrivateFieldGet(this, _GxIdeJsonImport_descriptionEl, "f").value;
            __classPrivateFieldGet(this, _GxIdeJsonImport_data, "f").parentId = (_a = __classPrivateFieldGet(this, _GxIdeJsonImport_moduleEntitySelector, "f").value) === null || _a === void 0 ? void 0 : _a.id;
            __classPrivateFieldGet(this, _GxIdeJsonImport_data, "f").assumeVarcharForNull = __classPrivateFieldGet(this, _GxIdeJsonImport_checkBoxEl, "f").value === "true";
            this.confirmCallback(__classPrivateFieldGet(this, _GxIdeJsonImport_data, "f")).then((formSubmitResult) => {
                this.validatableControls = validateControls(formSubmitResult, this.validatableControls);
            });
        });
        _GxIdeJsonImport_removeFileSelection.set(this, () => {
            var _a;
            __classPrivateFieldGet(this, _GxIdeJsonImport_data, "f").json = null;
            __classPrivateFieldGet(this, _GxIdeJsonImport_fileInputHiddenEl, "f").value = null;
            this.fileName = null;
            (_a = this.fileChangeCallback) === null || _a === void 0 ? void 0 : _a.call(this, null);
            __classPrivateFieldGet(this, _GxIdeJsonImport_validateForm, "f").call(this);
        });
        _GxIdeJsonImport_selectFileInputChangedHandler.set(this, (event) => {
            var _a, _b;
            const target = event.target;
            if (((_a = target.files) === null || _a === void 0 ? void 0 : _a.length) > 0 && this.radioGroupValue === "file") {
                __classPrivateFieldGet(this, _GxIdeJsonImport_data, "f").json = target.files[0];
                this.fileName = target.files[0].name;
                (_b = this.fileChangeCallback) === null || _b === void 0 ? void 0 : _b.call(this, target.files[0]);
                __classPrivateFieldGet(this, _GxIdeJsonImport_validateForm, "f").call(this);
            }
        });
        _GxIdeJsonImport_selectFileInputHandler.set(this, () => {
            __classPrivateFieldGet(this, _GxIdeJsonImport_fileInputHiddenEl, "f").click();
        });
        _GxIdeJsonImport_handleEditorChange.set(this, () => {
            __classPrivateFieldGet(this, _GxIdeJsonImport_validateForm, "f").call(this);
        });
        _GxIdeJsonImport_handleSlotChange.set(this, (e) => {
            const elements = e.target.assignedElements();
            if (elements.length > 0) {
                const element = elements[0];
                element.addEventListener("json-content-change", ((event) => {
                    if (this.radioGroupValue === "text") {
                        this.jsonContent = event.detail.content;
                        __classPrivateFieldGet(this, _GxIdeJsonImport_handleEditorChange, "f").call(this);
                    }
                }));
            }
        });
        this.fileName = undefined;
        this.radioGroupValue = "file";
        this.isFormValid = false;
        this.jsonContent = "";
        this.validatableControls = new Map();
        this.cancelCallback = undefined;
        this.confirmCallback = undefined;
        this.defaultParent = undefined;
        this.defaultRadioValue = undefined;
        this.parent = undefined;
        this.selectModuleCallback = undefined;
        this.modeChangeCallback = undefined;
        this.fileChangeCallback = undefined;
        this.validateNameCallback = undefined;
        this.validateJSONCallback = undefined;
        this.onJsonContentChange = undefined;
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeJsonImport_componentLocale, await Locale.getComponentStrings(this.el), "f");
        __classPrivateFieldSet(this, _GxIdeJsonImport_radioOptionsModel, [
            {
                value: "file",
                caption: __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.filePathLabel
            },
            { value: "text", caption: __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").main.jsonAreaLabel }
        ], "f");
    }
    componentDidLoad() {
        __classPrivateFieldGet(this, _GxIdeJsonImport_initializeValidatableControls, "f").call(this);
    }
    /**
     * Suspends or reactivates the shortcuts
     */
    async suspendShortcuts(suspendShortcuts) {
        __classPrivateFieldGet(this, _GxIdeJsonImport_chShortcutsEl, "f").suspend = suspendShortcuts;
    }
    render() {
        return (h(Host, { class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("ch-shortcuts", { src: __classPrivateFieldGet(this, _GxIdeJsonImport_shortcutsSrc, "f"), ref: (el) => (__classPrivateFieldSet(this, _GxIdeJsonImport_chShortcutsEl, el, "f")) }), h("section", { class: "section" }, h("header", { class: "header field-group spacing-body dialog-header-with-border" }, h("div", { class: "field-group field-group-name-description-module" }, h("label", {
            // name
            class: "label name-label", htmlFor: "input-name"
        }, __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.nameFieldLabel), h("ch-edit", { class: "input name-input", id: "input-name", ref: (el) => (__classPrivateFieldSet(this, _GxIdeJsonImport_nameEl, el, "f")), onInput: __classPrivateFieldGet(this, _GxIdeJsonImport_nameChangedHandler, "f") }), __classPrivateFieldGet(this, _GxIdeJsonImport_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeJsonImport_nameEl, "f")), h("label", {
            // description
            class: "label description-label", htmlFor: "input-description"
        }, __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.descriptionFieldLabel), h("ch-edit", { class: "input description-input", id: "input-description", ref: (el) => (__classPrivateFieldSet(this, _GxIdeJsonImport_descriptionEl, el, "f")) }), h("label", {
            // module/folder
            class: "label module-label", htmlFor: "input-module"
        }, __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.moduleFolderLabel), h("gx-ide-entity-selector", { class: "module module-entity-selector", id: "module", value: this.defaultParent, ref: (el) => (__classPrivateFieldSet(this, _GxIdeJsonImport_moduleEntitySelector, el, "f")), labelPosition: "none", defaultValue: this.defaultParent, selectEntityCallback: this.selectModuleCallback }), __classPrivateFieldGet(this, _GxIdeJsonImport_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeJsonImport_moduleEntitySelector, "f"))), h("div", { class: "radio-file-input-wrapper field-group" }, h("ch-radio-group-render", {
            // file url/text
            class: "radio-group", model: __classPrivateFieldGet(this, _GxIdeJsonImport_radioOptionsModel, "f"), onChange: __classPrivateFieldGet(this, _GxIdeJsonImport_changeRadioHandler, "f"), value: this.defaultRadioValue, ref: (el) => (__classPrivateFieldSet(this, _GxIdeJsonImport_radioGroup, el, "f"))
        }), h("div", { class: "field field-inline field-file-input" }, h("ch-edit", { autoFocus: true, id: "file-name", class: "input", part: "file-name", type: "text", value: this.fileName, readonly: true, placeholder: __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.fileNamePlaceholder, startImgSrc: FILE_ICON, disabled: this.radioGroupValue !== "file" }), h("input", { hidden: true, type: "file", accept: ".json", onChange: __classPrivateFieldGet(this, _GxIdeJsonImport_selectFileInputChangedHandler, "f"), ref: (el) => (__classPrivateFieldSet(this, _GxIdeJsonImport_fileInputHiddenEl, el, "f")) }), h("div", { class: "buttons-spacer" }, h("button", { "aria-label": __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.removeFileSelection, title: __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.removeFileSelection, id: "reset-all-button", class: "button-tertiary button-icon-only", part: "reset-all-button", onClick: __classPrivateFieldGet(this, _GxIdeJsonImport_removeFileSelection, "f"), disabled: this.radioGroupValue !== "file" }, h("ch-image", { class: "icon-m", src: MENU_DELETE })), h("button", { id: "select-file-load-button", class: "button-primary", part: "select-file-load-button", onClick: __classPrivateFieldGet(this, _GxIdeJsonImport_selectFileInputHandler, "f"), disabled: this.radioGroupValue !== "file" }, __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").header.selectfileButton))))), h("div", { class: "main field-group" }, h("div", { class: "json-area" }, h("div", { class: "slotted-container scrollable" }, h("slot", { name: "JsonTextEditor", onSlotchange: __classPrivateFieldGet(this, _GxIdeJsonImport_handleSlotChange, "f") }))), h("ch-checkbox", { class: "checkbox assume-varchar-checkbox", caption: __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").main.checkBoxLabel, checkedValue: "true", ref: (el) => (__classPrivateFieldSet(this, _GxIdeJsonImport_checkBoxEl, el, "f")) })), h("footer", { class: "dialog-footer dialog-footer-with-border 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, _GxIdeJsonImport_componentLocale, "f").footer.btnCancel), h("button", { class: "button-primary", id: "button-ok", onClick: __classPrivateFieldGet(this, _GxIdeJsonImport_createHandler, "f"), part: "button-ok", disabled: !this.isFormValid }, __classPrivateFieldGet(this, _GxIdeJsonImport_componentLocale, "f").footer.btnOk))))));
    }
    static get assetsDirs() { return ["gx-ide-assets/json-import"]; }
    get el() { return this; }
    static get style() { return jsonImportCss; }
}, [1, "gx-ide-json-import", {
        "cancelCallback": [16],
        "confirmCallback": [16],
        "defaultParent": [16],
        "defaultRadioValue": [1, "default-radio-value"],
        "parent": [16],
        "selectModuleCallback": [16],
        "modeChangeCallback": [16],
        "fileChangeCallback": [16],
        "validateNameCallback": [16],
        "validateJSONCallback": [16],
        "onJsonContentChange": [16],
        "fileName": [32],
        "radioGroupValue": [32],
        "isFormValid": [32],
        "jsonContent": [32],
        "validatableControls": [32],
        "suspendShortcuts": [64]
    }]);
_GxIdeJsonImport_componentLocale = new WeakMap(), _GxIdeJsonImport_data = new WeakMap(), _GxIdeJsonImport_radioOptionsModel = new WeakMap(), _GxIdeJsonImport_shortcutsSrc = new WeakMap(), _GxIdeJsonImport_checkBoxEl = new WeakMap(), _GxIdeJsonImport_chShortcutsEl = new WeakMap(), _GxIdeJsonImport_descriptionEl = new WeakMap(), _GxIdeJsonImport_fileInputHiddenEl = new WeakMap(), _GxIdeJsonImport_moduleEntitySelector = new WeakMap(), _GxIdeJsonImport_nameEl = new WeakMap(), _GxIdeJsonImport_radioGroup = new WeakMap(), _GxIdeJsonImport_initializeValidatableControls = new WeakMap(), _GxIdeJsonImport_evaluateTooltipRender = new WeakMap(), _GxIdeJsonImport_validateForm = new WeakMap(), _GxIdeJsonImport_nameChangedHandler = new WeakMap(), _GxIdeJsonImport_changeRadioHandler = new WeakMap(), _GxIdeJsonImport_createHandler = new WeakMap(), _GxIdeJsonImport_removeFileSelection = new WeakMap(), _GxIdeJsonImport_selectFileInputChangedHandler = new WeakMap(), _GxIdeJsonImport_selectFileInputHandler = new WeakMap(), _GxIdeJsonImport_handleEditorChange = new WeakMap(), _GxIdeJsonImport_handleSlotChange = new WeakMap();
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["gx-ide-json-import", "gx-ide-entity-selector"];
    components.forEach(tagName => { switch (tagName) {
        case "gx-ide-json-import":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, GxIdeJsonImport$1);
            }
            break;
        case "gx-ide-entity-selector":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}

const GxIdeJsonImport = GxIdeJsonImport$1;
const defineCustomElement = defineCustomElement$1;

export { GxIdeJsonImport, defineCustomElement };

//# sourceMappingURL=gx-ide-json-import.js.map