'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');

/**
 * @description This function converts SelectorCategoryData[] SuggestData
 */
const convertToTreeItem = (item) => {
    var _a;
    return ({
        caption: item.name,
        id: item.id,
        items: ((_a = item.items) !== null && _a !== void 0 ? _a : []).map(convertToTreeItem),
        lazy: item.hasChildren && (item.items == null || item.items.length === 0),
        leaf: item.hasChildren === false,
        startImgSrc: item.icon
    });
};
/**
 * @description This function converts ReferenceData[] data to TreeItemData[]
 */
const convertReferenceDataToTreeViewData = (referenceData) => {
    const treeData = [];
    if (!referenceData) {
        return treeData;
    }
    referenceData.forEach(item => {
        treeData.push(convertToTreeItem(item));
    });
    return treeData;
};
const mapSelectorCategoryDataToComboBoxModel = (categoryData) => {
    return categoryData.map((category) => {
        const mappedGroup = {
            value: category.name,
            caption: category.name,
            expandable: true,
            expanded: true,
            items: category.items.map((item) => ({
                value: item.id,
                caption: item.description,
                startImgSrc: item.icon
            }))
        };
        return mappedGroup;
    });
};

const referencesCss = ":host{display:grid;block-size:100%}.opacity-0{opacity:0}.gxi-hidden{display:none !important}.align-center{display:flex;align-items:center}.align-end{display:flex;align-items:end}.overflow-auto{overflow:auto}:host(.gx-ide-component){height:100% !important;display:flex !important;flex-direction:column !important}.gx-ide-main-wrapper{color:var(--gx-ide-component-text-color);font-weight:var(--font-style-regular);font-size:var(--font-size-body-m);font-family:var(--font-family-body);height:100%;display:flex;flex-direction:column;flex-grow:1;box-sizing:border-box}.buttons-container{display:flex;gap:var(--spacing-gap-m)}.giu-static-pill-icon-and-text,.giu-static-pill{background-color:var(--elevation-background-color, var(--color-accent-surface-elevation-1));padding-inline:var(--spacing-padding-m);color:var(--color-text-neutral-default);font-size:var(--font-size-body-s);line-height:var(--line-height-relaxed);font-weight:var(--font-style-regular);border-radius:4px;min-block-size:25px;display:inline-grid;align-items:center;pointer-events:none;inline-size:max-content}.giu-static-pill-icon-and-text{display:inline-grid;grid-auto-flow:column;grid-auto-columns:max-content;gap:var(--spacing-gap-m)}.tab-full-width-buttons::part(tab-list block-start),.tab-full-width-buttons::part(tab-list block-end){grid-auto-columns:1fr}.tab-full-width-buttons::part(tab block-start){justify-content:center}.tree-view::part(item__header){--icon__system_warning_warning--disabled:var(\n    --icon__system_warning_warning--enabled\n  )}.tabular-grid{--focus-outline-width:1px}:host{display:grid;block-size:100%;grid-template-rows:max-content 1fr}.section{display:contents}.header{}.header__top{grid-template-columns:max-content 1fr max-content}.header__bottom{display:grid;grid-template-rows:1fr;transition:var(--timing-fast) grid-template-rows}.header__bottom--hidden{grid-template-rows:0fr}.header__bottom .outer-wrapper{overflow:hidden}.header__bottom .inner-wrapper{padding-top:12px;display:flex;flex-direction:row;align-items:center;justify-content:space-between}.header__bottom .inner-wrapper__left{display:flex;align-items:center;gap:var(--spacing-gap-xl)}.main{display:grid;grid-template-columns:1fr 1fr}.section__referenced-by,.section__references-to{display:grid;grid-template-rows:max-content 1fr}.dialog-header__referenced-by,.dialog-header__references-to{padding-block-start:16px}.main__referenced-by,.main__references-to{grid-auto-rows:max-content;overflow:auto}.main__referenced-by{border-inline-end:var(--section-common-border)}.text-align-center{text-align: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 _GxIdeReferences_objectDataMap, _GxIdeReferences_shortcutsSrc, _GxIdeReferences__componentLocale, _GxIdeReferences_selectObjectSuggestEl, _GxIdeReferences_evaluateInitialReferenceData, _GxIdeReferences_hideBarButtonClickedHandler, _GxIdeReferences_loadReferencesHandler, _GxIdeReferences_openObjectCallbackHandler, _GxIdeReferences_openSelectorDialogCallbackHandler, _GxIdeReferences_referenceSelectedHandler, _GxIdeReferences_referencesItemOpenReferenceCallback, _GxIdeReferences_referencesLazyLoadCallbackHandler, _GxIdeReferences_selectObjectChangedHandler, _GxIdeReferences_selectObjectInputHandler;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/button",
    "components/pills",
    "components/combo-box",
    "components/icon",
    "components/checkbox",
    "components/tree-view",
    "utils/spacing",
    "utils/layout",
    "utils/form",
    "utils/typography"
];
const GEMINI_TOOLS_SHOW_MORE_HORIZONTAL = assetsManager.getIconPath({
    category: "gemini-tools",
    name: "show-more-horizontal",
    colorType: "primary"
});
const NAVIGATION_ARROW_DOWN_SKY_BLUE = assetsManager.getIconPath({
    category: "navigation",
    name: "arrow-down-skyblue",
    colorType: "primary"
});
const SUGGEST_OPTIONS = {
    alreadyProcessed: true,
    autoExpand: false,
    hideMatchesAndShowNonMatches: false,
    highlightMatchedItems: false,
    matchCase: false,
    regularExpression: false,
    strict: false,
    renderActiveItemIconOnExpand: true
};
const GxIdeReferences = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        _GxIdeReferences_objectDataMap.set(this, new Map());
        _GxIdeReferences_shortcutsSrc.set(this, index.getAssetPath(`./gx-ide-assets/references/shortcuts.json`));
        _GxIdeReferences__componentLocale.set(this, void 0);
        _GxIdeReferences_selectObjectSuggestEl.set(this, void 0);
        /**
         * Evaluates whether to call loadReferencesCallback based on the presence of a selected object.
         */
        _GxIdeReferences_evaluateInitialReferenceData.set(this, () => {
            if (this.selectedObject) {
                this.selectedObjectChanged(this.selectedObject);
            }
        });
        /**
         * Hides or shows the bar (the bottom section of the header).
         */
        _GxIdeReferences_hideBarButtonClickedHandler.set(this, () => {
            this.barHidden = !this.barHidden;
        });
        /**
         * Invokes 'loadReferencesCallback' for 'Is Referenced By' and 'Has References To' panels.
         */
        _GxIdeReferences_loadReferencesHandler.set(this, async (id, to = false) => {
            if (this.loadReferencesCallback) {
                this.loadReferencesCallback(id, to).then(result => {
                    if (to) {
                        this.referencesToTreeData =
                            convertReferenceDataToTreeViewData(result);
                    }
                    else {
                        this.referencedByTreeData =
                            convertReferenceDataToTreeViewData(result);
                    }
                });
            }
        });
        _GxIdeReferences_openObjectCallbackHandler.set(this, () => {
            var _a;
            // Returns an empty Promise
            this.openObjectCallback((_a = this.selectedObject) === null || _a === void 0 ? void 0 : _a.id);
        });
        /**
         * Fired when user clicks the ch-suggest button (...)
         */
        _GxIdeReferences_openSelectorDialogCallbackHandler.set(this, async () => {
            const objectDataResult = await this.openSelectorDialogCallback();
            if (objectDataResult) {
                this.selectedObject = objectDataResult;
            }
        });
        _GxIdeReferences_referenceSelectedHandler.set(this, (event) => {
            if (this.selectReferenceCallback) {
                const selectedItems = event.detail.map(item => item.item.id);
                this.selectReferenceCallback(selectedItems[0]);
                // returns void
            }
        });
        _GxIdeReferences_referencesItemOpenReferenceCallback.set(this, (itemOpened) => {
            const itemId = itemOpened.detail.id;
            if (this.openObjectCallback) {
                this.openObjectCallback(itemId);
                // returns void
            }
        });
        _GxIdeReferences_referencesLazyLoadCallbackHandler.set(this, (ref) => async (treeItemId) => {
            if (this.loadReferencesCallback) {
                const to = ref === "references-to";
                const referencesLazyChildren = await this.loadReferencesCallback(treeItemId, to);
                const treeLazyChildren = convertReferenceDataToTreeViewData(referencesLazyChildren);
                return new Promise(resolve => {
                    resolve(treeLazyChildren);
                });
            }
            return new Promise(resolve => {
                resolve([]);
            });
        });
        _GxIdeReferences_selectObjectChangedHandler.set(this, (event) => {
            const newSelectedValue = event.detail;
            const objectData = __classPrivateFieldGet(this, _GxIdeReferences_objectDataMap, "f").get(newSelectedValue);
            if (objectData) {
                this.selectedObject = objectData;
            }
        });
        _GxIdeReferences_selectObjectInputHandler.set(this, (event) => {
            // WA to know if the event was from searching on the input or an item selection.
            // const partValue = (e.target as HTMLElement).getAttribute("part");
            const typedValue = event.detail;
            if (this.selectorSourceCallback) {
                this.selectorSourceCallback(typedValue).then(selectorCategoryDataArray => {
                    // update map
                    __classPrivateFieldGet(this, _GxIdeReferences_objectDataMap, "f").clear();
                    selectorCategoryDataArray === null || selectorCategoryDataArray === void 0 ? void 0 : selectorCategoryDataArray.forEach(selectorCategoryData => {
                        selectorCategoryData.items.forEach(objectData => {
                            __classPrivateFieldGet(this, _GxIdeReferences_objectDataMap, "f").set(objectData.description, {
                                id: objectData.id,
                                typeName: objectData.typeName,
                                icon: objectData.icon,
                                name: objectData.name,
                                description: objectData.description
                            });
                        });
                    });
                    this.objectsSuggestions = mapSelectorCategoryDataToComboBoxModel(selectorCategoryDataArray);
                });
            }
        });
        this.barHidden = false;
        this.objectsSuggestions = undefined;
        this.referencedByTreeData = undefined;
        this.referencesToTreeData = undefined;
        this.loadReferencesCallback = undefined;
        this.openObjectCallback = undefined;
        this.openSelectorDialogCallback = undefined;
        this.selectReferenceCallback = undefined;
        this.selectorSourceCallback = undefined;
        this.selectedObject = undefined;
    }
    selectedObjectChanged(newSelectedObject) {
        if (newSelectedObject === null || newSelectedObject === void 0 ? void 0 : newSelectedObject.id) {
            /* update "is referenced by*/
            __classPrivateFieldGet(this, _GxIdeReferences_loadReferencesHandler, "f").call(this, newSelectedObject.id, false);
            /* update "has references to*/
            __classPrivateFieldGet(this, _GxIdeReferences_loadReferencesHandler, "f").call(this, newSelectedObject.id, true);
        }
    }
    componentDidLoad() {
        __classPrivateFieldGet(this, _GxIdeReferences_selectObjectSuggestEl, "f").focus();
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeReferences__componentLocale, await locale.Locale.getComponentStrings(this.el), "f");
        __classPrivateFieldGet(this, _GxIdeReferences_evaluateInitialReferenceData, "f").call(this);
    }
    render() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        return (index.h(index.Host, { class: "widget" }, index.h("ch-theme", { model: CSS_BUNDLES }), index.h("section", { class: "section" }, index.h("header", { class: "header dialog-header-with-border spacing-body-inline spacing-body-block-start" }, index.h("div", { class: "header__top field-group" }, index.h("button", { type: "button", class: "button-tertiary button-icon-only", "aria-label": __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").header.toggleBar, part: "toggle-bar-button", onClick: __classPrivateFieldGet(this, _GxIdeReferences_hideBarButtonClickedHandler, "f") }, index.h("ch-image", { class: "icon-m", src: NAVIGATION_ARROW_DOWN_SKY_BLUE, containerRef: this.el })), index.h("div", { class: "field field-inline" }, index.h("label", {
            // select object
            class: "label", part: "select-object-label", htmlFor: "object-selector-suggest"
        }, __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").header.selectObject), index.h("ch-combo-box-render", { accessibleName: __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").header.selectObject, class: "combo-box", model: this.objectsSuggestions, suggest: true, onInput: __classPrivateFieldGet(this, _GxIdeReferences_selectObjectInputHandler, "f"), onChange: __classPrivateFieldGet(this, _GxIdeReferences_selectObjectChangedHandler, "f"), value: (_a = this.selectedObject) === null || _a === void 0 ? void 0 : _a.name, part: "object-selector-suggest", suggestOptions: SUGGEST_OPTIONS, ref: (el) => (__classPrivateFieldSet(this, _GxIdeReferences_selectObjectSuggestEl, el, "f")) })), index.h("button", { type: "button", class: "button-tertiary button-icon-only", "aria-label": __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").header.openSelectorButton, onClick: __classPrivateFieldGet(this, _GxIdeReferences_openSelectorDialogCallbackHandler, "f"), part: "open-selector-dialog-button" }, index.h("ch-image", { class: "icon-m", src: GEMINI_TOOLS_SHOW_MORE_HORIZONTAL, containerRef: this.el }))), index.h("div", { class: {
                "header__bottom": true,
                "header__bottom--hidden": this.barHidden
            } }, index.h("div", { class: "outer-wrapper" }, index.h("div", { class: "inner-wrapper" }, index.h("div", { class: "inner-wrapper__left" }, index.h("ch-image", { class: "icon-m", src: this.selectedObject.icon, containerRef: this.el }), index.h("a", { class: "text-link", onClick: __classPrivateFieldGet(this, _GxIdeReferences_openObjectCallbackHandler, "f"), part: "open-object-text-link", "aria-label": __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.selectedObjectLink }, `${(_b = this.selectedObject) === null || _b === void 0 ? void 0 : _b.name} : ${(_c = this.selectedObject) === null || _c === void 0 ? void 0 : _c.description}`)))))), index.h("div", { class: "main" }, index.h("section", { class: "section__referenced-by" }, index.h("h2", { class: "dialog-header dialog-header-with-border dialog-header__referenced-by heading-5 text-align-center spacing-body-inline subtitle-regular-xs" }, __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.isReferencedBy), index.h("div", {
            // is referenced by
            class: "main__referenced-by spacing-body-block-start spacing-body-block-end"
        }, index.h("ch-tree-view-render", { class: {
                "gxi-hidden": !((_d = this.referencedByTreeData) === null || _d === void 0 ? void 0 : _d.length),
                "tree-view": true
            }, part: "referenced-by-tree", model: this.referencedByTreeData, "aria-label": __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.referencedByTreeAccessible, lazyLoadTreeItemsCallback: __classPrivateFieldGet(this, _GxIdeReferences_referencesLazyLoadCallbackHandler, "f").call(this, "referenced-by"), onSelectedItemsChange: __classPrivateFieldGet(this, _GxIdeReferences_referenceSelectedHandler, "f"), dragDisabled: true, dropDisabled: true, onItemOpenReference: __classPrivateFieldGet(this, _GxIdeReferences_referencesItemOpenReferenceCallback, "f"), showLines: "last" }), !((_e = this.referencedByTreeData) === null || _e === void 0 ? void 0 : _e.length) && (index.h("gx-ide-empty-state", { stateTitle: ((_f = this.referencedByTreeData) === null || _f === void 0 ? void 0 : _f.length) === 0
                ? __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.isReferencedByEmpty
                : __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.isReferencedByError })))), index.h("section", { class: "section__references-to" }, index.h("h2", { class: "dialog-header dialog-header-with-border dialog-header__references-to heading-5 text-align-center spacing-body-inline subtitle-regular-xs" }, __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.hasReferencesTo), index.h("div", {
            // has references to
            class: "main__references-to spacing-body-block-start spacing-body-block-end"
        }, index.h("ch-tree-view-render", { class: {
                "gxi-hidden": !((_g = this.referencesToTreeData) === null || _g === void 0 ? void 0 : _g.length),
                "tree-view": true
            }, part: "referenced-to-tree", model: this.referencesToTreeData, "aria-label": __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.referencesToTreeAccessible, lazyLoadTreeItemsCallback: __classPrivateFieldGet(this, _GxIdeReferences_referencesLazyLoadCallbackHandler, "f").call(this, "references-to"), onSelectedItemsChange: __classPrivateFieldGet(this, _GxIdeReferences_referenceSelectedHandler, "f"), dragDisabled: true, dropDisabled: true, onItemOpenReference: __classPrivateFieldGet(this, _GxIdeReferences_referencesItemOpenReferenceCallback, "f"), showLines: "last" }), !((_h = this.referencesToTreeData) === null || _h === void 0 ? void 0 : _h.length) && (index.h("gx-ide-empty-state", { stateTitle: ((_j = this.referencesToTreeData) === null || _j === void 0 ? void 0 : _j.length) === 0
                ? __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.hasReferencesEmpty
                : __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").main.hasReferencesToError })))))), index.h("ch-shortcuts", { src: __classPrivateFieldGet(this, _GxIdeReferences_shortcutsSrc, "f") })));
    }
    static get assetsDirs() { return ["gx-ide-assets/references"]; }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "selectedObject": ["selectedObjectChanged"]
    }; }
};
_GxIdeReferences_objectDataMap = new WeakMap(), _GxIdeReferences_shortcutsSrc = new WeakMap(), _GxIdeReferences__componentLocale = new WeakMap(), _GxIdeReferences_selectObjectSuggestEl = new WeakMap(), _GxIdeReferences_evaluateInitialReferenceData = new WeakMap(), _GxIdeReferences_hideBarButtonClickedHandler = new WeakMap(), _GxIdeReferences_loadReferencesHandler = new WeakMap(), _GxIdeReferences_openObjectCallbackHandler = new WeakMap(), _GxIdeReferences_openSelectorDialogCallbackHandler = new WeakMap(), _GxIdeReferences_referenceSelectedHandler = new WeakMap(), _GxIdeReferences_referencesItemOpenReferenceCallback = new WeakMap(), _GxIdeReferences_referencesLazyLoadCallbackHandler = new WeakMap(), _GxIdeReferences_selectObjectChangedHandler = new WeakMap(), _GxIdeReferences_selectObjectInputHandler = new WeakMap();
GxIdeReferences.style = referencesCss;

exports.gx_ide_references = GxIdeReferences;

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