'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);
};
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { FormErrorAutoFocusContainer } from './FormErrorAutoFocusContainer';
var Form = /** @class */ (function (_super) {
    __extends(Form, _super);
    function Form(props, context) {
        var _this = _super.call(this, props, context) || this;
        _this.getNeededAutoFocus = function () {
            return _this.formErrorAutoFocusContainer.getNeededAutoFocus();
        };
        _this.setNeededAutoFocus = function (value) {
            _this.formErrorAutoFocusContainer.setNeededAutoFocus(value);
        };
        _this.setValue = _this.setOutputValue.bind(_this);
        return _this;
    }
    Form.prototype.setValue = function (value, diff) { };
    Form.prototype.getChildContext = function () {
        return {
            form: {
                readOnly: this.extractFlatProperty('readOnly'),
                value: this.getInputValue(),
                errors: this.extractProperty('errors'),
                setValue: this.setValue
            }
        };
    };
    Form.prototype.getInputValue = function () {
        return this.inputConverter(this.extractProperty('value'));
    };
    Form.prototype.setOutputValue = function (value, diff) {
        var _a, _b;
        value = this.outputConverter(__assign({}, this.getInputValue(), value), diff === undefined ? value : diff);
        if (diff === undefined) {
            diff = value;
        }
        if (this.props.onChange) {
            this.props.onChange(value, diff);
        }
        else if (this.props.name && this.context.form) {
            this.context.form.setValue((_a = {}, _a[this.props.name] = value, _a), (_b = {}, _b[this.props.name] = diff, _b));
        }
    };
    Form.prototype.inputConverter = function (value) {
        return this.props.inputConverter
            ? this.props.inputConverter.call(this, value)
            : value;
    };
    Form.prototype.outputConverter = function (value, diff) {
        return this.props.outputConverter
            ? this.props.outputConverter.call(this, value, diff)
            : value;
    };
    Form.prototype.extractProperty = function (propName, props) {
        if (props === void 0) { props = this.props; }
        var value = props[propName];
        var form = this.context.form;
        if (form && value === undefined) {
            value = form[propName][props.name];
        }
        return value || {};
    };
    Form.prototype.extractFlatProperty = function (propName) {
        var value = this.props[propName];
        var form = this.context.form;
        if (form && value === undefined) {
            value = form[propName];
        }
        return value;
    };
    Form.prototype.render = function () {
        var _this = this;
        return (React.createElement(FormErrorAutoFocusContainer, { ref: function (el) { return (_this.formErrorAutoFocusContainer = el); } }, this.props.noContainer ? (this.props.children) : (React.createElement("div", { "data-test-automation-id": this.props.testAutomationId, className: classnames('rc-form', this.props.className) }, this.props.children))));
    };
    Form.childContextTypes = {
        form: PropTypes.object
    };
    Form.contextTypes = {
        form: PropTypes.object
    };
    Form.propTypes = {
        onChange: PropTypes.func,
        name: PropTypes.string,
        inputConverter: PropTypes.func,
        outputConverter: PropTypes.func,
        noContainer: PropTypes.bool,
        testAutomationId: PropTypes.string,
        children: PropTypes.node,
        className: PropTypes.string
    };
    Form.defaultProps = {
        onChange: null,
        name: '',
        inputConverter: null,
        outputConverter: null,
        children: null,
        className: null,
        testAutomationId: 'form',
        noContainer: false
    };
    return Form;
}(React.Component));
export { Form };
//# sourceMappingURL=Form.js.map