'use strict';
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
require("./polyfills");
var reactDom = require("react-dom");
var React = require("react");
// import * as builderReact from '@builder.io/react'
var emotionCore = require("@emotion/core");
var emotionStyled = require("@emotion/styled");
var mobx = require("mobx");
var mobxReact = require("mobx-react");
var mst = require("@builder.io/mobx-state-tree");
var mui = require("@material-ui/core");
var muiIcons = require("@material-ui/icons");
var builder = require("@builder.io/sdk");
// import { CloudinaryImageEditor } from '@builder.io/plugin-cloudinary/dist/lib'
// reactDom.render(CloudinaryImageEditor, document.body)
var context = {
    user: {
        organization: {
            value: {
                settings: {
                    plugins: {
                        get: function () { },
                        set: function () { }
                    }
                }
            }
        }
    }
};
require("systemjs/dist/system.js");
require("systemjs/dist/extras/named-register.js");
// Ugh, webpack...
exports.system = eval('System');
var exposePackages = {
    react: React,
    'react-dom': reactDom,
    '@builder.io/sdk': builder,
    // '@builder.io/react': builderReact,
    '@emotion/core': emotionCore,
    '@emotion/styled': emotionStyled,
    mobx: mobx,
    'mobx-state-tree': mst,
    'mobx-react': mobxReact,
    '@material-ui/core': mui,
    '@material-ui/icons': muiIcons
};
Object.keys(exposePackages).forEach(function (packageName) {
    exports.system.register(packageName, [], function (_export) {
        return {
            execute: function () {
                var content = exposePackages[packageName];
                _export(__assign(__assign({}, content), { default: content }));
            }
        };
    });
});
console.log('hi from worker');
if (typeof self !== 'undefined') {
    console.log(1);
    self.addEventListener('message', function (event) {
        // console.log(2, event)
        var data = event.data;
        if (data && data.type === 'builder.loadPlugin') {
            console.log('loadPluginMessage', event.data);
            var plugin = '@builder.io/plugin-cloudinary';
            var pluginUrl = plugin.startsWith('http') ? plugin : 'https://unpkg.com/' + plugin;
            exports.system
                .import(pluginUrl)
                .then(function (output) {
                // TODO: read the exported component
                var editor = builder.Builder.editors[0];
                if (editor && editor.component) {
                    // TODO: bind value, onchange, context
                    console.log('a2');
                    reactDom.render(React.createElement(editor.component), document.body);
                }
                else if (output.default) {
                    console.log('b2');
                    reactDom.render(React.createElement(output.default), document.body);
                }
            })
                .catch(console.error);
        }
    });
    self.postMessage({ type: 'builder.workerLoaded' }, '*');
}
// reactDom.render(React.createElement('div', {}, 'hello there'), document.body)
setTimeout(function () {
    var plugin = '@builder.io/plugin-cloudinary';
    var pluginUrl = plugin.startsWith('http') ? plugin : 'https://unpkg.com/' + plugin;
    exports.system
        // TODO: why is systemjs loading/returning React as output here??
        .import(pluginUrl)
        .then(function (output) {
        setTimeout(function () {
            console.log('output', output);
            var editor = builder.Builder.editors[0];
            if (editor && editor.component) {
                console.log('a');
                // TODO: bind value, onchange, context
                reactDom.render(React.createElement(editor.component, { context: context }), document.body);
            }
            else if (output.default) {
                console.log('b');
                reactDom.render(React.createElement(output.default, { context: context }), document.body);
            }
        });
    })
        .catch(console.error);
}, 100);
//# sourceMappingURL=plugin-loader.js.map