'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 locale = require('./locale-ff30a5f3.js');

const mapCategoryToListItemGroup = (category) => {
    const categoryGroup = {
        caption: category.name,
        id: category.name,
        type: "group",
        expanded: true,
        expandable: true,
        items: category.types.map(type => {
            return {
                caption: type.name,
                id: type.name,
                selected: type.selected,
                additionalInformation: {
                    "stretch-start": {
                        center: [{ imgSrc: type.icon }]
                    }
                },
                type: "actionable"
            };
        })
    };
    return categoryGroup;
};
const mapDataTypesToList = (dataTypes) => {
    return dataTypes.map((dataType, index) => {
        return {
            caption: dataType.name,
            id: dataType.name,
            selected: dataType.selected,
            additionalInformation: {
                "stretch-start": {
                    center: [{ imgSrc: dataType.icon || undefined }]
                }
            },
            type: "actionable",
            order: index
        };
    });
};

const dataTypeSelectorCss = "button.button-secondary{border-color:var(--color-border-neutral-default)}:host{position:relative;display:grid;block-size:100%;overflow:auto;grid-template-rows:1fr max-content;--control-border-width:1px;--control-border-radius:4px}.main{display:contents}.selected-category{margin:0;text-transform:capitalize;margin-inline-start:16px;white-space:nowrap}.footer{overflow:auto;padding:var(--spacing-padding-m)}.list-box::part(item__action),.list-box::part(group__action){font-size:var(--font-size-body-s) !important}.pseudo-icon{inline-size:var(--icon-m);block-size:var(--icon-m);display:flex;align-items:center;justify-content:center}";

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 _GxIdeKbManagerImport__componentLocale, _GxIdeKbManagerImport_showAll, _GxIdeKbManagerImport_categoriesActionListItemGroupMap, _GxIdeKbManagerImport_dataTypeCategoryMap, _GxIdeKbManagerImport_dataTypeDataIdMap, _GxIdeKbManagerImport_lastArrowKey, _GxIdeKbManagerImport_selectedDataTypeIds, _GxIdeKbManagerImport_focusAlreadySet, _GxIdeKbManagerImport_actionListEl, _GxIdeKbManagerImport_evaluateSelectionChanged, _GxIdeKbManagerImport_listKeyDownHandler, _GxIdeKbManagerImport_listClickHandler, _GxIdeKbManagerImport_showAllObjects, _GxIdeKbManagerImport_showRecentlyObjects, _GxIdeKbManagerImport_handleButtonTab, _GxIdeKbManagerImport_renderFooter, _GxIdeKbManagerImport_setFocus;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/list-box",
    "utils/form--full",
    "utils/typography",
    "utils/layout",
    "utils/spacing",
    "chameleon/scrollbar"
];
const ARROW_UP_KEY = "ArrowUp";
const ARROW_DOWN_KEY = "ArrowDown";
const ENTER_KEY = "Enter";
const TAB_KEY = "Tab";
const ALL_CATEGORY_ID = "all";
const RECENTLY_USED_CATEGORY_ID = "recenttly-used";
const CLOCK_ICON = assetsManager.getIconPath({
    category: "system",
    name: "time",
    colorType: "primary"
});
const GxIdeKbManagerImport = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        _GxIdeKbManagerImport__componentLocale.set(this, void 0);
        _GxIdeKbManagerImport_showAll.set(this, false);
        _GxIdeKbManagerImport_categoriesActionListItemGroupMap.set(this, new Map());
        _GxIdeKbManagerImport_dataTypeCategoryMap.set(this, new Map());
        _GxIdeKbManagerImport_dataTypeDataIdMap.set(this, new Map());
        _GxIdeKbManagerImport_lastArrowKey.set(this, null);
        _GxIdeKbManagerImport_selectedDataTypeIds.set(this, {
            // For setting focus
            categoryId: undefined,
            dataTypeId: undefined
        });
        _GxIdeKbManagerImport_focusAlreadySet.set(this, false);
        _GxIdeKbManagerImport_actionListEl.set(this, void 0);
        _GxIdeKbManagerImport_evaluateSelectionChanged.set(this, (path) => {
            const chActionListItem = path.find(el => el.tagName === "CH-ACTION-LIST-ITEM");
            if (chActionListItem) {
                const id = chActionListItem.id;
                const dataTypeData = __classPrivateFieldGet(this, _GxIdeKbManagerImport_dataTypeDataIdMap, "f").get(id);
                this.dataTypeSelectedCallback(dataTypeData);
            }
        });
        _GxIdeKbManagerImport_listKeyDownHandler.set(this, (event) => {
            const pressedKey = event.key;
            const path = event.composedPath();
            if (pressedKey === ARROW_UP_KEY || pressedKey === ARROW_DOWN_KEY) {
                __classPrivateFieldSet(this, _GxIdeKbManagerImport_lastArrowKey, pressedKey, "f");
                if (pressedKey === ARROW_UP_KEY) {
                    // Check if this is the first group. IF it is, let the host know,
                    // so it can close the dialog.
                    const chActionListItem = path.find(el => el.tagName === "CH-ACTION-LIST-ITEM");
                    if (chActionListItem !== undefined) {
                        return;
                    }
                    // This is a ch-action-list-group
                    const actionGroup = path.find(el => el.tagName === "CH-ACTION-LIST-GROUP");
                    const currentFocusedActionGroupId = actionGroup.id;
                    const firstActionListGroupId = __classPrivateFieldGet(this, _GxIdeKbManagerImport_actionListEl, "f").shadowRoot.firstElementChild.id;
                    if (currentFocusedActionGroupId === firstActionListGroupId) {
                        this.arrowUpOnFirstElement();
                    }
                }
            }
            else if (pressedKey === ENTER_KEY) {
                __classPrivateFieldGet(this, _GxIdeKbManagerImport_evaluateSelectionChanged, "f").call(this, path);
            }
            else if (pressedKey === TAB_KEY) {
                event.stopPropagation();
            }
        });
        _GxIdeKbManagerImport_listClickHandler.set(this, (event) => {
            if (__classPrivateFieldGet(this, _GxIdeKbManagerImport_lastArrowKey, "f")) ;
            else if (event.detail === 0) {
                // ignore keyboard Enter (Enter happens by default on buttons on onClick)
                return;
            }
            else {
                const path = event.composedPath();
                __classPrivateFieldGet(this, _GxIdeKbManagerImport_evaluateSelectionChanged, "f").call(this, path);
            }
            __classPrivateFieldSet(this, _GxIdeKbManagerImport_lastArrowKey, null, "f");
        });
        _GxIdeKbManagerImport_showAllObjects.set(this, () => {
            if (!__classPrivateFieldGet(this, _GxIdeKbManagerImport_showAll, "f")) {
                const allCategoriesActionListModel = [];
                let counter = 1;
                const mapLength = __classPrivateFieldGet(this, _GxIdeKbManagerImport_categoriesActionListItemGroupMap, "f").size;
                for (const key of __classPrivateFieldGet(this, _GxIdeKbManagerImport_categoriesActionListItemGroupMap, "f").keys()) {
                    allCategoriesActionListModel.push(__classPrivateFieldGet(this, _GxIdeKbManagerImport_categoriesActionListItemGroupMap, "f").get(key));
                    if (counter !== mapLength) {
                        allCategoriesActionListModel.push({ type: "separator" });
                    }
                    counter++;
                }
                this.categoriesListBoxModel = allCategoriesActionListModel;
                __classPrivateFieldSet(this, _GxIdeKbManagerImport_showAll, true, "f");
                this.selectedCategory = ALL_CATEGORY_ID;
            }
        });
        _GxIdeKbManagerImport_showRecentlyObjects.set(this, () => {
            __classPrivateFieldSet(this, _GxIdeKbManagerImport_showAll, false, "f");
            this.categoriesListBoxModel = this.recentlyUsedActionListModel;
            this.selectedCategory = RECENTLY_USED_CATEGORY_ID;
        });
        this.selectedCategoryHandler = (e) => {
            this.selectedCategory = e.currentTarget.dataset.name;
            const newSelectedCategory = [
                __classPrivateFieldGet(this, _GxIdeKbManagerImport_categoriesActionListItemGroupMap, "f").get(this.selectedCategory)
            ];
            this.categoriesListBoxModel = newSelectedCategory;
            __classPrivateFieldSet(this, _GxIdeKbManagerImport_showAll, false, "f");
        };
        _GxIdeKbManagerImport_handleButtonTab.set(this, (event) => {
            // Prevents bubbling, which causes this component to close, due to how the IDE handles Tab event
            // in a parent element.
            if (event.key === TAB_KEY || event.key === ENTER_KEY) {
                event.stopPropagation();
            }
        });
        _GxIdeKbManagerImport_renderFooter.set(this, () => {
            const recentlyUsedBtnClass = this.selectedCategory === RECENTLY_USED_CATEGORY_ID
                ? "button-secondary"
                : "button-tertiary";
            const allBtnClass = this.selectedCategory === ALL_CATEGORY_ID
                ? "button-secondary"
                : "button-tertiary";
            let selectedCategoryDescription = this.selectedCategory;
            if (this.selectedCategory === RECENTLY_USED_CATEGORY_ID) {
                selectedCategoryDescription =
                    __classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").fixedTypes.recentlyUsed;
            }
            else if (this.selectedCategory === ALL_CATEGORY_ID) {
                selectedCategoryDescription = __classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").fixedTypes.all;
            }
            else {
                selectedCategoryDescription = this.selectedCategory;
            }
            return (index.h("footer", { class: "footer dialog-footer dialog-footer-start scrollable\n      " }, index.h("div", { class: "buttons-spacer" }, this.recentlyUsed && this.recentlyUsed.length > 0 && (index.h("button", { class: `button-icon-only ${recentlyUsedBtnClass}`, "aria-label": `${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").select} ${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").fixedTypes.recentlyUsed}`, title: `${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").select} ${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").fixedTypes.recentlyUsed}`, onClick: __classPrivateFieldGet(this, _GxIdeKbManagerImport_showRecentlyObjects, "f"), onKeyDown: __classPrivateFieldGet(this, _GxIdeKbManagerImport_handleButtonTab, "f") }, index.h("ch-image", { class: "icon-m", src: CLOCK_ICON }))), index.h("button", { class: `button-icon-only ${allBtnClass}`, onClick: __classPrivateFieldGet(this, _GxIdeKbManagerImport_showAllObjects, "f"), onKeyDown: __classPrivateFieldGet(this, _GxIdeKbManagerImport_handleButtonTab, "f"), "aria-label": `${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").select} ${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").fixedTypes.all}`, title: `${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").select} ${__classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").fixedTypes.all}` }, index.h("span", { class: "pseudo-icon" }, "[*]")), this.categories.map(category => {
                const buttonClass = category.name === this.selectedCategory
                    ? "button-secondary"
                    : "button-tertiary";
                return (index.h("button", { class: `button-icon-only ${buttonClass}`, "aria-label": category.name, title: category.name, onClick: this.selectedCategoryHandler, onKeyDown: __classPrivateFieldGet(this, _GxIdeKbManagerImport_handleButtonTab, "f"), "data-name": category.name }, index.h("ch-image", { class: "icon-m", src: category.icon })));
            })), index.h("p", { class: "body-regular-s selected-category" }, selectedCategoryDescription)));
        });
        _GxIdeKbManagerImport_setFocus.set(this, async () => {
            if (__classPrivateFieldGet(this, _GxIdeKbManagerImport_focusAlreadySet, "f")) {
                return;
            }
            let focusableElement;
            setTimeout(() => {
                // setTimeout as a WA, since the list-box elements are not rendered/read immediatelly
                if (this.focusType === "selected" &&
                    __classPrivateFieldGet(this, _GxIdeKbManagerImport_selectedDataTypeIds, "f").categoryId &&
                    __classPrivateFieldGet(this, _GxIdeKbManagerImport_selectedDataTypeIds, "f").dataTypeId) {
                    // set focus on the selected element
                    const selectedDataTypeEl = __classPrivateFieldGet(this, _GxIdeKbManagerImport_actionListEl, "f").shadowRoot.getElementById(__classPrivateFieldGet(this, _GxIdeKbManagerImport_selectedDataTypeIds, "f").dataTypeId);
                    if (selectedDataTypeEl) {
                        focusableElement = selectedDataTypeEl;
                    }
                }
                else {
                    // set focus of the first element
                    const firstActionListGroup = __classPrivateFieldGet(this, _GxIdeKbManagerImport_actionListEl, "f").shadowRoot.firstElementChild;
                    const firstButton = firstActionListGroup.shadowRoot.firstElementChild;
                    focusableElement = firstButton;
                }
                focusableElement && focusableElement.focus();
                __classPrivateFieldSet(this, _GxIdeKbManagerImport_focusAlreadySet, true, "f");
            }, 150);
        });
        this.categoriesListBoxModel = [];
        this.selectedCategory = undefined;
        this.recentlyUsedActionListModel = [];
        this.loading = true;
        this.categoriesJustChanged = false;
        this.actionListDidRender = false;
        this.categories = undefined;
        this.dataTypeSelectedCallback = undefined;
        this.arrowUpOnFirstElement = undefined;
        this.focusType = "first-element";
        this.recentlyUsed = [];
    }
    categoriesChanged(newCategories) {
        if (!(newCategories === null || newCategories === void 0 ? void 0 : newCategories.length)) {
            return;
        }
        __classPrivateFieldGet(this, _GxIdeKbManagerImport_categoriesActionListItemGroupMap, "f").clear();
        newCategories.forEach(category => {
            const categoryListBoxModel = mapCategoryToListItemGroup(category);
            __classPrivateFieldGet(this, _GxIdeKbManagerImport_categoriesActionListItemGroupMap, "f").set(category.name, categoryListBoxModel);
            category.types.forEach(type => {
                const isSelected = type.selected;
                if (isSelected) {
                    // For setting focus
                    __classPrivateFieldGet(this, _GxIdeKbManagerImport_selectedDataTypeIds, "f").categoryId = category.name;
                    __classPrivateFieldGet(this, _GxIdeKbManagerImport_selectedDataTypeIds, "f").dataTypeId = type.name;
                }
                __classPrivateFieldGet(this, _GxIdeKbManagerImport_dataTypeCategoryMap, "f").set(type.name, category.id);
                __classPrivateFieldGet(this, _GxIdeKbManagerImport_dataTypeDataIdMap, "f").set(type.name, {
                    id: category.id,
                    name: type.name,
                    icon: type.icon,
                    selected: type.selected
                });
            });
        });
        this.loading = false;
        __classPrivateFieldGet(this, _GxIdeKbManagerImport_showAllObjects, "f").call(this);
        this.categoriesJustChanged = true;
    }
    recentlyUsedChanged(newRecentlyUsed) {
        this.recentlyUsedActionListModel = null;
        this.recentlyUsedActionListModel = mapDataTypesToList(newRecentlyUsed);
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeKbManagerImport__componentLocale, await locale.Locale.getComponentStrings(this.el), "f");
        // initialize models
        this.recentlyUsed.length && this.recentlyUsedChanged(this.recentlyUsed);
    }
    render() {
        return (index.h(index.Host, { class: "widget" }, index.h("ch-theme", { model: CSS_BUNDLES }), this.loading ? (index.h("gx-ide-loader", { loaderTitle: __classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").loader.title, description: __classPrivateFieldGet(this, _GxIdeKbManagerImport__componentLocale, "f").loader.description, show: true })) : (index.h("section", { class: "main" }, index.h("ch-action-list-render", { ref: async (el) => {
                __classPrivateFieldSet(this, _GxIdeKbManagerImport_actionListEl, el, "f");
                __classPrivateFieldGet(this, _GxIdeKbManagerImport_setFocus, "f").call(this);
            }, class: "list-box", model: this.categoriesListBoxModel, selection: "single", onKeyDown: __classPrivateFieldGet(this, _GxIdeKbManagerImport_listKeyDownHandler, "f"), onClick: __classPrivateFieldGet(this, _GxIdeKbManagerImport_listClickHandler, "f") }), __classPrivateFieldGet(this, _GxIdeKbManagerImport_renderFooter, "f").call(this)))));
    }
    static get assetsDirs() { return ["gx-ide-assets/data-type-selector"]; }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "categories": ["categoriesChanged"],
        "recentlyUsed": ["recentlyUsedChanged"]
    }; }
};
_GxIdeKbManagerImport__componentLocale = new WeakMap(), _GxIdeKbManagerImport_showAll = new WeakMap(), _GxIdeKbManagerImport_categoriesActionListItemGroupMap = new WeakMap(), _GxIdeKbManagerImport_dataTypeCategoryMap = new WeakMap(), _GxIdeKbManagerImport_dataTypeDataIdMap = new WeakMap(), _GxIdeKbManagerImport_lastArrowKey = new WeakMap(), _GxIdeKbManagerImport_selectedDataTypeIds = new WeakMap(), _GxIdeKbManagerImport_focusAlreadySet = new WeakMap(), _GxIdeKbManagerImport_actionListEl = new WeakMap(), _GxIdeKbManagerImport_evaluateSelectionChanged = new WeakMap(), _GxIdeKbManagerImport_listKeyDownHandler = new WeakMap(), _GxIdeKbManagerImport_listClickHandler = new WeakMap(), _GxIdeKbManagerImport_showAllObjects = new WeakMap(), _GxIdeKbManagerImport_showRecentlyObjects = new WeakMap(), _GxIdeKbManagerImport_handleButtonTab = new WeakMap(), _GxIdeKbManagerImport_renderFooter = new WeakMap(), _GxIdeKbManagerImport_setFocus = new WeakMap();
GxIdeKbManagerImport.style = dataTypeSelectorCss;

exports.gx_ide_data_type_selector = GxIdeKbManagerImport;

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