import React, { PureComponent } from 'react';
import { View } from 'react-native';
import memoize from 'memoize-one';
import * as t from 'tcomb-form-native';
import { aboveLabels, floatingLabels, hiddenLabels, inlineLabels, styles } from './Templates';
const TcombForm = t.form.Form;
const defaultTemplates = t.form.Form.templates;
export var FormLabelPosition;
(function (FormLabelPosition) {
    FormLabelPosition[FormLabelPosition["Above"] = 0] = "Above";
    FormLabelPosition[FormLabelPosition["Floating"] = 1] = "Floating";
    FormLabelPosition[FormLabelPosition["Hidden"] = 2] = "Hidden";
    FormLabelPosition[FormLabelPosition["Inline"] = 3] = "Inline";
})(FormLabelPosition || (FormLabelPosition = {}));
const LabelMap = {
    [FormLabelPosition.Inline]: inlineLabels,
    [FormLabelPosition.Hidden]: hiddenLabels,
    [FormLabelPosition.Floating]: floatingLabels,
    [FormLabelPosition.Above]: aboveLabels
};
export class Form extends PureComponent {
    constructor(props) {
        super(props);
        this.calculateStyles = memoize((activeColor, errorColor, inactiveColor) => {
            return styles({ activeColor, errorColor, inactiveColor });
        });
        this.calculateBlurs = memoize((fieldsOptions) => {
            Object.keys(fieldsOptions).forEach(path => {
                const prevOnBlur = fieldsOptions[path].onBlur;
                fieldsOptions[path].onBlur = () => {
                    if (prevOnBlur instanceof Function) {
                        prevOnBlur();
                    }
                    this.validateField(path);
                };
            });
        });
        this.getValue = () => {
            if (this.form.current) {
                return this.form.current.getValue();
            }
        };
        this.validate = () => {
            if (this.form.current) {
                return this.form.current.validate();
            }
        };
        this.validateField = (path) => {
            if (this.form.current) {
                return this.form.current.getComponent(path).validate();
            }
        };
        this.getComponent = (...args) => {
            if (this.form.current) {
                return this.form.current.getComponent.apply(this.form.current, args);
            }
        };
        this.form = React.createRef();
    }
    render() {
        const { errorColor = '#d0021b', activeColor = '#000000', inactiveColor = '#9B9B9B', fieldsTypes, fieldsOptions, fieldsStyleConfig, labelPosition = FormLabelPosition.Inline, style, value, onChange, templates, validateOnBlur } = this.props;
        const stylesheet = this.calculateStyles(activeColor, errorColor, inactiveColor);
        if (validateOnBlur !== false) {
            this.calculateBlurs(fieldsOptions);
        }
        const _options = {
            stylesheet: Object.assign({}, stylesheet, fieldsStyleConfig),
            fields: fieldsOptions,
            templates: Object.assign({}, defaultTemplates, LabelMap[labelPosition], templates)
        };
        return (<View style={style}>
        <TcombForm ref={this.form} options={_options} type={fieldsTypes} onChange={onChange} value={value}/>
      </View>);
    }
}
Form.defaultProps = {
    errorColor: '#d0021b',
    activeColor: '#000000',
    inactiveColor: '#cccccc',
    labelPosition: FormLabelPosition.Inline
};
//# sourceMappingURL=Form.js.map