import * as React from 'react';
import {
    BasicConfig, BasicContainer, BasicContainerPropsInterface,
    defaultData
} from '../../render/core/Container/types';
import './Container.css';
import {createChild} from '../../render/util/createChild';
import Container from '../../render/core/Container/index';
import {ProviderSourceConfig} from '../../render/core/DataProvider/Controller';
import componentLoader from '../../render/util/componentLoader';
import {CustomerSourceConfig} from '../../render/core/DataCustomer/Controller';

export class ContainerConfig extends BasicConfig {
    /**
     * 字级组件
     */
    children: BasicConfig[];

    /**
     * 数据模型Key
     */
    model: string;

    /**
     * 初始化数据
     */
    data?: defaultData;

    /**
     * container 继承属性映射
     */
    props?: Object;

    /**
     * 自动同步子级的对应属性的值到父级
     */
    bind?: {child: string; parent: string}[];

    /**
     * dataProvider配置
     */
    dataProvider?: ProviderSourceConfig[];

    /**
     * dataCustomer配置
     */
    dataCustomer?: CustomerSourceConfig;
}

export class ContainerPropsInterface extends BasicContainerPropsInterface {
    info: ContainerConfig;
}

export default class AbstractContainer extends BasicContainer<ContainerPropsInterface, {}> {
    constructor(props: ContainerPropsInterface) {
        super(props);
    }

    render() {
        let children;

        if (Array.isArray(this.props.info.children)) {
            children = this.props.info.children.map((child, index) => {
                return this.renderChild(child, 0, index);
            });
        }

        let childElement = React.createElement(Container, this.props, children);

        return this.renderChildren(this.props.info, childElement);
    }

    private renderChild(info: BasicConfig, depth: number, index: number) {
        return createChild(info, {
            key: `${info.type}_${depth}_${index}`,
            info: info
        });
    }
}

componentLoader.addComponent('container', AbstractContainer, ContainerPropsInterface);
