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() {
        inner.call(this,arguments);
    },
    componentWillMount() {
        inner.call(this,arguments);
    },
    render() {
        return null;
    }
};
function inner(nextProps, context){
    let vnode = this.vnode;
    let lastChildren = this._children || {}, nextChildren;
    let updateQueue = this.updater.updateQueue;
    if(!this._updater){
        this._updater = new DOMUpdater(vnode);
    }
    if(nextProps){
        nextChildren = (this._children = fiberizeChildren(nextProps._children, this._updater));
    }
    Refs.diffChildren(lastChildren, nextChildren, vnode, context || {}, updateQueue, []);

}

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