import { useCallback } from 'react';
export function useHandleColumnChange(step, onChange) {
    return useCallback((previous, oldName, newName)=>{
        const columns = {
            ...step.args.columns
        };
        // this is the previous column mapping - remove it in case we
        // selected a different one to rename
        delete columns[previous];
        columns[oldName] = newName;
        onChange?.({
            ...step,
            args: {
                ...step.args,
                columns
            }
        });
    }, [
        step,
        onChange
    ]);
}
// find the next column from the table to suggest
function next(step, table) {
    return table?.columnNames().find((name)=>{
        if (!step.args.columns) {
            return true;
        }
        return !step.args.columns[name];
    });
}
export function useHandleAddButtonClick(step, table, onChange) {
    return useCallback(()=>{
        const nextName = next(step, table);
        if (nextName) {
            onChange?.({
                ...step,
                args: {
                    ...step.args,
                    columns: {
                        ...step.args.columns,
                        [nextName]: nextName
                    }
                }
            });
        }
    }, [
        step,
        table,
        onChange
    ]);
}
export function useDisabled(step, table) {
    if (!table) {
        return true;
    }
    return table.columnNames().length === Object.keys(step.args.columns || {}).length;
}
