"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var schviz_1 = require("@scion-scxml/schviz");
var scxml = require("@scion-scxml/scxml");
var scion = require("@scion-scxml/core");
var handle_errors_1 = require("../handle-errors");
var fs = require("fs");
var AppComponent = /** @class */ (function (_super) {
    __extends(AppComponent, _super);
    function AppComponent(props) {
        var _this = _super.call(this, props) || this;
        //read, and perform initial render
        var scxmlContents;
        try {
            scxmlContents = fs.readFileSync(props.scxmlPath, 'utf8');
        }
        catch (err) {
            handle_errors_1.handleError(err);
            return _this;
        }
        //if he is SCXML, convert him to scjson
        var initialLayout = 'right';
        var tic = new Date();
        var scjson = scxml.ext.compilerInternals.scxmlToScjson(scxmlContents);
        var toc = new Date();
        console.log('SCXML -> SCJSON duration', toc - tic);
        _this.state = {
            interpreter: null,
            scjson: scjson,
            layoutName: initialLayout,
            layoutOptions: schviz_1.default.layouts[initialLayout]
        };
        //if everything worked, then watch the file for changes
        fs.watchFile(props.scxmlPath, { persistent: true, interval: 100 }, function (cur, prev) {
            handle_errors_1.clear();
            var scxmlContents = fs.readFileSync(props.scxmlPath, 'utf8');
            var tic = new Date();
            var scjson = scxml.ext.compilerInternals.scxmlToScjson(scxmlContents);
            var toc = new Date();
            console.log('SCXML -> SCJSON duration', toc - tic);
            //if he is SCXML, convert him to scjson
            _this.setState({
                interpreter: null,
                scjson: scjson
            });
        });
        return _this;
    }
    AppComponent.prototype.handleRunButtonClick = function (event) {
        //create a new scxml instance
        //bind it to highlight behavior
        //then call setState
        if (this.state.interpreter) {
            this.setState({
                interpreter: null,
                configuration: null
            });
        }
        else {
            //start him
            this.startScxml();
        }
    };
    AppComponent.prototype.startScxml = function () {
        var _this = this;
        //1 - 2. get the xml file and convert it to json
        scxml.pathToModel(this.props.scxmlPath, function (err, model) {
            if (err) {
                console.error(err);
                process.exit(1);
            }
            model.prepare(function (err, fnModel) {
                if (err) {
                    console.error(err);
                    process.exit(1);
                }
                //Use the statechart object model to instantiate an instance of the statechart interpreter. Optionally, we can pass to the construct an object to be used as the context object (the 'this' object) in script evaluation. Lots of other parameters are available.
                var interpreter = new scion.Statechart(fnModel);
                interpreter.start();
                _this.setState({
                    interpreter: interpreter,
                    configuration: interpreter.getConfiguration()
                });
            });
        });
    };
    AppComponent.prototype.sendEvent = function (eventObject) {
        this.setState({
            configuration: this.state.interpreter.gen(eventObject)
        });
    };
    AppComponent.prototype.render = function () {
        if (!this.state)
            return React.createElement("div", null, "There was an error.");
        return React.createElement("div", { id: "embed_outer", className: this.state.interpreter ? 'simulation-mode' : 'viz-mode' },
            React.createElement("div", { id: "embed_inner" },
                React.createElement("div", { id: "scxml-content" },
                    React.createElement(schviz_1.default, { scjson: this.state.scjson, layoutOptions: this.state.layoutOptions, configuration: this.state.configuration, hideActions: this.props.hideActions }))));
    };
    return AppComponent;
}(React.Component));
exports.default = AppComponent;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQXBwQ29tcG9uZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbXBvbmVudHMvQXBwQ29tcG9uZW50LnRzeCJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7OztBQUFBLDZCQUErQjtBQUMvQiw4Q0FBeUM7QUFDekMsMENBQTZDO0FBQzdDLHlDQUE0QztBQUM1Qy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