'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-75aef097.js');
const assetsManager = require('./assets-manager-089e91b1.js');
require('./mer-animated-dots-78490ca4.js');
require('lit');
require('lit/directives/when.js');
require('lit/directives/if-defined.js');
const config = require('./config-3bb95f39.js');
const locale = require('./locale-ff30a5f3.js');

const mapOptionsToComboBoxItemModel = (options) => {
    return options.map(option => {
        var _a;
        return {
            value: option.id,
            caption: option.label,
            startImgSrc: (_a = option.iconName) !== null && _a !== void 0 ? _a : ""
        };
    });
};

const wwAttributesCss = ":host{display:grid;block-size:100%;overflow:auto;grid-template-rows:max-content 1fr max-content}.section{display:contents}.header{grid-template:\"name type\" max-content \"object object\" max-content;grid-template-columns:1fr 1fr}.name__field{grid-area:name}.name__label{inline-size:40px}.type__field{grid-area:type}.more__btn{grid-area:more;inline-size:64px}.object-selector__field{grid-area:object}.main{overflow:auto}ch-tabular-grid.tabular-grid--empty::part(main){overflow:hidden}ch-tabular-grid-rowset-empty{position:relative}.tabular-grid-row 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}.matching-attributes{margin: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 _GxIdeWWAttributes__componentLocale, _GxIdeWWAttributes_chGridEl, _GxIdeWWAttributes_contextMenuCallbackHandler, _GxIdeWWAttributes_deleteSelectionCallbackHandle, _GxIdeWWAttributes_deselectAll, _GxIdeWWAttributes_getAttributes, _GxIdeWWAttributes_listenChanges, _GxIdeWWAttributes_openSelectionCallbackHandle, _GxIdeWWAttributes_entityCallbackHandler, _GxIdeWWAttributes_entityValueChangedHandler, _GxIdeWWAttributes_renderAttributesGrid, _GxIdeWWAttributes_evaluateAttributesGridContent, _GxIdeWWAttributes_renderFilter, _GxIdeWWAttributes_selectAll, _GxIdeWWAttributes_nameInputHandler, _GxIdeWWAttributes_typeInputHandler;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/button",
    "components/edit",
    "components/combo-box",
    "components/tabular-grid",
    "components/icon",
    "utils/form",
    "utils/layout",
    "utils/spacing",
    "chameleon/scrollbar"
];
const FILTER_ICON = assetsManager.getIconPath({
    category: "window-tools",
    name: "filter",
    colorType: "on-surface"
});
const GxIdeWWAttributes = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        /**
         * Component hard-coded strings translations.
         */
        _GxIdeWWAttributes__componentLocale.set(this, void 0);
        /* References needed to collect data */
        _GxIdeWWAttributes_chGridEl.set(this, void 0);
        _GxIdeWWAttributes_contextMenuCallbackHandler.set(this, (ev) => {
            this.contextMenuCallback({
                selection: this.selectedAttributesIds,
                clientX: ev.clientX,
                clientY: ev.clientY
            });
        });
        _GxIdeWWAttributes_deleteSelectionCallbackHandle.set(this, () => {
            this.deleteSelectionCallback(this.selectedAttributesIds);
        });
        _GxIdeWWAttributes_deselectAll.set(this, () => {
            __classPrivateFieldGet(this, _GxIdeWWAttributes_chGridEl, "f").selectAllRows(false);
        });
        _GxIdeWWAttributes_getAttributes.set(this, () => {
            var _a;
            this.loading = true;
            const filters = {
                name: this.name.trim(),
                type: this.type,
                object: (_a = this.object) === null || _a === void 0 ? void 0 : _a.id
            };
            this.loadCallback(filters).then((items) => {
                var _a;
                this.attributes = items;
                if (((_a = this.selectedAttributesIds) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                    this.selectedAttributesIds = [];
                    __classPrivateFieldGet(this, _GxIdeWWAttributes_deselectAll, "f").call(this);
                }
                this.loading = false;
            });
        });
        _GxIdeWWAttributes_listenChanges.set(this, () => {
            // for grid selection
            __classPrivateFieldGet(this, _GxIdeWWAttributes_chGridEl, "f").addEventListener("selectionChanged", (ev) => {
                this.selectedAttributesIds = ev.detail.rowsId;
                this.selectionChangeCallback(this.selectedAttributesIds);
            });
            __classPrivateFieldGet(this, _GxIdeWWAttributes_chGridEl, "f").addEventListener("contextmenu", (ev) => {
                ev.preventDefault();
                ev.stopPropagation();
                __classPrivateFieldGet(this, _GxIdeWWAttributes_contextMenuCallbackHandler, "f").call(this, ev);
            });
            __classPrivateFieldGet(this, _GxIdeWWAttributes_chGridEl, "f").addEventListener("rowDoubleClicked", () => {
                __classPrivateFieldGet(this, _GxIdeWWAttributes_openSelectionCallbackHandle, "f").call(this);
            });
        });
        _GxIdeWWAttributes_openSelectionCallbackHandle.set(this, () => {
            this.openSelectionCallback(this.selectedAttributesIds);
        });
        _GxIdeWWAttributes_entityCallbackHandler.set(this, async () => {
            const result = await this.objectActionCallback();
            this.object = {
                id: result.id,
                name: result.name,
                iconSrc: result.iconSrc
            };
            return new Promise(resolve => {
                resolve(null);
            });
        });
        _GxIdeWWAttributes_entityValueChangedHandler.set(this, (event) => {
            this.object = event.detail;
            __classPrivateFieldGet(this, _GxIdeWWAttributes_getAttributes, "f").call(this);
        });
        _GxIdeWWAttributes_renderAttributesGrid.set(this, () => {
            var _a;
            const tabularGridEmpty = ((_a = this.attributes) === null || _a === void 0 ? void 0 : _a.length) === 0 || !this.attributes;
            return (index.h("ch-tabular-grid", { class: {
                    "tabular-grid": true,
                    "tabular-grid--empty": tabularGridEmpty,
                    "main": true
                }, ref: (el) => (__classPrivateFieldSet(this, _GxIdeWWAttributes_chGridEl, el, "f")), "row-selection-mode": "multiple", part: "ch-grid-attributes" }, index.h("ch-tabular-grid-columnset", { class: "tabular-grid-column-set" }, index.h("ch-tabular-grid-column", { "column-name": "", "column-name-position": "text", settingable: false, sortable: false, size: config.config.tabularGrid.colSize.maxContent, class: "tabular-grid-column" }), index.h("ch-tabular-grid-column", { "column-name": __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").tableHead.name, "column-name-position": "text", size: config.config.tabularGrid.colSize.auto, class: "tabular-grid-column", settingable: false, sortable: false }), index.h("ch-tabular-grid-column", { "column-name": __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").tableHead.type, "column-name-position": "text", class: "tabular-grid-column", size: config.config.tabularGrid.colSize.auto, settingable: false, sortable: false }), index.h("ch-tabular-grid-column", { "column-name": __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").tableHead.description, "column-name-position": "text", size: config.config.tabularGrid.colSize.auto, class: "tabular-grid-column", settingable: false, sortable: false })), __classPrivateFieldGet(this, _GxIdeWWAttributes_evaluateAttributesGridContent, "f").call(this)));
        });
        _GxIdeWWAttributes_evaluateAttributesGridContent.set(this, () => {
            if (this.loading) {
                return (index.h("ch-tabular-grid-rowset", { class: "tabular-grid-rowset" }, index.h("ch-tabular-grid-rowset-empty", null, index.h("gx-ide-loader", { loaderTitle: __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").loader.title, description: __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").loader.description, show: true }))));
            }
            else if (this.attributes.length) {
                return (index.h("ch-tabular-grid-rowset", { class: "tabular-grid-rowset" }, this.attributes.map((attr) => (index.h("ch-tabular-grid-row", { class: "tabular-grid-row", rowid: attr.id }, index.h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, index.h("ch-image", { class: "icon-xs", src: attr.icon })), index.h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, attr.name), index.h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, attr.type), index.h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, attr.description))))));
            }
            else {
                // filter returned none
                return (index.h("ch-tabular-grid-rowset", { class: "tabular-grid-rowset" }, index.h("ch-tabular-grid-rowset-empty", null, index.h("gx-ide-empty-state", { stateIconSrc: FILTER_ICON, stateTitle: __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").emptyStateTitle, isAnimated: true }))));
            }
        });
        _GxIdeWWAttributes_renderFilter.set(this, () => {
            var _a;
            return (index.h("header", { class: "header field-group dialog-header-with-border spacing-body" }, index.h("div", { class: "field field-inline name__field" }, index.h("label", { class: "label name__label", htmlFor: "name" }, __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").filter.name), index.h("ch-edit", {
                // Name
                id: "name", class: "input", value: this.name, debounce: 300, onInput: __classPrivateFieldGet(this, _GxIdeWWAttributes_nameInputHandler, "f"), part: "filter-name"
            })), index.h("div", { class: "field field-inline type__field" }, index.h("label", { class: "label", htmlFor: "type" }, __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").filter.type), index.h("ch-combo-box-render", {
                // Type
                id: "type", class: "combo-box", disabled: !this.types, value: this.type, model: mapOptionsToComboBoxItemModel((_a = this.types) !== null && _a !== void 0 ? _a : []), part: "filter-type", onInput: __classPrivateFieldGet(this, _GxIdeWWAttributes_typeInputHandler, "f")
            })), index.h("div", { class: "field field-inline object-selector__field" }, index.h("label", { class: "label", htmlFor: "object-selector" }, __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").filter.object), index.h("gx-ide-entity-selector", {
                // Object
                id: "object-selector", value: this.object, labelPosition: "none", defaultValue: null, selectEntityCallback: __classPrivateFieldGet(this, _GxIdeWWAttributes_entityCallbackHandler, "f"), onValueChanged: __classPrivateFieldGet(this, _GxIdeWWAttributes_entityValueChangedHandler, "f"), part: "filter-module-folder"
            }))));
        });
        _GxIdeWWAttributes_selectAll.set(this, () => {
            __classPrivateFieldGet(this, _GxIdeWWAttributes_chGridEl, "f").selectAllRows();
        });
        _GxIdeWWAttributes_nameInputHandler.set(this, (e) => {
            this.name = e.detail.trim();
            __classPrivateFieldGet(this, _GxIdeWWAttributes_getAttributes, "f").call(this);
        });
        _GxIdeWWAttributes_typeInputHandler.set(this, (e) => {
            this.type = e.detail;
            __classPrivateFieldGet(this, _GxIdeWWAttributes_getAttributes, "f").call(this);
        });
        this.attributes = undefined;
        this.selectedAttributesIds = [];
        this.filterMore = false;
        this.name = "";
        this.loading = true;
        this.object = undefined;
        this.type = undefined;
        this.contextMenuCallback = undefined;
        this.deleteSelectionCallback = undefined;
        this.loadCallback = undefined;
        this.objectActionCallback = undefined;
        this.objects = undefined;
        this.openSelectionCallback = undefined;
        this.selectionChangeCallback = undefined;
        this.types = undefined;
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeWWAttributes__componentLocale, await locale.Locale.getComponentStrings(this.el), "f");
    }
    componentDidLoad() {
        __classPrivateFieldGet(this, _GxIdeWWAttributes_listenChanges, "f").call(this);
        this.type = this.types[0].id; // before #getAttributes
        __classPrivateFieldGet(this, _GxIdeWWAttributes_getAttributes, "f").call(this);
    }
    keyDownHandler(eventInfo) {
        if (document.activeElement === this.el) {
            switch (eventInfo.key) {
                case "Enter":
                    __classPrivateFieldGet(this, _GxIdeWWAttributes_openSelectionCallbackHandle, "f").call(this);
                    break;
                case "Delete":
                    __classPrivateFieldGet(this, _GxIdeWWAttributes_deleteSelectionCallbackHandle, "f").call(this);
                    break;
            }
        }
    }
    /**
     * Reloads the view, refreshing the filters and attributes table.
     */
    async reload() {
        this.name = "";
        this.type = this.types[0].id;
        this.object = null;
        __classPrivateFieldGet(this, _GxIdeWWAttributes_getAttributes, "f").call(this);
    }
    /**
     * Validate necessary data input
     */
    async validate() {
        const isValid = true;
        return isValid;
    }
    render() {
        var _a, _b;
        const btnDisabled = !this.attributes || this.attributes.length === 0;
        const matchingAttributesLabel = ((_a = this.attributes) === null || _a === void 0 ? void 0 : _a.length) === 1
            ? __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").matchingAttribute
            : __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").matchingAttributes;
        const attributesLength = ((_b = this.attributes) === null || _b === void 0 ? void 0 : _b.length) || 0;
        return (index.h(index.Host, { class: "widget" }, index.h("ch-theme", { model: CSS_BUNDLES }), index.h("section", { class: "section" }, __classPrivateFieldGet(this, _GxIdeWWAttributes_renderFilter, "f").call(this), __classPrivateFieldGet(this, _GxIdeWWAttributes_renderAttributesGrid, "f").call(this), index.h("footer", { class: "dialog-footer dialog-footer-with-border spacing-body-block-end spacing-body-inline dialog-footer-space-between" }, index.h("p", { class: "body-regular-s matching-attributes" }, `${attributesLength} ${matchingAttributesLabel}`), index.h("button", { id: "button-select-all", class: "button-primary", onClick: __classPrivateFieldGet(this, _GxIdeWWAttributes_selectAll, "f"), disabled: btnDisabled, part: "button button--select-all" }, __classPrivateFieldGet(this, _GxIdeWWAttributes__componentLocale, "f").footer.btnSelectAll)))));
    }
    static get assetsDirs() { return ["gx-ide-assets/ww-attributes"]; }
    get el() { return index.getElement(this); }
};
_GxIdeWWAttributes__componentLocale = new WeakMap(), _GxIdeWWAttributes_chGridEl = new WeakMap(), _GxIdeWWAttributes_contextMenuCallbackHandler = new WeakMap(), _GxIdeWWAttributes_deleteSelectionCallbackHandle = new WeakMap(), _GxIdeWWAttributes_deselectAll = new WeakMap(), _GxIdeWWAttributes_getAttributes = new WeakMap(), _GxIdeWWAttributes_listenChanges = new WeakMap(), _GxIdeWWAttributes_openSelectionCallbackHandle = new WeakMap(), _GxIdeWWAttributes_entityCallbackHandler = new WeakMap(), _GxIdeWWAttributes_entityValueChangedHandler = new WeakMap(), _GxIdeWWAttributes_renderAttributesGrid = new WeakMap(), _GxIdeWWAttributes_evaluateAttributesGridContent = new WeakMap(), _GxIdeWWAttributes_renderFilter = new WeakMap(), _GxIdeWWAttributes_selectAll = new WeakMap(), _GxIdeWWAttributes_nameInputHandler = new WeakMap(), _GxIdeWWAttributes_typeInputHandler = new WeakMap();
GxIdeWWAttributes.style = wwAttributesCss;

exports.gx_ide_ww_attributes = GxIdeWWAttributes;

//# sourceMappingURL=gx-ide-ww-attributes.cjs.entry.js.map