var __classPrivateFieldGet = (this && this.__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 = (this && this.__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;
/* eslint-disable @stencil-community/own-props-must-be-private */
/* STENCIL IMPORTS */
import { getAssetPath, Host, h } from "@stencil/core";
/* OTHER LIBRARIES IMPORTS */
import { getIconPath } from "@genexus/mercury";
/* CUSTOM IMPORTS */
import { Locale } from "../../common/locale";
import { convertReferenceDataToTreeViewData, mapSelectorCategoryDataToComboBoxModel } from "./helpers";
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 = getIconPath({
    category: "gemini-tools",
    name: "show-more-horizontal",
    colorType: "primary"
});
const NAVIGATION_ARROW_DOWN_SKY_BLUE = 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
};
export class GxIdeReferences {
    constructor() {
        _GxIdeReferences_objectDataMap.set(this, new Map());
        _GxIdeReferences_shortcutsSrc.set(this, 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.getComponentStrings(this.el), "f");
        __classPrivateFieldGet(this, _GxIdeReferences_evaluateInitialReferenceData, "f").call(this);
    }
    render() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        return (h(Host, { class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("section", { class: "section" }, h("header", { class: "header dialog-header-with-border spacing-body-inline spacing-body-block-start" }, h("div", { class: "header__top field-group" }, 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") }, h("ch-image", { class: "icon-m", src: NAVIGATION_ARROW_DOWN_SKY_BLUE, containerRef: this.el })), h("div", { class: "field field-inline" }, h("label", {
            // select object
            class: "label", part: "select-object-label", htmlFor: "object-selector-suggest"
        }, __classPrivateFieldGet(this, _GxIdeReferences__componentLocale, "f").header.selectObject), 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")) })), 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" }, h("ch-image", { class: "icon-m", src: GEMINI_TOOLS_SHOW_MORE_HORIZONTAL, containerRef: this.el }))), h("div", { class: {
                "header__bottom": true,
                "header__bottom--hidden": this.barHidden
            } }, h("div", { class: "outer-wrapper" }, h("div", { class: "inner-wrapper" }, h("div", { class: "inner-wrapper__left" }, h("ch-image", { class: "icon-m", src: this.selectedObject.icon, containerRef: this.el }), 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}`)))))), h("div", { class: "main" }, h("section", { class: "section__referenced-by" }, 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), h("div", {
            // is referenced by
            class: "main__referenced-by spacing-body-block-start spacing-body-block-end"
        }, 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) && (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 })))), h("section", { class: "section__references-to" }, 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), h("div", {
            // has references to
            class: "main__references-to spacing-body-block-start spacing-body-block-end"
        }, 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) && (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 })))))), h("ch-shortcuts", { src: __classPrivateFieldGet(this, _GxIdeReferences_shortcutsSrc, "f") })));
    }
    static get is() { return "gx-ide-references"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["references.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["references.css"]
        };
    }
    static get assetsDirs() { return ["gx-ide-assets/references"]; }
    static get properties() {
        return {
            "loadReferencesCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LoadReferencesCallback",
                    "resolved": "(id: string, to: boolean) => Promise<ReferenceData[]>",
                    "references": {
                        "LoadReferencesCallback": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/references/references.tsx",
                            "id": "src/components/references/references.tsx::LoadReferencesCallback"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Callback invoked when a node is expanded in any references panels"
                }
            },
            "openObjectCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "OpenObjectCallback",
                    "resolved": "(id: string) => Promise<void>",
                    "references": {
                        "OpenObjectCallback": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/references/references.tsx",
                            "id": "src/components/references/references.tsx::OpenObjectCallback"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Callback invoked when user wants to open an object"
                }
            },
            "openSelectorDialogCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "OpenSelectorDialogCallback",
                    "resolved": "() => Promise<ObjectData>",
                    "references": {
                        "OpenSelectorDialogCallback": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/references/references.tsx",
                            "id": "src/components/references/references.tsx::OpenSelectorDialogCallback"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Callback invoked when user wants to show object selection dialog"
                }
            },
            "selectReferenceCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "SelectReferenceCallback",
                    "resolved": "(id: string) => Promise<void>",
                    "references": {
                        "SelectReferenceCallback": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/references/references.tsx",
                            "id": "src/components/references/references.tsx::SelectReferenceCallback"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Callback invoked when any item from the references panels is selected"
                }
            },
            "selectorSourceCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "SelectorSourceCallback",
                    "resolved": "(prefix: string) => Promise<SelectorCategoryData[]>",
                    "references": {
                        "SelectorSourceCallback": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/references/references.tsx",
                            "id": "src/components/references/references.tsx::SelectorSourceCallback"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Callback invoked when user writes on object selector input"
                }
            },
            "selectedObject": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "ObjectData",
                    "resolved": "{ id: string; typeName: string; icon: string; name: string; description?: string; }",
                    "references": {
                        "ObjectData": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/references/references.tsx",
                            "id": "src/components/references/references.tsx::ObjectData"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Currently selected object"
                }
            }
        };
    }
    static get states() {
        return {
            "barHidden": {},
            "objectsSuggestions": {},
            "referencedByTreeData": {},
            "referencesToTreeData": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "selectedObject",
                "methodName": "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();
//# sourceMappingURL=references.js.map
