import { jsx as _jsx } from "react/jsx-runtime";
import { SortDirection } from '@essex/arquero';
import set from 'lodash-es/set.js';
import { useCallback, useMemo } from 'react';
import { SortInstruction } from '../controls/index.js';
export function useSorts(step, columnOptions, onChange) {
    return useMemo(()=>{
        return (step.args.orders || []).map((order1, index)=>{
            const handleSortChange = (order)=>{
                const update = {
                    ...step
                };
                set(update, `args.orders[${index}]`, order);
                onChange?.(update);
            };
            const handleDeleteClick = ()=>{
                const update = {
                    ...step
                };
                update.args.orders.splice(index, 1);
                onChange?.(update);
            };
            return /*#__PURE__*/ _jsx(SortInstruction, {
                columnOptions: columnOptions,
                order: order1,
                onChange: handleSortChange,
                onDelete: handleDeleteClick
            }, `orderby-${order1.column}-${index}`);
        });
    }, [
        step,
        columnOptions,
        onChange
    ]);
}
export function useAddButtonClickedHandler(dataTable, step, onChange) {
    return useCallback(()=>{
        onChange?.({
            ...step,
            args: {
                ...step.args,
                orders: [
                    ...step.args.orders || [],
                    newSort(dataTable)
                ]
            }
        });
    }, [
        step,
        dataTable,
        onChange
    ]);
}
function newSort(table) {
    const column = table?.columnNames()[0];
    const direction = SortDirection.Ascending;
    return {
        column,
        direction
    };
}
