import { MantineReactTable, MRT_RowData, MRT_TableInstance, MRT_TableOptions, useMantineReactTable, Xor } from "mantine-react-table";
import { useNejReactTable } from "./useNejReactTable";

type TableInstanceProp<TData extends MRT_RowData> = {
    table: MRT_TableInstance<TData>;
};

type Props<TData extends MRT_RowData> = Xor<
    TableInstanceProp<TData>,
    MRT_TableOptions<TData>
>;

const isTableInstanceProp = <TData extends MRT_RowData>(
    props: Props<TData>,
): props is TableInstanceProp<TData> =>
    (props as TableInstanceProp<TData>).table !== undefined;

export const NejReactTable = <TData extends MRT_RowData>(
    props: Props<TData>,
) => {
    let table: MRT_TableInstance<TData>;

    if (isTableInstanceProp(props)) {
        table = props.table;
    } else {
        table = useNejReactTable(props);
    }

    return <MantineReactTable {...props} />;
};
