import * as React from 'react';
import { Box, Flex } from 'rebass';
import { CheckBox } from '../../../../components/CheckBox';
import DropdownButton from '../../../../components/DropdownButton';
import HelpBlock from '../../../../components/HelpBlock';
import { Icon } from '../../../../components/icons';
import { DataSource, InfiniteTable, } from '../../../../components/InfiniteTable';
import { Tabs } from '../../../../components/Tabs';
import { Tag } from '../../../../components/Tag';
import { useAdaptable } from '../../../AdaptableContext';
import { AdaptablePopover } from '../../../AdaptablePopover';
import UIHelper from '../../../UIHelper';
const tableDOMProps = {
    style: {
        height: '100%',
        minWidth: '10rem',
        minHeight: 300,
        flex: 1,
    },
};
export const ColumnsSection = (props) => {
    const { api } = useAdaptable();
    const primaryKey = api.optionsApi.getPrimaryKey();
    const allColumns = React.useMemo(() => {
        return api.columnApi
            .getUIAvailableColumns()
            .filter((col) => col.field)
            .map((column) => ({
            label: column.friendlyName,
            value: column,
        }));
    }, []);
    const handleColumnMapChange = (newColMap) => {
        props.onColumnsChange(props.columnsMap?.map((cm) => {
            if (cm.field === newColMap.field) {
                return { ...cm, ...newColMap };
            }
            return cm;
        }));
    };
    const columns = React.useMemo(() => {
        return {
            include: {
                field: 'include',
                header: 'Included',
                maxWidth: 110,
                align: 'center',
                render: (params) => {
                    return (React.createElement(CheckBox, { disabled: params.data?.abColumn?.field === primaryKey, onChange: (checked) => {
                            handleColumnMapChange({
                                ...params.data,
                                include: checked,
                            });
                        }, checked: Boolean(params.data.include) }));
                },
            },
            field: {
                field: 'field',
                header: 'Data Field',
            },
            abColumn: {
                header: 'AdapTable Column',
                render: (params) => {
                    // filter out used columns
                    const availableColumns = allColumns
                        .filter((c) => !props.columnsMap?.find((cm) => cm.abColumn === c.value))
                        .map((col) => {
                        return {
                            ...col,
                            onClick: () => {
                                handleColumnMapChange({
                                    ...params.data,
                                    abColumn: col.value,
                                    include: true,
                                });
                            },
                        };
                    });
                    return (React.createElement(DropdownButton, { showClearButton: true, onClear: () => {
                            handleColumnMapChange({
                                ...params.data,
                                abColumn: null,
                                include: false,
                            });
                        }, width: "100%", columns: ['label'], items: availableColumns }, params.data?.abColumn?.friendlyName ??
                        params.data?.abColumn?.columnId ??
                        'Select Column'));
                },
            },
            valid: {
                header: 'Valid',
                maxWidth: 100,
                minWidth: 90,
                align: 'center',
                render: (row) => {
                    if (!row.data.abColumn) {
                        return (React.createElement(AdaptablePopover, { popoverMinWidth: 200, showEvent: "mouseenter", hideEvent: "mouseleave", headerText: 'Validation Error', bodyText: ['Select Column'], MessageType: 'Error' }));
                    }
                    const successColor = UIHelper.getColorByMessageType('Success');
                    return React.createElement(Icon, { name: "check", style: { color: successColor, fill: 'currentColor' } });
                },
            },
        };
    }, [props.columnsMap]);
    if (!props.columnsMap) {
        return (React.createElement(Box, { p: 2 },
            React.createElement(HelpBlock, { color: 'var(--ab-color-error)' },
                "No Data has been Imported. Go to ",
                React.createElement(Tag, null, "Upload"),
                " step and provide Data")));
    }
    return (React.createElement(Flex, { flexDirection: "column", p: 2, height: "100%" },
        React.createElement(Tabs, { mb: 2, height: "100%" },
            React.createElement(Tabs.Tab, null, "Columns"),
            React.createElement(Tabs.Content, null,
                React.createElement(DataSource, { data: props.columnsMap, primaryKey: 'field' },
                    React.createElement(InfiniteTable, { columnPinning: {
                            valid: 'end',
                            type: 'end',
                        }, columnTypes: {
                            default: {
                                minWidth: 100,
                                defaultFlex: 1,
                            },
                        }, domProps: tableDOMProps, columns: columns }))))));
};
ColumnsSection.displayName = 'ColumnsSection';
