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

    let nextChildren = (this._children = fiberizeChildren(vnode.props.child, this._updater));
    console.log(lastChildren, nextChildren,vnode, this.context, updateQueue);
    Refs.diffChildren(lastChildren, nextChildren, vnode, this.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;
}
