import { createVnode, createElement, fiberizeChildren } from "./createElement";
import { Refs } from "./Refs";
import { DOMUpdater } from "./DOMUpdater";
import { disposeChildren } from "./dispose";

function Portal(props) {
    this.isPortal = true;
    this.container = props.container;
    this.children = props._children;
}
Portal.prototype = {
    constructor: Portal,
    componentWillUnmount() {
        disposeChildren(this._children, this.updater.updateQueue);
    },
    componentWillReceiveProps(nextProps, context) {
        var vnode = this.container;
        let lastChildren = this._children;
        let updateQueue = this.updater.updateQueue;
        let nextChildren = (this._children = fiberizeChildren(nextProps._children, this._updater));
        Refs.diffChildren(lastChildren, nextChildren, vnode, context, updateQueue, []);

        // Refs.receiveVnode(vnode, nextProps.container, context, this.updater.updateQueue, []);
    },
    componentWillMount() {
        let vnode = this.container;
        let updateQueue = this.updater.updateQueue;
        let updater = (this._updater = new DOMUpdater(vnode));
        let children = (this._children = fiberizeChildren(this.children, updater));
        Refs.diffChildren({}, children, vnode, children, {}, updateQueue, []);
    },
    render() {
        return null;
    }
};
function inner(nextProps, context, this){
    var lastChildren = this._children || {}, nextChildren
    if(nextProps){
       nextChildren = (this._children = fiberizeChildren(nextProps._children, this._updater));
    }

}

//[Top API] ReactDOM.createPortal
export function createPortal(children, node) {
    var container = createVnode(node);
    var props = container.props;
    //  props.children = children;
    var portal = createElement(Portal, {
        container,
        _children: children
    });
    container.return = portal;
    return portal;
}
