'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 searchCss = ":host{display:grid;block-size:100%}.search{display:grid;grid-template-rows:max-content 1fr;gap:var(--spacing-gap-xl)}.search__header{display:grid;grid-template-columns:1fr}.search__results{position:relative}ch-tree-view-render::part(root-item item__header){padding-inline-start:0;font-weight:var(--font-style-semi-bold);font-size:var(--font-size-body-s);line-height:var(--line-height-relaxed);font-family:var(--font-family-body)}ch-tree-view-render::part(search-item item__header){padding-inline-start:0;font-weight:var(--font-style-regular);font-size:var(--font-size-body-s);line-height:var(--line-height-relaxed);font-family:var(--font-family-body);color:var(--color-text-neutral-neutral) !important}.input--loading::part(start-img){animation:spin 1s linear infinite}@keyframes spin{100%{transform:rotate(360deg)}}";

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 __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 _GxIdeSearch_componentLocale, _GxIdeSearch_searchInputValue, _GxIdeSearch_searchInputHandler, _GxIdeSearch_searchInputKeyDownHandler, _GxIdeSearch_mapSearchResultToTreeViewModel, _GxIdeSearch_treeItemDoubleClickHandler, _GxIdeSearch_renderSearchResults;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "components/edit",
    "components/tree-view",
    "utils/typography",
    "utils/spacing",
    "chameleon/scrollbar"
];
const SEARCH_ICON = assetsManager.getIconPath({
    category: "system",
    name: "search",
    colorType: "on-surface"
});
const INFORMATION_ICON = assetsManager.getIconPath({
    category: "system",
    name: "information",
    colorType: "on-interactive"
});
const GxIdeSearch = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        _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 (index.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 (index.h("gx-ide-loader", { loaderTitle: __classPrivateFieldGet(this, _GxIdeSearch_componentLocale, "f").loader.title, show: true }));
            }
            if (this.searchResultModel.length > 0) {
                return (index.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 (index.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.Locale.getComponentStrings(this.el), "f");
    }
    render() {
        return (index.h(index.Host, { class: "widget" }, index.h("ch-theme", { model: CSS_BUNDLES }), index.h("section", { class: "spacing-body search" }, index.h("header", { class: "search__header" }, index.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.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") })), index.h("main", { class: "search__results" }, __classPrivateFieldGet(this, _GxIdeSearch_renderSearchResults, "f").call(this)))));
    }
    static get assetsDirs() { return ["gx-ide-assets/search"]; }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "isKbOpen": ["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();
GxIdeSearch.style = searchCss;

exports.gx_ide_search = GxIdeSearch;

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