import { op } from 'arquero';
import { useCallback, useMemo } from 'react';
export function useColumnValues(step, table) {
    return useMemo(()=>{
        const { column  } = step.args;
        if (!table || !column) {
            return [];
        }
        const args = {
            [column]: op.array_agg_distinct(column)
        };
        const rollup = table.orderby(column).rollup(args);
        return rollup.get(column, 0);
    }, [
        table,
        step
    ]);
}
export function useHandleRecodeChange(step, onChange) {
    return useCallback((previous, oldValue, newValue)=>{
        const map = {
            ...step.args.map
        };
        delete map[previous];
        map[oldValue] = newValue;
        onChange?.({
            ...step,
            args: {
                ...step.args,
                map
            }
        });
    }, [
        step,
        onChange
    ]);
}
export function useRecodeDelete(step, onChange) {
    return useCallback((value)=>{
        const args = {
            ...step.args
        };
        delete args.map[value];
        onChange?.({
            ...step,
            args: {
                ...step.args,
                ...args
            }
        });
    }, [
        step,
        onChange
    ]);
}
// find the next value from the table to suggest
function next(step, values) {
    return values.find((value)=>{
        if (!step.args.map) {
            return true;
        }
        return step.args.map[value] === undefined;
    });
}
export function useHandleAddButtonClick(step, values, onChange) {
    return useCallback(()=>{
        const nextValue = next(step, values);
        if (nextValue !== undefined) {
            // could be a 0 or false...
            onChange?.({
                ...step,
                args: {
                    ...step.args,
                    map: {
                        ...step.args.map,
                        [nextValue]: nextValue
                    }
                }
            });
        }
    }, [
        step,
        values,
        onChange
    ]);
}
export function useDisabled(step, values) {
    if (values.length === 0 || !step.args.column) {
        return true;
    }
    return values.length === Object.keys(step.args.map || {}).length;
}
