import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useCallback } from "react";
import { InsightPicker, } from "@gooddata/sdk-ui-ext";
import { Dialog } from "@gooddata/sdk-ui-kit";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectBackendCapabilities } from "../../../model/store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectEnableVisualizationFilteringByTags, selectObjectAvailabilityConfig, selectSettings, } from "../../../model/store/config/configSelectors.js";
import { selectCurrentUser } from "../../../model/store/user/userSelectors.js";
import { getAuthor } from "../../../model/utils/author.js";
import { InsightPickerListItem } from "./InsightPickerListItem.js";
import { useInsertInsightToLayout } from "./useInsertInsightToLayout.js";
function useAuthor() {
    const capabilities = useDashboardSelector(selectBackendCapabilities);
    const currentUser = useDashboardSelector(selectCurrentUser);
    return getAuthor(capabilities, currentUser);
}
/**
 * @internal
 */
export function InsightPickerPanel({ onClose, searchQuery, onSearchChange, sortBy, sortDirection, onSortChange, authorFilter, onAuthorFilterChange, tagFilter, onTagFilterChange, }) {
    const settings = useDashboardSelector(selectSettings);
    const author = useAuthor();
    const objectAvailability = useDashboardSelector(selectObjectAvailabilityConfig);
    const isFilteringByTagsEnabled = useDashboardSelector(selectEnableVisualizationFilteringByTags);
    const insertInsight = useInsertInsightToLayout();
    const includeTags = isFilteringByTagsEnabled ? objectAvailability?.includeObjectsWithTags : undefined;
    const excludeTags = isFilteringByTagsEnabled ? objectAvailability?.excludeObjectsWithTags : undefined;
    const handleSelect = useCallback((_ref, _item) => {
        // no-op — selection handled via drag-and-drop or keyboard activate
    }, []);
    const handleInsightActivate = useCallback((insight) => {
        onClose();
        insertInsight(insight);
    }, [onClose, insertInsight]);
    const renderItem = useCallback((renderProps) => (_jsx(InsightPickerListItem, { item: renderProps.item, type: renderProps.type, sourceInsight: renderProps.sourceInsight, metadataTimeZone: settings?.metadataTimeZone, onActivate: handleInsightActivate, onDropped: onClose })), [settings?.metadataTimeZone, handleInsightActivate, onClose]);
    return (_jsx(Dialog, { className: "gd-insight-picker-panel s-insight-picker-panel", containerClassName: "gd-insight-picker-panel-overlay", isModal: false, shouldCloseOnClick: () => true, closeOnEscape: true, onClose: onClose, alignPoints: [{ align: "bc bc" }], children: _jsx(InsightPicker, { author: author, includeTags: includeTags, excludeTags: excludeTags, metadataTimeZone: settings?.metadataTimeZone, enableDescriptions: settings?.enableDescriptions, enableSemanticSearch: false, maxHeight: 350, width: 718, searchQuery: searchQuery, onSearchChange: onSearchChange, sortBy: sortBy, sortDirection: sortDirection, onSortChange: onSortChange, authorFilter: authorFilter, onAuthorFilterChange: onAuthorFilterChange, tagFilter: tagFilter, onTagFilterChange: onTagFilterChange, onSelect: handleSelect, onItemActivate: (_item, sourceInsight) => {
                if (sourceInsight) {
                    handleInsightActivate(sourceInsight);
                }
            }, renderItem: renderItem }) }));
}
