import { r as registerInstance, g as getAssetPath, h, H as Host, a as getElement } from './index-b153bfd0.js';
import { L as Locale } from './locale-e5107fad.js';
import { c as config } from './config-5a775b7a.js';
import { g as getIconPath } from './assets-manager-7d6fc08f.js';
import './mer-animated-dots-7cbae6ef.js';
import 'lit';
import 'lit/directives/when.js';
import 'lit/directives/if-defined.js';

const dataSelectorCss = "button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;grid-template-rows:max-content 1fr max-content;overflow:auto;block-size:100%}.section{display:contents}.header__field-group{display:grid;grid-template-columns:1fr 1fr}.tabular-grid{contain:size}.data ch-tabular-grid-cell:is(:nth-child(2),:nth-child(3),:nth-child(4)){align-items:start;justify-content:start;display:inline-block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;min-inline-size:0}ch-tabular-grid.empty-result::part(main){overflow:hidden}ch-tabular-grid-rowset-empty{position:relative}";

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 _GxIdeDataSelector_firstGridObjectId, _GxIdeDataSelector_loadItemsCallbackJustCalled, _GxIdeDataSelector_componentLocale, _GxIdeDataSelector_animateEmptyState, _GxIdeDataSelector_shortcutsSrc, _GxIdeDataSelector_filterPatternEl, _GxIdeDataSelector_filterObjectEl, _GxIdeDataSelector_chShortcutsEl, _GxIdeDataSelector_chTabularGridObjectsEl, _GxIdeDataSelector_evaluateFirstRowSelection, _GxIdeDataSelector_chGridKeyDownHandler, _GxIdeDataSelector_confirmCallbackHandler, _GxIdeDataSelector_editCallbackHandler, _GxIdeDataSelector_hostKeyPressHandler, _GxIdeDataSelector_objectsSelectionChangedHandler, _GxIdeDataSelector_newVariableCallbackHandler, _GxIdeDataSelector_refreshUIWithNewConfiguration, _GxIdeDataSelector_renderFilter, _GxIdeDataSelector_renderObjects, _GxIdeDataSelector_renderEditButtons, _GxIdeDataSelector_sortObjectsByName;
// Best performance bundle
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/button",
    "components/edit",
    "components/tabular-grid",
    "components/icon",
    "utils/form",
    "utils/layout",
    "utils/spacing",
    "chameleon/scrollbar"
];
const FILTER_ICON = getIconPath({
    category: "window-tools",
    name: "filter",
    colorType: "on-surface"
});
const GxIdeDataSelector = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        _GxIdeDataSelector_firstGridObjectId.set(this, void 0); // used to select the first item after "loadItemsCallback"
        _GxIdeDataSelector_loadItemsCallbackJustCalled.set(this, false); // used to select the first item after "loadItemsCallback"
        _GxIdeDataSelector_componentLocale.set(this, void 0);
        _GxIdeDataSelector_animateEmptyState.set(this, false);
        _GxIdeDataSelector_shortcutsSrc.set(this, getAssetPath(`./gx-ide-assets/data-selector/shortcuts.json`));
        /* References needed to collect data */
        _GxIdeDataSelector_filterPatternEl.set(this, void 0);
        _GxIdeDataSelector_filterObjectEl.set(this, void 0);
        _GxIdeDataSelector_chShortcutsEl.set(this, void 0);
        _GxIdeDataSelector_chTabularGridObjectsEl.set(this, void 0);
        /**
         * After items have been loaded and rendered, the first row should be selected.
         */
        _GxIdeDataSelector_evaluateFirstRowSelection.set(this, () => {
            if (__classPrivateFieldGet(this, _GxIdeDataSelector_loadItemsCallbackJustCalled, "f")) {
                __classPrivateFieldGet(this, _GxIdeDataSelector_chTabularGridObjectsEl, "f").selectRow(__classPrivateFieldGet(this, _GxIdeDataSelector_firstGridObjectId, "f"));
            }
            __classPrivateFieldSet(this, _GxIdeDataSelector_loadItemsCallbackJustCalled, false, "f");
        });
        _GxIdeDataSelector_chGridKeyDownHandler.set(this, (e) => {
            if (e.key === "Enter") {
                __classPrivateFieldGet(this, _GxIdeDataSelector_confirmCallbackHandler, "f").call(this, e);
            }
        });
        _GxIdeDataSelector_confirmCallbackHandler.set(this, (e) => {
            e.stopPropagation();
            this.confirmCallback(this.selectedObjectsIds);
        });
        _GxIdeDataSelector_editCallbackHandler.set(this, async (e) => {
            e.stopPropagation();
            if (this.selectedObjectsIds[0]) {
                const result = await this.editCallback(this.selectedObjectsIds[0]);
                if (result) {
                    __classPrivateFieldGet(this, _GxIdeDataSelector_refreshUIWithNewConfiguration, "f").call(this);
                }
            }
        });
        _GxIdeDataSelector_hostKeyPressHandler.set(this, (e) => {
            // TODO : revise why this is necessary
            // just prevent keypress propagation
            if (e.key === "Enter") {
                e.stopPropagation();
            }
        });
        _GxIdeDataSelector_objectsSelectionChangedHandler.set(this, (event) => {
            this.selectedObjectsIds = event.detail.rowsId;
        });
        _GxIdeDataSelector_newVariableCallbackHandler.set(this, async () => {
            const response = await this.newVariableCallback();
            if (response) {
                __classPrivateFieldGet(this, _GxIdeDataSelector_refreshUIWithNewConfiguration, "f").call(this);
            }
        });
        _GxIdeDataSelector_refreshUIWithNewConfiguration.set(this, () => {
            var _a, _b, _c;
            this.objects = [];
            const filters = {
                pattern: (_a = __classPrivateFieldGet(this, _GxIdeDataSelector_filterPatternEl, "f")) === null || _a === void 0 ? void 0 : _a.value,
                object: (_c = (_b = __classPrivateFieldGet(this, _GxIdeDataSelector_filterObjectEl, "f")) === null || _b === void 0 ? void 0 : _b.value) === null || _c === void 0 ? void 0 : _c.id
            };
            this.loading = true;
            this.loadItemsCallback(filters).then((items) => {
                if (items.length) {
                    this.objects = __classPrivateFieldGet(this, _GxIdeDataSelector_sortObjectsByName, "f").call(this, items);
                    __classPrivateFieldSet(this, _GxIdeDataSelector_firstGridObjectId, this.objects[0].id, "f");
                    __classPrivateFieldSet(this, _GxIdeDataSelector_loadItemsCallbackJustCalled, true, "f");
                }
                this.selectedObjectsIds = [];
                this.loading = false;
            });
        });
        _GxIdeDataSelector_renderFilter.set(this, () => {
            return [
                h("div", { class: "header__field-group field-group" }, h("div", { class: "field field-block pattern-field" }, h("label", { class: "label", htmlFor: "filter-pattern" }, __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").filter.pattern), h("ch-edit", { autoFocus: true, id: "filter-pattern", name: "filter-pattern", class: "input pattern-input", part: "filter-pattern", debounce: config.inputDebounce, onInput: __classPrivateFieldGet(this, _GxIdeDataSelector_refreshUIWithNewConfiguration, "f"), ref: (el) => (__classPrivateFieldSet(this, _GxIdeDataSelector_filterPatternEl, el, "f")) })), h("gx-ide-entity-selector", { id: "object-selector", value: this.defaultModule, defaultValue: this.defaultModule, selectEntityCallback: this.selectObjectCallback, ref: (el) => (__classPrivateFieldSet(this, _GxIdeDataSelector_filterObjectEl, el, "f")), class: "entity", onValueChanged: __classPrivateFieldGet(this, _GxIdeDataSelector_refreshUIWithNewConfiguration, "f") }))
            ];
        });
        _GxIdeDataSelector_renderObjects.set(this, () => {
            const gridIsEmpty = !this.objects.length;
            const displayLoader = this.loader && this.loading;
            if (!gridIsEmpty || displayLoader) {
                __classPrivateFieldSet(this, _GxIdeDataSelector_animateEmptyState, true, "f"); // improve performance if is first render
            }
            return (h("ch-tabular-grid", { class: {
                    "tabular-grid": true,
                    "empty-result": this.objects.length === 0,
                    "data": true
                }, rowSelectionMode: this.selectionType, onKeyDown: __classPrivateFieldGet(this, _GxIdeDataSelector_chGridKeyDownHandler, "f"), part: "ch-grid-objects", onSelectionChanged: this.objects.length ? __classPrivateFieldGet(this, _GxIdeDataSelector_objectsSelectionChangedHandler, "f") : undefined, ref: (el) => (__classPrivateFieldSet(this, _GxIdeDataSelector_chTabularGridObjectsEl, el, "f")) }, h("ch-tabular-grid-columnset", { class: "tabular-grid-column-set" }, h("ch-tabular-grid-column", { "column-name-position": "text", settingable: false, sortable: false, size: config.tabularGrid.colSize.maxContent, class: "tabular-grid-column" }), h("ch-tabular-grid-column", { "column-name": __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").tableHead.name, "column-name-position": "text", settingable: false, sortable: false, size: config.tabularGrid.colSize.common, class: "tabular-grid-column" }), h("ch-tabular-grid-column", { "column-name": __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").tableHead.dataType, "column-name-position": "text", settingable: false, sortable: false, size: config.tabularGrid.colSize.common, class: "tabular-grid-column" }), h("ch-tabular-grid-column", { "column-name": __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").tableHead.description, "column-name-position": "text", settingable: false, sortable: false, size: config.tabularGrid.colSize.common, class: "tabular-grid-column" })), !gridIsEmpty ? (h("ch-tabular-grid-rowset", { class: "tabular-grid-rowset" }, this.objects.map((obj) => (h("ch-tabular-grid-row", { rowid: obj.id, onDblClick: __classPrivateFieldGet(this, _GxIdeDataSelector_confirmCallbackHandler, "f"), class: "tabular-grid-row" }, h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, h("ch-image", { src: obj.type === "attribute"
                    ? "objects/attribute"
                    : "objects-parts/variables", class: "icon-s" })), h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, obj.name), h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, obj.dataType), h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, obj.description)))))) : (h("ch-tabular-grid-rowset", { class: "tabular-grid-rowset" }, h("ch-tabular-grid-rowset-empty", null, displayLoader ? (h("gx-ide-loader", { loaderTitle: __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").loader.title, show: true })) : (h("gx-ide-empty-state", { isAnimated: __classPrivateFieldGet(this, _GxIdeDataSelector_animateEmptyState, "f"), stateIconSrc: FILTER_ICON, stateTitle: __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").emptyState.title })))))));
        });
        _GxIdeDataSelector_renderEditButtons.set(this, () => {
            if (!this.editCallback && !this.newVariableCallback) {
                return null;
            }
            const newVariableButtonEnabled = this.newVariableCallback && !this.loading;
            const editButtonEnabled = this.editCallback && !this.loading && this.selectedObjectsIds.length;
            return (h("div", { class: "buttons-spacer" }, this.newVariableCallback ? (h("button", {
                // New Variable Button
                class: "button-secondary", disabled: !newVariableButtonEnabled, part: "button button-new", type: "button", onClick: __classPrivateFieldGet(this, _GxIdeDataSelector_newVariableCallbackHandler, "f")
            }, __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").footer.btnNew)) : ([]), this.editCallback ? (h("button", {
                // Edit Button
                class: "button-secondary", disabled: !editButtonEnabled, part: "button button-edit", type: "button", onClick: editButtonEnabled && __classPrivateFieldGet(this, _GxIdeDataSelector_editCallbackHandler, "f")
            }, __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").footer.btnEdit)) : ([])));
        });
        _GxIdeDataSelector_sortObjectsByName.set(this, (objects) => 
        // TODO (make this a common helper function
        // and use it in other dialog grids that
        // require filter by name).
        objects.sort((a, b) => {
            const nameA = a.name.toLowerCase(), nameB = b.name.toLowerCase();
            if (nameA < nameB) {
                return -1;
            }
            if (nameA > nameB) {
                return 1;
            }
            return 0;
        }));
        this.loading = true;
        this.objects = [];
        this.selectedObjectsIds = [];
        this.cancelCallback = undefined;
        this.confirmCallback = undefined;
        this.defaultModule = undefined;
        this.displayTitle = false;
        this.editCallback = undefined;
        this.loadItemsCallback = undefined;
        this.loader = false;
        this.newVariableCallback = undefined;
        this.selectObjectCallback = undefined;
        this.selectionType = "multiple";
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeDataSelector_componentLocale, await Locale.getComponentStrings(this.el), "f");
    }
    connectedCallback() {
        __classPrivateFieldGet(this, _GxIdeDataSelector_refreshUIWithNewConfiguration, "f").call(this);
    }
    componentDidLoad() {
        __classPrivateFieldGet(this, _GxIdeDataSelector_filterPatternEl, "f").focus();
    }
    componentDidRender() {
        __classPrivateFieldGet(this, _GxIdeDataSelector_evaluateFirstRowSelection, "f").call(this);
    }
    /**
     * Suspends or reactivates the shortcuts
     */
    async suspendShortcuts(suspendShortcuts) {
        if (suspendShortcuts) {
            __classPrivateFieldGet(this, _GxIdeDataSelector_chShortcutsEl, "f").suspend = true;
        }
        else {
            __classPrivateFieldGet(this, _GxIdeDataSelector_chShortcutsEl, "f").suspend = false;
        }
    }
    /**
     * Validate necessary data input
     */
    async validate() {
        const isValid = true;
        return isValid;
    }
    render() {
        const okButtonEnabled = !this.loading;
        const footerJustifySpaceBetween = !!this.newVariableCallback || !!this.editCallback;
        return (h(Host, { onKeyPress: __classPrivateFieldGet(this, _GxIdeDataSelector_hostKeyPressHandler, "f"), class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("ch-shortcuts", { src: __classPrivateFieldGet(this, _GxIdeDataSelector_shortcutsSrc, "f"), ref: (el) => (__classPrivateFieldSet(this, _GxIdeDataSelector_chShortcutsEl, el, "f")) }), h("section", { class: "section" }, h("header", { class: "header dialog-header-with-border spacing-body-block-start spacing-body-inline" }, __classPrivateFieldGet(this, _GxIdeDataSelector_renderFilter, "f").call(this)), __classPrivateFieldGet(this, _GxIdeDataSelector_renderObjects, "f").call(this), h("footer", { class: {
                "footer": true,
                "dialog-footer-with-border": true,
                "dialog-footer-space-between": footerJustifySpaceBetween,
                "spacing-body-inline": true,
                "spacing-body-block-end": true
            } }, __classPrivateFieldGet(this, _GxIdeDataSelector_renderEditButtons, "f").call(this), h("div", { class: "buttons-spacer" }, h("button", {
            // Cancel Button
            class: "button-secondary", part: "button button-cancel", type: "button", onClick: this.cancelCallback
        }, __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").footer.btnCancel), h("button", {
            // Confirm Button
            class: "button-primary", part: "button button-ok", disabled: !okButtonEnabled, type: "button", onClick: okButtonEnabled && __classPrivateFieldGet(this, _GxIdeDataSelector_confirmCallbackHandler, "f")
        }, __classPrivateFieldGet(this, _GxIdeDataSelector_componentLocale, "f").footer.btnConfirm))))));
    }
    static get delegatesFocus() { return true; }
    static get assetsDirs() { return ["gx-ide-assets/data-selector"]; }
    get el() { return getElement(this); }
};
_GxIdeDataSelector_firstGridObjectId = new WeakMap(), _GxIdeDataSelector_loadItemsCallbackJustCalled = new WeakMap(), _GxIdeDataSelector_componentLocale = new WeakMap(), _GxIdeDataSelector_animateEmptyState = new WeakMap(), _GxIdeDataSelector_shortcutsSrc = new WeakMap(), _GxIdeDataSelector_filterPatternEl = new WeakMap(), _GxIdeDataSelector_filterObjectEl = new WeakMap(), _GxIdeDataSelector_chShortcutsEl = new WeakMap(), _GxIdeDataSelector_chTabularGridObjectsEl = new WeakMap(), _GxIdeDataSelector_evaluateFirstRowSelection = new WeakMap(), _GxIdeDataSelector_chGridKeyDownHandler = new WeakMap(), _GxIdeDataSelector_confirmCallbackHandler = new WeakMap(), _GxIdeDataSelector_editCallbackHandler = new WeakMap(), _GxIdeDataSelector_hostKeyPressHandler = new WeakMap(), _GxIdeDataSelector_objectsSelectionChangedHandler = new WeakMap(), _GxIdeDataSelector_newVariableCallbackHandler = new WeakMap(), _GxIdeDataSelector_refreshUIWithNewConfiguration = new WeakMap(), _GxIdeDataSelector_renderFilter = new WeakMap(), _GxIdeDataSelector_renderObjects = new WeakMap(), _GxIdeDataSelector_renderEditButtons = new WeakMap(), _GxIdeDataSelector_sortObjectsByName = new WeakMap();
GxIdeDataSelector.style = dataSelectorCss;

export { GxIdeDataSelector as gx_ide_data_selector };

//# sourceMappingURL=gx-ide-data-selector.entry.js.map