/*!
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project.
 */ import { DataType } from '@datashaper/schema';
import { columnType, columnTypes } from '@datashaper/tables';
import { toggleListItem } from '@datashaper/utilities';
import { isNumericInputStep, NodeInput } from '@datashaper/workflow';
import { useCallback, useEffect, useMemo } from 'react';
import { useResetArgs } from '../hooks/common.js';
import { useDropdownChangeAllHandler, useDropdownChangeHandler, useTextFieldChangeHandler } from '../hooks/fluent.js';
import { selectStepComponent } from '../selectStepComponent.js';
export function getSimpleDropdownOptions(list) {
    return list.map((name)=>({
            key: name,
            text: name.toString()
        }));
}
export function useStepChangeHandler(index, onChange) {
    return useCallback((step)=>onChange(step, index), [
        index,
        onChange
    ]);
}
export function useStepArgsComponent(step) {
    return useMemo(()=>step ? selectStepComponent(step) : null, [
        step
    ]);
}
export function useColumnFilter(step, table) {
    const typeMap = useMemo(()=>{
        if (table) {
            return columnTypes(table);
        }
        return {};
    }, [
        table
    ]);
    return useCallback((name)=>{
        return isNumericInputStep(step) ? typeMap[name] === DataType.Number : true;
    }, [
        typeMap,
        step
    ]);
}
export function useInputTableChanged(step, onChange) {
    const resetArgs = useResetArgs();
    return useDropdownChangeHandler(step, (s, val)=>{
        if (val != null) {
            // wire up the Step's input field
            s.input[NodeInput.Source] = {
                node: val
            };
            resetArgs(s);
        } else {
            // no value: delete the input
            delete s.input[NodeInput.Source];
        }
    }, onChange);
}
export function useInputColumnChanged(step, onChange, dataTable) {
    return useDropdownChangeHandler(step, (s, val)=>{
        s.args.column = val;
        s.args.dataType = dataTable != null ? columnType(dataTable, val) : DataType.Unknown;
    }, onChange);
}
export function useInputColumnListChanged(step, onChange) {
    return useDropdownChangeHandler(step, (s, val)=>s.args.columns = toggleListItem(s.args.columns ?? [], val), onChange);
}
export function useInputColumnListAllChanged(step, onChangeAll) {
    return useDropdownChangeAllHandler(step, (s, val)=>s.args.columns = val, onChangeAll);
}
export function useOutputColumnChanged(step, onChange) {
    return useTextFieldChangeHandler(step, (s, val)=>{
        s.args.to = val;
    }, onChange);
}
export function useOutputTableChanged(step, onChangeOutput, onChange) {
    return useTextFieldChangeHandler(step, (_s, val)=>onChangeOutput(val), onChange);
}
export function useDefaultOutputNameInitially(step, output, onChangeOutput) {
    useEffect(()=>{
        onChangeOutput(output ?? step.id);
    }, // eslint-disable-next-line react-hooks/exhaustive-deps
    []);
}
