"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 (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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 });
var jellyschema = require("jellyschema");
var cloneDeep = require("lodash/cloneDeep");
var isEqual = require("lodash/isEqual");
var merge = require("lodash/merge");
var omit = require("lodash/omit");
var React = require("react");
var Form_1 = require("../Form");
var formulas_1 = require("./formulas");
var computeFormSchemas = function (jellySchema, uiSchema) {
    var computed = jellyschema.generateJsonAndUiSchema(jellySchema);
    return {
        schema: computed.jsonSchema,
        uiSchema: merge({}, computed.uiSchema, uiSchema),
    };
};
var JellyForm = (function (_super) {
    __extends(JellyForm, _super);
    function JellyForm(props) {
        var _this = _super.call(this, props) || this;
        _this.onChange = function (changeData) {
            var formData = changeData.formData;
            var schema = _this.state.schema;
            var data = cloneDeep(formData);
            var evaluate = formulas_1.runFormulas(schema, data);
            var startingState = formulas_1.defaultValueForSchema(schema);
            var result = merge(startingState, formData, evaluate);
            _this.setState({ value: result });
            if (_this.props.onFormChange) {
                _this.props.onFormChange(__assign({}, changeData, { formData: result }));
            }
        };
        var _a = computeFormSchemas(props.schema, props.uiSchema), schema = _a.schema, uiSchema = _a.uiSchema;
        _this.state = {
            value: props.value || formulas_1.defaultValueForSchema(schema),
            schema: schema,
            uiSchema: uiSchema,
        };
        return _this;
    }
    JellyForm.prototype.componentWillReceiveProps = function (nextProps) {
        if (!isEqual(this.props.value, nextProps.value)) {
            this.setState({
                value: formulas_1.runFormulas(this.state.schema, nextProps.value),
            });
        }
        if (!isEqual(this.props.schema, nextProps.schema) ||
            !isEqual(this.props.uiSchema, nextProps.uiSchema)) {
            var _a = computeFormSchemas(nextProps.schema, nextProps.uiSchema), schema = _a.schema, uiSchema = _a.uiSchema;
            this.setState({
                schema: schema,
                uiSchema: uiSchema,
            });
        }
    };
    JellyForm.prototype.render = function () {
        var _a = this.state, value = _a.value, schema = _a.schema, uiSchema = _a.uiSchema;
        var options = omit(this.props, [
            'value',
            'schema',
            'uiSchema',
            'onFormChange',
        ]);
        return (React.createElement(Form_1.default, __assign({ value: value, schema: schema, uiSchema: uiSchema, onFormChange: this.onChange }, options)));
    };
    return JellyForm;
}(React.Component));
exports.default = JellyForm;
