import { createTrait } from '@meta-ui/core';
import { Type } from '@sinclair/typebox';
import { isEqual } from 'lodash-es';
import { ValidResultSchema } from '../../types/ValidResultSchema';
const rules = new Map();
export function addValidationRule(name, rule) {
    rules.set(name, rule);
}
addValidationRule('email', text => {
    if (/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(text)) {
        return {
            isInvalid: false,
            errorMsg: '',
        };
    }
    else {
        return {
            isInvalid: true,
            errorMsg: '请输入正确的 email',
        };
    }
});
addValidationRule('phoneNumber', text => {
    if (/^1[3456789]\d{9}$/.test(text)) {
        return {
            isInvalid: false,
            errorMsg: '',
        };
    }
    else {
        return {
            isInvalid: true,
            errorMsg: '请输入正确的手机号码',
        };
    }
});
const ValidationResultCache = {};
const ValidationTraitImpl = props => {
    const { value, minLength, maxLength, mergeState, componentId, rule } = props;
    const result = {
        isInvalid: false,
        errorMsg: '',
    };
    if (value.length > maxLength) {
        result.isInvalid = true;
        result.errorMsg = `最长不能超过${maxLength}个字符`;
    }
    else if (value.length < minLength) {
        result.isInvalid = true;
        result.errorMsg = `不能少于${minLength}个字符`;
    }
    else {
        const rulesArr = rule ? rule.split(',') : [];
        for (const ruleName of rulesArr) {
            const validateFunc = rules.get(ruleName);
            if (validateFunc) {
                const { isInvalid, errorMsg } = validateFunc(value);
                if (isInvalid) {
                    result.isInvalid = true;
                    result.errorMsg = errorMsg;
                    break;
                }
            }
        }
    }
    if (!isEqual(result, ValidationResultCache[componentId])) {
        ValidationResultCache[componentId] = result;
        mergeState({
            validResult: result,
        });
    }
    return {
        props: null,
    };
};
const PropsSchema = Type.Object({
    value: Type.String(),
    rule: Type.String(),
    maxLength: Type.Integer(),
    minLength: Type.Integer(),
});
export default Object.assign(Object.assign({}, createTrait({
    version: 'core/v1',
    metadata: {
        name: 'validation',
        description: 'validation trait',
    },
    spec: {
        properties: PropsSchema,
        state: Type.Object({
            validResult: ValidResultSchema,
        }),
        methods: [],
    },
})), { impl: ValidationTraitImpl });
//# sourceMappingURL=validation.js.map