"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
function runCodeHere(el, code) {
    if (!el) {
        return;
    }
    // Compatibility with Jupyter/notebook JS evaluation.  Set element so
    // the user has a handle on the context of the current output.
    const element = el;
    try {
        // tslint:disable-next-line:no-eval
        return eval(code);
    }
    catch (err) {
        const pre = document.createElement("pre");
        pre.style.whiteSpace = "pre-wrap";
        if (err.stack) {
            pre.textContent = err.stack;
        }
        else {
            pre.textContent = err;
        }
        element.appendChild(pre);
        return err;
    }
}
exports.runCodeHere = runCodeHere;
class JavaScript extends react_1.default.PureComponent {
    componentDidMount() {
        runCodeHere(this.el, this.props.data);
    }
    componentDidUpdate() {
        runCodeHere(this.el, this.props.data);
    }
    render() {
        return (react_1.default.createElement("div", { ref: el => {
                this.el = el;
            } }));
    }
}
exports.JavaScript = JavaScript;
JavaScript.defaultProps = {
    data: "",
    mediaType: "application/javascript"
};
exports.default = JavaScript;
