import { InputItem, Toast } from 'antd-mobile';
import * as React from 'react';
import { Component } from 'react';
import GlobalState from '../lib/state';
export * from 'antd-mobile';
import 'antd-mobile/dist/antd-mobile.css';
import './antd.css';
export class AForm extends GlobalState {
    constructor() {
        super(...arguments);
        this.getFieldProps = (name, ...args) => {
            var { getFieldProps, getFieldError } = this.props.form;
            var attrs = {
                error: getFieldError(name),
                onErrorClick: () => Toast.info(getFieldError(name))
            };
            return Object.assign(attrs, getFieldProps(name, ...args));
        };
        this.getFieldsError = (...args) => this.props.form.getFieldsError(...args);
        this.getFieldError = (...args) => this.props.form.getFieldError(...args);
        this.validateFields = (...args) => this.props.form.validateFields(...args);
        this.getFieldDecorator = (...args) => this.props.form.getFieldDecorator(...args);
        this.isFieldsTouched = (...args) => this.props.form.isFieldsTouched(...args);
        this.isFieldValidating = (...args) => this.props.form.isFieldValidating(...args);
        this.getFieldValue = (...args) => this.props.form.getFieldValue(...args);
    }
    _check(name) {
        var { rules } = this.props.form.getFieldInstance(name).props;
        var value = this.props.form.getFieldValue(name);
        if (rules) {
            return rules.every((rule) => {
                if (rule.validator) {
                    var err;
                    rule.validator(rule, value, (e) => err = e, {}, {});
                    if (err)
                        return false;
                }
                else if (rule.required && !value) {
                    return false;
                }
                else if (rule.pattern) {
                    if (!rule.pattern.exec(value)) {
                        return false;
                    }
                }
                return true;
            });
        }
        return true;
    }
    get canSubmit() {
        return this.isCanSubmit();
    }
    isCanSubmit() {
        var errors = this.getFieldsError();
        for (var i in errors) {
            if (errors[i]) {
                return false;
            }
            else {
                if (!this.isFieldValidating(i)) {
                    if (!this._check(i)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    async componentDidMount() {
        await this.onLoad();
        this.setState(this.state);
    }
    componentWillUnmount() {
        this.onUnload();
    }
    onLoad() {
    }
    onUnload() {
    }
    rulesRequired(message) {
        return {
            validator: (rule, value, callback, source, options) => {
                if ((value || value + 1 === 1) && value !== 'undefined') {
                    if (Array.isArray(value)) {
                        if (value.length && value.every(e => (e || e + 1 === 1) && e !== 'undefined')) {
                            callback();
                        }
                        else {
                            callback(rule.message);
                        }
                    }
                    else {
                        callback();
                    }
                }
                else {
                    callback(rule.message);
                }
            },
            message,
        };
    }
}
export class AFItem extends Component {
    get name() {
        return this.props.name;
    }
    get form() {
        return this.props.form;
    }
    render() {
        var { getFieldProps, getFieldError, getFieldDecorator } = this.form;
        var { name, tag: Tag = InputItem, type = 'text', errorIcon, error = getFieldError(this.props.name), onErrorClick = () => Toast.info(getFieldError(this.props.name)), initialValue, rules, children, form, ...propss } = this.props;
        if (Tag == 'none') {
            var resolve = function (vdom) {
                var children = vdom.props.children;
                if (!children) {
                    return vdom;
                }
                children = Array.isArray(children) ? children : [children];
                var new_children = [];
                for (var i = 0; i < children.length; i++) {
                    var ch = children[i];
                    if (typeof ch.type == 'string') {
                        if (ch.type == 'input') {
                            new_children.push(getFieldDecorator(name, {
                                initialValue,
                                rules,
                            })(React.cloneElement(ch, { ...propss, key: i })));
                        }
                        else {
                            new_children.push(ch);
                        }
                    }
                    else {
                        new_children.push(resolve(ch));
                    }
                }
                return React.createElement(vdom.type, { ...vdom.props, key: vdom.key }, ...new_children);
            };
            return (React.createElement("div", { className: `cc-item-wrap ${error ? 'cc-item-wrap-error' : ''}` },
                resolve(children),
                React.createElement("div", { className: "cc-error-extra", onClick: onErrorClick })));
        }
        else {
            var fieldProps = getFieldProps(name, {
                initialValue,
                rules,
            });
            var vdom = (React.createElement(Tag, Object.assign({ type: type, error: error, onErrorClick: onErrorClick }, fieldProps, this.props), children));
            if (errorIcon) {
                return (React.createElement("div", { className: `cc-item-wrap ${error ? 'cc-item-wrap-error' : ''}` },
                    vdom,
                    React.createElement("div", { className: "cc-error-extra", onClick: onErrorClick })));
            }
            else {
                return vdom;
            }
        }
    }
}
