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

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

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