import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { BooleanOperator } from '@data-wrangling-components/core';
import { ActionButton } from '@fluentui/react';
import { memo, useCallback } from 'react';
import { EnumDropdown } from '../controls/index.js';
import { useDropdownChangeHandler, useStepDataTable } from '../hooks/index.js';
import { LeftAlignedRow } from '../styles.js';
import { Container, Vertical } from './Filter.styles.js';
import { FilterFunction } from './shared/index.js';
/**
 * Provides inputs for a Filter step.
 */ export const Filter = /*#__PURE__*/ memo(function Filter({ step , graph , input , table , onChange  }) {
    const dataTable = useStepDataTable(step, graph, input, table);
    const handleButtonClick = useCallback(()=>{
        onChange?.({
            ...step,
            args: {
                ...step.args,
                criteria: [
                    ...step.args.criteria || [],
                    {}
                ]
            }
        });
    }, [
        step,
        onChange
    ]);
    const handleFilterChange = useCallback((criterion, index)=>{
        const criteria = [
            ...step.args.criteria
        ];
        if (criterion === undefined) {
            criteria.splice(index, 1);
        } else {
            criteria[index] = criterion;
        }
        onChange?.({
            ...step,
            args: {
                ...step.args,
                criteria
            }
        });
    }, [
        step,
        onChange
    ]);
    const handleLogicalChange = useDropdownChangeHandler(step, (s, val)=>s.args.logical = val, onChange);
    const filters = useFilters(dataTable, step.args.column, step.args.criteria, handleFilterChange);
    return /*#__PURE__*/ _jsxs(Container, {
        children: [
            filters,
            /*#__PURE__*/ _jsx(ActionButton, {
                onClick: handleButtonClick,
                iconProps: {
                    iconName: 'Add'
                },
                disabled: !dataTable,
                children: "Add criteria"
            }),
            step.args.criteria.length > 1 ? /*#__PURE__*/ _jsx(LeftAlignedRow, {
                children: /*#__PURE__*/ _jsx(EnumDropdown, {
                    label: 'Logical combination',
                    placeholder: 'Choose boolean',
                    enumeration: BooleanOperator,
                    labels: {
                        or: 'OR',
                        and: 'AND',
                        nor: 'NOR',
                        nand: 'NAND',
                        xor: 'XOR'
                    },
                    selectedKey: step.args.logical,
                    onChange: handleLogicalChange
                })
            }) : null
        ]
    });
});
function useFilters(table, column, criteria, onChange) {
    if (!table) {
        return null;
    }
    return criteria.map((criterion, index)=>{
        const handleChange = (f)=>onChange(f, index);
        return /*#__PURE__*/ _jsx(Vertical, {
            index: index,
            children: /*#__PURE__*/ _jsx(FilterFunction, {
                table: table,
                column: column,
                criterion: criterion,
                onChange: handleChange,
                suppressLabels: index > 0
            })
        }, `filter-function-${index}`);
    });
}
