"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
var __extends = (this && this.__extends) || (function () {
    var 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 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) || 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var isEqual = require("lodash/isEqual");
var omit = require("lodash/omit");
var React = require("react");
var react_jsonschema_form_1 = require("react-jsonschema-form");
var styled_components_1 = require("styled-components");
var Button_1 = require("../../../components/Button");
var Grid_1 = require("../../../components/Grid");
var utils = require("../../../utils");
var DescriptionField_1 = require("./fields/DescriptionField");
var TitleField_1 = require("./fields/TitleField");
var ArrayFieldTemplate_1 = require("./templates/ArrayFieldTemplate");
var FieldTemplate_1 = require("./templates/FieldTemplate");
var ObjectFieldTemplate_1 = require("./templates/ObjectFieldTemplate");
var BaseInput_1 = require("./widgets/BaseInput");
var SUPPORTED_SCHEMA_FORMATS = [
    'data-url',
    'date',
    'date-time',
    'email',
    'hostname',
    'ipv4',
    'ipv6',
    'uri',
];
var widgets = {
    BaseInput: BaseInput_1.default,
};
var fields = {
    DescriptionField: DescriptionField_1.DescriptionField,
    TitleField: TitleField_1.TitleField,
};
var FormWrapper = styled_components_1.default(Grid_1.Box)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\tfieldset {\n\t\tmargin: 0;\n\t\tpadding: 0;\n\t\tborder: 0;\n\t}\n"], ["\n\tfieldset {\n\t\tmargin: 0;\n\t\tpadding: 0;\n\t\tborder: 0;\n\t}\n"])));
var FormHOC = (function (_super) {
    __extends(FormHOC, _super);
    function FormHOC(props) {
        var _this = _super.call(this, props) || this;
        _this.change = function (data) {
            _this.setState({ value: data.formData });
            if (_this.props.onFormChange) {
                _this.props.onFormChange(data);
            }
        };
        _this.submit = function (data) {
            if (_this.props.onFormSubmit) {
                _this.props.onFormSubmit(data);
            }
        };
        _this.state = {
            value: _this.props.value || {},
            schema: _this.parseSchema(_this.props.schema),
        };
        return _this;
    }
    FormHOC.registerWidget = function (name, value) {
        widgets[name] = value;
    };
    FormHOC.prototype.componentWillReceiveProps = function (nextProps) {
        if (!isEqual(this.props.value, nextProps.value)) {
            this.setState({
                value: nextProps.value,
            });
        }
        if (!isEqual(this.props.schema, nextProps.schema)) {
            this.setState({
                schema: this.parseSchema(nextProps.schema),
            });
        }
    };
    FormHOC.prototype.parseSchema = function (schema) {
        var whitelist = SUPPORTED_SCHEMA_FORMATS.concat(Object.keys(widgets));
        return utils.stripSchemaFormats(schema, whitelist);
    };
    FormHOC.prototype.render = function () {
        var _a = this.props, hideSubmitButton = _a.hideSubmitButton, submitButtonText = _a.submitButtonText, submitButtonProps = _a.submitButtonProps, uiSchema = _a.uiSchema;
        var cleanProps = omit(this.props, [
            'schema',
            'submitButtonText',
            'hideSubmitButton',
            'submitButtonProps',
            'value',
            'onFormChange',
            'onFormSubmit',
            'uiSchema',
        ]);
        var schema = utils.disallowAdditionalProperties(this.state.schema);
        return (React.createElement(FormWrapper, __assign({}, cleanProps),
            React.createElement(react_jsonschema_form_1.default, { schema: schema, formData: this.state.value, onSubmit: this.submit, onChange: this.change, uiSchema: uiSchema, widgets: widgets, fields: fields, FieldTemplate: FieldTemplate_1.default, ObjectFieldTemplate: ObjectFieldTemplate_1.default, ArrayFieldTemplate: ArrayFieldTemplate_1.default },
                hideSubmitButton && React.createElement("span", null),
                !hideSubmitButton && (React.createElement(Button_1.default, __assign({ primary: true }, submitButtonProps), submitButtonText || 'Submit')))));
    };
    return FormHOC;
}(React.Component));
exports.default = FormHOC;
var templateObject_1;
