import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2025 GoodData Corporation
import { useCallback, useEffect, useMemo, useRef } from "react";
import debounce from "debounce-promise";
import { useIntl } from "react-intl";
import AsyncSelect from "react-select/async";
import { useBackendStrict } from "@gooddata/sdk-ui";
import { EmptyRenderer, GroupHeadingRenderer, InputRendered, LoadingMessageRenderer, MenuListRendered, NoOptionsMessageRenderer, OptionRenderer, } from "./AsyncSelectComponents.js";
import { messages } from "../locales.js";
import { isSelectErrorOption, isWorkspaceItem, } from "../types.js";
import { loadWorkspaceOptionsPromise } from "./backend/loadWorkspaceOptionsPromise.js";
const SEARCH_INTERVAL = 400;
export function AddWorkspaceSelect({ addedWorkspaces, grantedWorkspaces, onSelectWorkspace, }) {
    const backend = useBackendStrict();
    const intl = useIntl();
    const selectRef = useRef(null);
    useEffect(() => {
        selectRef.current?.focus();
    }, []);
    const onSelect = useCallback((value) => {
        if (!isSelectErrorOption(value)) {
            const workspace = value.value;
            if (isWorkspaceItem(workspace)) {
                onSelectWorkspace(workspace);
            }
        }
    }, [onSelectWorkspace]);
    const noOptionsMessage = useMemo(() => () => intl.formatMessage(messages.searchWorkspaceNoMatch), [intl]);
    const loadOptions = useMemo(() => debounce(loadWorkspaceOptionsPromise(backend, intl), SEARCH_INTERVAL, { leading: true }), [backend, intl]);
    const onKeyDownCallback = useCallback((e) => {
        const target = e.target;
        // react-select has default behavior on space and tab key  - open menu / select first option etc.
        // We need disable this behavior for space key by set e.preventDefault when input is empty and for tab key completely
        //space
        if (e.keyCode === 32 && !target.value) {
            e.preventDefault();
        }
        // tab
        if (e.keyCode === 9) {
            e.preventDefault();
        }
    }, []);
    const usedWorkspace = useMemo(() => (grantedWorkspaces ? [...addedWorkspaces, ...grantedWorkspaces] : []), [addedWorkspaces, grantedWorkspaces]);
    const filterOption = (option) => {
        const workspace = option.value;
        return !usedWorkspace.some((usedWorkspace) => {
            return usedWorkspace.id === workspace.id;
        });
    };
    return (_jsx("div", { className: "gd-share-dialog-content-select s-user-management-workspace-select", children: _jsx(AsyncSelect, { ref: selectRef, defaultMenuIsOpen: true, classNamePrefix: "gd-share-dialog", components: {
                DropdownIndicator: EmptyRenderer,
                IndicatorSeparator: EmptyRenderer,
                Input: InputRendered,
                Option: OptionRenderer,
                GroupHeading: GroupHeadingRenderer,
                LoadingMessage: LoadingMessageRenderer,
                LoadingIndicator: EmptyRenderer,
                MenuList: MenuListRendered,
                NoOptionsMessage: NoOptionsMessageRenderer,
            }, loadOptions: loadOptions, defaultOptions: true, placeholder: intl.formatMessage(messages.searchWorkspacePlaceholder), onKeyDown: onKeyDownCallback, noOptionsMessage: noOptionsMessage, onChange: onSelect, value: null, filterOption: filterOption }) }));
}
//# sourceMappingURL=AddWorkspaceSelect.js.map