import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2024-2025 GoodData Corporation
import { useCallback, useMemo } from "react";
import { useIntl } from "react-intl";
import { AsyncPaginate } from "react-select-async-paginate";
import { useBackendStrict } from "@gooddata/sdk-ui";
import { EmptyRenderer, GroupHeadingRenderer, LoadingMessageRenderer, NoOptionsMessageRenderer, OptionRenderer, SingleValueInputRenderer, SingleValueRenderer, WrappedMenuListRenderer, } from "./AsyncSelectComponents.js";
import { messages } from "../locales.js";
import { isSelectErrorOption, isWorkspaceItem, } from "../types.js";
import { loadPaginatedWorkspaceOptionsPromise } from "./backend/loadWorkspaceOptionsPromise.js";
const SEARCH_INTERVAL = 400;
export function AddSingleWorkspaceSelect({ addedWorkspace, grantedWorkspaces, onSelectWorkspace, mode = "EDIT", }) {
    const backend = useBackendStrict();
    const intl = useIntl();
    const isEditMode = mode === "EDIT";
    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(() => {
        return isEditMode && loadPaginatedWorkspaceOptionsPromise(backend, intl);
    }, [backend, intl, isEditMode]);
    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(() => (addedWorkspace ? [addedWorkspace, ...(grantedWorkspaces ?? [])] : grantedWorkspaces), [addedWorkspace, 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(AsyncPaginate, { autoFocus: true, defaultMenuIsOpen: isEditMode, classNamePrefix: "gd-share-dialog", components: {
                IndicatorSeparator: EmptyRenderer,
                Input: SingleValueInputRenderer,
                Option: OptionRenderer,
                GroupHeading: GroupHeadingRenderer,
                LoadingMessage: LoadingMessageRenderer,
                LoadingIndicator: EmptyRenderer,
                MenuList: WrappedMenuListRenderer,
                NoOptionsMessage: NoOptionsMessageRenderer,
                SingleValue: SingleValueRenderer,
            }, styles: {
                dropdownIndicator: (base) => ({
                    ...base,
                    padding: "0",
                    marginRight: "-6px",
                    svg: { height: "14px" },
                }),
            }, loadOptions: loadOptions, defaultOptions: true, placeholder: intl.formatMessage(messages.searchWorkspacePlaceholder), onKeyDown: onKeyDownCallback, noOptionsMessage: noOptionsMessage, onChange: onSelect, filterOption: filterOption, isMulti: false, isDisabled: !isEditMode, value: addedWorkspace
                ? {
                    label: addedWorkspace.title,
                    value: { id: addedWorkspace.id, title: addedWorkspace.title },
                }
                : null, additional: {
                page: 0,
            }, debounceTimeout: SEARCH_INTERVAL }) }));
}
//# sourceMappingURL=AddSingleWorkspaceSelect.js.map