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 __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 _GxIdeSearch_componentLocale, _GxIdeSearch_searchInputValue, _GxIdeSearch_searchInputHandler, _GxIdeSearch_searchInputKeyDownHandler, _GxIdeSearch_mapSearchResultToTreeViewModel, _GxIdeSearch_treeItemDoubleClickHandler, _GxIdeSearch_renderSearchResults;
import { h, Host } from "@stencil/core";
import { getIconPath } from "@genexus/mercury";
import { config } from "../../common/config";
import { Locale } from "../../common/locale";
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/edit",
    "components/tree-view",
    "utils/typography",
    "utils/spacing",
    "chameleon/scrollbar"
];
const SEARCH_ICON = getIconPath({
    category: "system",
    name: "search",
    colorType: "on-surface"
});
const INFORMATION_ICON = getIconPath({
    category: "system",
    name: "information",
    colorType: "on-interactive"
});
export class GxIdeSearch {
    constructor() {
        _GxIdeSearch_componentLocale.set(this, void 0);
        _GxIdeSearch_searchInputValue.set(this, "");
        _GxIdeSearch_searchInputHandler.set(this, async (event) => {
            var _a;
            if (!this.isKbOpen) {
                return;
            }
            const inputElement = event.target;
            const searchText = (_a = inputElement === null || inputElement === void 0 ? void 0 : inputElement.value) === null || _a === void 0 ? void 0 : _a.trim();
            __classPrivateFieldSet(this, _GxIdeSearch_searchInputValue, searchText, "f");
            if (!searchText) {
                this.searchResultModel = [];
                this.hasSearched = false;
                this.isSearching = false;
                return;
            }
            this.hasSearched = true;
            this.isSearching = true;
            this.searchResultModel = [];
            try {
                const searchResultData = await this.searchCallback({
                    text: searchText,
                    onlyTitles: false,
                    includeSubModules: true,
                    allBranches: false,
                    forLLM: false
                });
                this.searchResultModel =
                    __classPrivateFieldGet(this, _GxIdeSearch_mapSearchResultToTreeViewModel, "f").call(this, searchResultData);
            }
            finally {
                this.isSearching = false;
            }
        });
        _GxIdeSearch_searchInputKeyDownHandler.set(this, (event) => {
            if (event.key === "Enter") {
                __classPrivateFieldGet(this, _GxIdeSearch_searchInputHandler, "f").call(this, event);
            }
        });
        _GxIdeSearch_mapSearchResultToTreeViewModel.set(this, (searchResultData) => {
            if (!searchResultData || searchResultData.length === 0) {
                return [];
            }
            return [
                {
                    id: "root",
                    caption: `${__classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").labels.results} (${searchResultData.length})`,
                    expanded: true,
                    parts: "root-item",
                    items: searchResultData.map((item) => ({
                        id: item.objectId,
                        startImgSrc: item.objectIcon,
                        caption: item.objectName,
                        leaf: true,
                        parts: "search-item"
                    }))
                }
            ];
        });
        _GxIdeSearch_treeItemDoubleClickHandler.set(this, async (event) => {
            var _a;
            const itemId = event.detail.id;
            await ((_a = this.itemDoubleClickCallback) === null || _a === void 0 ? void 0 : _a.call(this, itemId));
        });
        _GxIdeSearch_renderSearchResults.set(this, () => {
            if (!this.isKbOpen) {
                return (h("gx-ide-empty-state", { stateTitle: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").noKbOpenState.title, stateDescription: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").noKbOpenState.description, stateIconSrc: INFORMATION_ICON, isAnimated: true }));
            }
            if (this.isSearching) {
                return (h("gx-ide-loader", { loaderTitle: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").loader.title, show: true }));
            }
            if (this.searchResultModel.length > 0) {
                return (h("ch-tree-view-render", { class: "tree-view", model: this.searchResultModel, expandableButton: "no", expandOnClick: false, onItemOpenReference: __classPrivateFieldGet(this, _GxIdeSearch_treeItemDoubleClickHandler, "f") }));
            }
            if (this.hasSearched) {
                return (h("gx-ide-empty-state", { stateTitle: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").emptyState.title, stateDescription: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").emptyState.description, stateIconSrc: INFORMATION_ICON, isAnimated: true }));
            }
            return null;
        });
        this.searchResultModel = [];
        this.hasSearched = false;
        this.isSearching = false;
        this.isKbOpen = false;
        this.searchCallback = undefined;
        this.itemDoubleClickCallback = undefined;
    }
    valueChangeHandler(newValue) {
        if (!newValue) {
            __classPrivateFieldSet(this, _GxIdeSearch_searchInputValue, "", "f");
            this.searchResultModel = [];
            this.hasSearched = false;
            this.isSearching = false;
        }
    }
    /**
     * Method to show or hide the settings panel, can be called from the outside to control the visibility of the settings
     */
    async showSettings(_show) {
        return Promise.resolve();
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeSearch_componentLocale, await Locale.getComponentStrings(this.el), "f");
    }
    render() {
        return (h(Host, { class: "widget" }, h("ch-theme", { model: CSS_BUNDLES }), h("section", { class: "spacing-body search" }, h("header", { class: "search__header" }, h("ch-edit", { id: "search-input", class: "input", accessibleName: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").accessibleName, placeholder: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").placeholders.search, debounce: config.inputDebounce, onInput: __classPrivateFieldGet(this, _GxIdeSearch_searchInputHandler, "f"), onKeyDown: __classPrivateFieldGet(this, _GxIdeSearch_searchInputKeyDownHandler, "f"), type: "search", startImgSrc: SEARCH_ICON, disabled: !this.isKbOpen, value: __classPrivateFieldGet(this, _GxIdeSearch_searchInputValue, "f") })), h("main", { class: "search__results" }, __classPrivateFieldGet(this, _GxIdeSearch_renderSearchResults, "f").call(this)))));
    }
    static get is() { return "gx-ide-search"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["search.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["search.css"]
        };
    }
    static get assetsDirs() { return ["gx-ide-assets/search"]; }
    static get properties() {
        return {
            "isKbOpen": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Indicates whether a Knowledge Base is currently open"
                },
                "attribute": "is-kb-open",
                "reflect": false,
                "defaultValue": "false"
            },
            "searchCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "(\n    searchParams: SearchParamsData\n  ) => Promise<SearchResultData[]>",
                    "resolved": "(searchParams: SearchParamsData) => Promise<SearchResultData[]>",
                    "references": {
                        "SearchParamsData": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/search/search.tsx",
                            "id": "src/components/search/search.tsx::SearchParamsData"
                        },
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "SearchResultData": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/search/search.tsx",
                            "id": "src/components/search/search.tsx::SearchResultData"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Callback to fetch search results"
                }
            },
            "itemDoubleClickCallback": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "ItemDoubleClickCallback",
                    "resolved": "(itemId: string) => Promise<void>",
                    "references": {
                        "ItemDoubleClickCallback": {
                            "location": "local",
                            "path": "/home/runner/work/genexus-ide-ui/genexus-ide-ui/src/components/search/search.tsx",
                            "id": "src/components/search/search.tsx::ItemDoubleClickCallback"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Callback invoked when a search result item is double-clicked"
                }
            }
        };
    }
    static get states() {
        return {
            "searchResultModel": {},
            "hasSearched": {},
            "isSearching": {}
        };
    }
    static get methods() {
        return {
            "showSettings": {
                "complexType": {
                    "signature": "(_show: boolean) => Promise<void>",
                    "parameters": [{
                            "name": "_show",
                            "type": "boolean",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Method to show or hide the settings panel, can be called from the outside to control the visibility of the settings",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "isKbOpen",
                "methodName": "valueChangeHandler"
            }];
    }
}
_GxIdeSearch_componentLocale = new WeakMap(), _GxIdeSearch_searchInputValue = new WeakMap(), _GxIdeSearch_searchInputHandler = new WeakMap(), _GxIdeSearch_searchInputKeyDownHandler = new WeakMap(), _GxIdeSearch_mapSearchResultToTreeViewModel = new WeakMap(), _GxIdeSearch_treeItemDoubleClickHandler = new WeakMap(), _GxIdeSearch_renderSearchResults = new WeakMap();
//# sourceMappingURL=search.js.map
