/*!
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project.
 */ /* eslint-disable @typescript-eslint/unbound-method */ import { introspect } from '@datashaper/tables';
import { cloneStep, Workflow } from '@datashaper/workflow';
import { useCallback, useEffect } from 'react';
import { useCreateTableName } from '../hooks/common.js';
import { useHandleStepOutputChanged, useHandleStepSave, useModalState } from '../hooks/index.js';
export function useOnDuplicateStep(workflow, table, onSave) {
    const createTableName = useCreateTableName(workflow);
    return useCallback((step)=>{
        const outputTable = table ?? workflow?.latestOutputForNode(step.id)?.table;
        const clonedStep = cloneStep(step, outputTable?.columnNames());
        clonedStep.id = '';
        onSave?.(clonedStep, createTableName(workflow.outputNameForNode(step.id) ?? step.id));
    }, [
        onSave,
        workflow,
        table,
        createTableName
    ]);
}
/**
 * A hook to get the onEditStep callback
 *
 * @param setStep - The step setter
 * @param setStepIndex - The step-index setter
 * @param showTableModal - A callback to show the table modal
 * @returns
 */ export function useOnEditStep(setStep, setStepIndex, showTableModal) {
    return useCallback((step, index)=>{
        setStep(step);
        setStepIndex(index);
        showTableModal();
    }, [
        setStep,
        showTableModal,
        setStepIndex
    ]);
}
export function useOnCreateStep(save, selectOutput, dismissModal) {
    return useCallback((step, output, index)=>{
        save(step, output, index);
        dismissModal?.();
        if (output) selectOutput?.(output);
    }, [
        save,
        dismissModal,
        selectOutput
    ]);
}
/**
 *
 * @param workflow - The workflow
 * @returns A callback to use when saving a step, either new or existing
 */ export function useOnSaveStep(workflow) {
    const updateStep = useHandleStepSave(workflow);
    const updateStepOutput = useHandleStepOutputChanged(workflow);
    return useCallback((step, output, index)=>{
        const stepResult = updateStep(step, index);
        updateStepOutput(stepResult, output);
    }, [
        updateStepOutput,
        updateStep
    ]);
}
/**
 *
 * @param workflow - The workflow
 * @returns A callback to use when saving a step, either new or existing
 */ export function useOnUpdateStep(workflow) {
    const updateStep = useHandleStepSave(workflow);
    const updateStepOutput = useHandleStepOutputChanged(workflow);
    return useCallback((step, output, index)=>{
        const stepResult = updateStep(step, index);
        updateStepOutput(stepResult, output);
    }, [
        updateStepOutput,
        updateStep
    ]);
}
export function useWorkflowOutputListener(workflow, setOutput) {
    useEffect(()=>setOutput && workflow.onChange(()=>{
            const outputs = workflow.toArray().filter((t)=>!!t).map((table)=>{
                if (!table.metadata) {
                    table.metadata = introspect(table.table, true);
                }
                return table;
            });
            setOutput(outputs);
        }, true), [
        workflow,
        setOutput
    ]);
}
export function useOnDeleteStep(workflow) {
    return useCallback((index)=>{
        workflow.removeStep(index);
    }, [
        workflow
    ]);
}
export function useWorkflowListener(workflow, setWorkflow) {
    useEffect(()=>setWorkflow && workflow.onChange(()=>setWorkflow(new Workflow(workflow.toSchema()))), [
        workflow,
        setWorkflow
    ]);
}
/**
 * A hook to manage state for showing the the step transformation modal
 *
 * @param setStep - A mutator for the selected step
 * @param setStepIndex - A mutator for the selected step indexw
 * @returns An object containing the isOpen state, and show/hide callbacks
 */ export function useTransformModalState(setStep, setStepIndex) {
    const onDismiss = useCallback(()=>{
        setStep(undefined);
        setStepIndex(undefined);
    }, [
        setStep,
        setStepIndex
    ]);
    return useModalState(undefined, onDismiss);
}
