"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const construct_1 = require("./construct");
var jsx;
(function (jsx) {
    /**
     * The JSX factory function. This is what TypeScript converts a JSX statement to.
     *
     * For example:
     *      <Foo p1='a' p2={2}/>
     * will produce this code:
     *      jsx.create(Foo, { p1: 'a', p2: 2 }, []);
     *
     * This function will not actually create any objects, but rather just return a tree of
     * element information for later consumption by jsx.construct(tree), which can be used
     * to materialize an actual construct tree from.
     *
     * @param type The class
     * @param props Property hash
     * @param children Array of children
     * @returns element tree
     */
    function create(type, props, ...children) {
        if (!(type.prototype instanceof construct_1.Construct)) {
            throw new Error('All nodes must derive from Construct: ' + type);
        }
        return {
            type, props, children
        };
    }
    jsx.create = create;
    /**
     * Converts a JSX tree to a construct tree.
     * Creates all construct objects and associate them together as children.
     * @param tree The JSX tree
     * @param parent Optional parent for the construct tree
     * @returns A Construct object
     */
    function construct(tree, parent) {
        const id = (tree.props && tree.props.id) || '';
        const root = new tree.type(parent, id, tree.props); // create root
        createChildren(root, tree.children);
        return root;
    }
    jsx.construct = construct;
    function createChildren(parent, children) {
        for (const child of children) {
            const id = (child.props && child.props.id) || '';
            const childObj = new child.type(parent, id, child.props);
            createChildren(childObj, child.children);
        }
    }
})(jsx = exports.jsx || (exports.jsx = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoianN4LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsianN4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsMkNBQXdDO0FBRXhDLElBQWlCLEdBQUcsQ0FtRG5CO0FBbkRELFdBQWlCLEdBQUc7SUFFaEI7Ozs7Ozs7Ozs7Ozs7Ozs7T0FnQkc7SUFDSCxTQUFnQixNQUFNLENBQUMsSUFBUyxFQUFFLEtBQVUsRUFBRSxHQUFHLFFBQWU7UUFDNUQsSUFBSSxDQUFDLENBQUMsSUFBSSxDQUFDLFNBQVMsWUFBWSxxQkF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