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

function Portal(props,context) {
    this.isPortal = true;
    this.props = props;
    this.context = context;
}
Portal.prototype = {
    constructor: Portal,
    componentWillUnmount() {
        disposeChildren(this._children, this.updater.updateQueue);
    },
    componentWillReceiveProps() {
        inner.apply(this,arguments);
    },
    componentWillMount() {
        inner.call(this, this.props);
    },
    render() {
        return null;
    }
};
function inner(nextProps, context){
    let vnode = this.props.vnode;
    let lastChildren = this.child || {}, nextChildren;
    let updateQueue = this.updater.updateQueue;
    if(!this._updater){
        this._updater = new DOMUpdater(vnode);
    }
    if(nextProps){
        nextChildren = (this._children = fiberizeChildren(vnode.child, this._updater));
    }
    Refs.diffChildren(lastChildren, nextChildren, vnode, context || {}, updateQueue, []);

}

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