import { createGraphManager, readStep } from '@data-wrangling-components/core';
import isArray from 'lodash-es/isArray.js';
import { useCallback, useEffect, useMemo } from 'react';
export function useGraphManager(workflow, inputs) {
    const manager = useMemo(()=>createGraphManager(undefined, workflow), // eslint-disable-next-line react-hooks/exhaustive-deps
    []);
    // this effect should fire when a new workflow json is uploaded
    useEffect(function resetWorkflowWhenWorkflowChanges() {
        manager.reset(workflow);
    }, [
        manager,
        workflow
    ]);
    useEffect(function syncInputs() {
        inputs?.forEach((i)=>manager.addInput(i));
    }, [
        manager,
        inputs
    ]);
    return manager;
}
/**
 * Returns a hook to generate a new table name based on the given input e.g.
 * "join" could result in "join 1" or "join 2" depending on how many collisions
 *  occur.
 * @param graph - the graph manager
 * @returns a safe output name to use
 */ export function useCreateTableName(graph) {
    return useCallback((name)=>graph.suggestOutputName(name), [
        graph
    ]);
}
export function useFormattedColumnArg() {
    return useCallback((stepArgs, newName = 'New column')=>{
        const args = stepArgs;
        Object.keys(args).forEach((key)=>{
            if (key === 'to' && !isArray(args[key])) {
                args[key] = newName;
            }
        });
        return args;
    }, []);
}
export function useResetArgs() {
    return useCallback((step)=>{
        const template = readStep(step).args;
        const args = step.args;
        // rewrite through the proxy for each arg entry
        Object.keys(args).forEach((key)=>{
            args[key] = template[key];
        });
        return step;
    }, []);
}
