import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import { useIntl } from "react-intl";
import { commonDialogMessages, olpTransferOwnershipDialogMessages, uiGranteeAsyncPickerMessages, } from "../../locales.js";
import { bem } from "../@utils/bem.js";
import { UiAutocomplete } from "../UiAutocomplete/UiAutocomplete.js";
import { UiButton } from "../UiButton/UiButton.js";
import { UiCheckbox } from "../UiCheckbox/UiCheckbox.js";
import { UiGranteeRow } from "../UiGranteeRow/UiGranteeRow.js";
import { UiIconButton } from "../UiIconButton/UiIconButton.js";
import { UiDialogFooter } from "../UiModalDialog/UiDialogFooter.js";
import { UiDialogHeader } from "../UiModalDialog/UiDialogHeader.js";
const { b, e } = bem("gd-ui-kit-transfer-ownership-dialog");
/**
 * Transfer-ownership dialog card — header + single-user search picker + the
 * picked owner row (with an "Owner" tag) + an "Also remove my access" checkbox
 * and an informational note, then a footer with Cancel and Transfer. Renders
 * inline as a plain card; for modal behavior use `UiTransferOwnershipDialog`.
 *
 * The card owns the visual composition but no policy. The caller controls the
 * picked owner via `selectedOwner` / `onSelectedOwnerChange`, decides whether
 * that owner is already an owner, and performs the actual transfer on
 * `onTransfer`.
 *
 * @internal
 */
export function UiTransferOwnershipDialogCard({ objectTitle, loadOptions, selectedOwner, onSelectedOwnerChange, alsoRemoveMyAccess, onAlsoRemoveMyAccessChange, onBack, onClose, onCancel, onTransfer, isSaving = false, dataTestId, }) {
    const intl = useIntl();
    const dialogTitle = intl.formatMessage(olpTransferOwnershipDialogMessages.title, { title: objectTitle });
    const backButton = (_jsx(UiIconButton, { icon: "chevronLeft", variant: "tertiary", size: "small", onClick: onBack, accessibilityConfig: { ariaLabel: intl.formatMessage(olpTransferOwnershipDialogMessages.back) } }));
    const usersLabel = intl.formatMessage(uiGranteeAsyncPickerMessages.sectionUsers);
    // Only users can own an object, so the search shows a single Users section
    // (no Groups). The caller already returns users only; ignore any groups.
    const adaptedLoadOptions = useCallback(async (search) => {
        const { users } = await loadOptions(search);
        const sections = users.length > 0
            ? [
                {
                    id: "users",
                    label: usersLabel,
                    options: users.map((u) => ({
                        id: u.id,
                        label: u.name,
                        secondaryText: u.email,
                        grantee: u,
                    })),
                },
            ]
            : [];
        return { sections };
    }, [loadOptions, usersLabel]);
    const handleSelect = useCallback((option) => onSelectedOwnerChange(option.grantee), [onSelectedOwnerChange]);
    const autocompleteMessages = useMemo(() => ({
        searchPlaceholder: intl.formatMessage(olpTransferOwnershipDialogMessages.searchPlaceholder),
        stateError: intl.formatMessage(uiGranteeAsyncPickerMessages.stateError),
        stateNoMatch: intl.formatMessage(uiGranteeAsyncPickerMessages.stateNoMatch),
    }), [intl]);
    const userLabel = intl.formatMessage(olpTransferOwnershipDialogMessages.userLabel);
    return (_jsxs("div", { className: b(), "data-testid": dataTestId, children: [
            _jsx(UiDialogHeader, { title: dialogTitle, titleSize: "large", onClose: onClose, leading: backButton }), _jsxs("div", { className: e("field"), children: [
                    _jsx("div", { className: e("field-label"), children: userLabel }), _jsx(UiAutocomplete, { loadOptions: adaptedLoadOptions, selectedIds: selectedOwner ? [selectedOwner.id] : undefined, onSelect: handleSelect, messages: autocompleteMessages, accessibilityConfig: { ariaLabel: userLabel } })
                ] }), selectedOwner ? (_jsxs(_Fragment, { children: [
                    _jsx(UiGranteeRow, { kind: "user", name: selectedOwner.name, email: selectedOwner.email, isOwner: true }), _jsx(UiCheckbox, { checked: alsoRemoveMyAccess, onChange: (ev) => onAlsoRemoveMyAccessChange(ev.target.checked), label: intl.formatMessage(olpTransferOwnershipDialogMessages.alsoRemoveMyAccess) }), _jsx("div", { className: e("note"), children: intl.formatMessage(alsoRemoveMyAccess
                            ? olpTransferOwnershipDialogMessages.noteLoseAccess
                            : olpTransferOwnershipDialogMessages.noteKeepAccess) })
                ] })) : null, _jsxs(UiDialogFooter, { divider: true, children: [
                    _jsx(UiButton, { label: intl.formatMessage(commonDialogMessages.cancel), variant: "secondary", size: "medium", onClick: onCancel }), _jsx(UiButton, { label: intl.formatMessage(olpTransferOwnershipDialogMessages.transfer), variant: "primary", size: "medium", onClick: onTransfer, isDisabled: !selectedOwner || isSaving })
                ] })
        ] }));
}
