import {List, ListProps, ListState} from './withStore/List';
import {TreeStore} from '../../data/TreeStore';

type TreeListProps<R> = ListProps<R> & {
    store: TreeStore<R>;
};

type TreeListState<R> = ListState<R> & {
    nodes: R[];
};

// @ts-ignore
export abstract class TreeList<
    P extends TreeListProps<R>,
    S extends TreeListState<R>,
    R
> extends List<P, S, R> {
    constructor(props, context) {
        super(props, context);

        this.state = {
            nodes: this.props.store.getData()
        } as any;
    }

    onStoreChange(data) {
        this.setState({nodes: data});
    }

    hasItems() {
        return this.props.store.getFilteredCount() > 0;
    }

    isEmptyList() {
        return (
            this.props.store.isLoaded() &&
            !this.props.store.hasFilters() &&
            !this.props.store.getTotalCount()
        );
    }

    isEmptyListMessageVisible() {
        return this.props.store.isLoaded() && !this.hasItems();
    }

    renderItems() {
        const result = [];

        this.state.nodes.forEach((node, index) => {
            this.props.store.applyCallbackToSubtree(node, index, (record, recordIndex, level) => {
                result.push(this.renderItem(record, recordIndex, level) as never);
            });
        });

        return result;
    }

    renderItem(record, index, level) {
        return null;
    }
}
