// (C) 2026 GoodData Corporation
import { useCallback } from "react";
/**
 * Selection callbacks for the add-grantee dialog. Caller owns the list and
 * receives the next list via `onSelectedGranteesChange`.
 *
 * @internal
 */
export function useGranteeSelection({ selectedGrantees, onSelectedGranteesChange, initialPermissionLevel = "VIEW", }) {
    const select = useCallback((option) => {
        // The picker already filters out picked ids, but a quick double-click
        // can land a second select call before the parent re-renders — guard
        // against the duplicate row here.
        if (selectedGrantees.some((g) => g.id === option.id)) {
            return;
        }
        onSelectedGranteesChange([
            ...selectedGrantees,
            { ...option, permissionLevel: initialPermissionLevel },
        ]);
    }, [selectedGrantees, onSelectedGranteesChange, initialPermissionLevel]);
    const changePermission = useCallback((grantee, next) => {
        onSelectedGranteesChange(selectedGrantees.map((g) => (g.id === grantee.id ? { ...g, permissionLevel: next } : g)));
    }, [selectedGrantees, onSelectedGranteesChange]);
    const remove = useCallback((grantee) => {
        onSelectedGranteesChange(selectedGrantees.filter((g) => g.id !== grantee.id));
    }, [selectedGrantees, onSelectedGranteesChange]);
    return { select, changePermission, remove };
}
