/*!
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project.
 */ import { DataType } from '@datashaper/schema';
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 useHandleKeyChange(step, onChange) {
    return useCallback((previousKey, newKey)=>{
        const mapList = {
            ...step.args.mapping
        };
        const mapping = {};
        for(const key in mapList){
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            key === previousKey.toString() ? mapping[newKey] = mapList[key] : mapping[key] = mapList[key];
        }
        onChange?.({
            ...step,
            args: {
                ...step.args,
                mapping
            }
        });
    }, [
        step,
        onChange
    ]);
}
export function useHandleValueChange(step, dataType, onChange) {
    return useCallback((key, newValue)=>{
        const mapList = {
            ...step.args.mapping
        };
        const mapping = {};
        for(const keyElement in mapList){
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            keyElement === key.toString() ? mapping[keyElement] = dataType === DataType.Date ? new Date(newValue) : newValue : mapping[keyElement] = mapList[keyElement];
        }
        onChange?.({
            ...step,
            args: {
                ...step.args,
                mapping
            }
        });
    }, [
        step,
        onChange,
        dataType
    ]);
}
export function useRecodeDelete(step, onChange) {
    return useCallback((value)=>{
        const args = {
            ...step.args
        };
        delete args.mapping[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.mapping) {
            return true;
        }
        return step.args.mapping[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,
                    mapping: {
                        ...step.args.mapping,
                        [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.mapping || {}).length;
}
