/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __awaiter, __generator, __values } from 'tslib';
import pick from 'lodash/pick';
import React__default from 'react';
import { validateObject, validations } from 'amis-core';

function formatErrors(errors) {
    var formated = {};
    Object.keys(errors).forEach(function (key) {
        var origin = errors[key][0];
        if (origin) {
            formated[key] = {
                type: origin.rule,
                message: origin.msg
            };
        }
    });
    return formated;
}
function useValidationResolver(__, validate) {
    var _this = this;
    if (__ === void 0) { __ = function (str) { return str; }; }
    return React__default.useCallback(function (values, context, config) { return __awaiter(_this, void 0, void 0, function () {
        var rules, customValidator, ruleKeys, _a, _b, key, field, errors, _c, _d, key, validate_1, result, e_1_1, e_2;
        var e_3, _e, e_1, _f;
        return __generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    rules = {};
                    customValidator = {};
                    ruleKeys = Object.keys(validations);
                    try {
                        for (_a = __values(Object.keys(config.fields)), _b = _a.next(); !_b.done; _b = _a.next()) {
                            key = _b.value;
                            field = config.fields[key];
                            rules[key] = pick(field, ruleKeys);
                            if (field.required) {
                                rules[key].isRequired = true;
                            }
                            if (typeof field.validate === 'function') {
                                customValidator[key] = field.validate;
                            }
                        }
                    }
                    catch (e_3_1) { e_3 = { error: e_3_1 }; }
                    finally {
                        try {
                            if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
                        }
                        finally { if (e_3) throw e_3.error; }
                    }
                    errors = validateObject(values, rules, undefined, __);
                    _g.label = 1;
                case 1:
                    _g.trys.push([1, 6, 7, 8]);
                    _c = __values(Object.keys(customValidator)), _d = _c.next();
                    _g.label = 2;
                case 2:
                    if (!!_d.done) return [3 /*break*/, 5];
                    key = _d.value;
                    validate_1 = customValidator[key];
                    return [4 /*yield*/, validate_1(values[key])];
                case 3:
                    result = _g.sent();
                    if (typeof result === 'string') {
                        errors[key] = errors[key] || [];
                        errors[key].push({
                            rule: 'custom',
                            msg: result
                        });
                    }
                    _g.label = 4;
                case 4:
                    _d = _c.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_1_1 = _g.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 8:
                    _g.trys.push([8, 10, , 11]);
                    return [4 /*yield*/, (validate === null || validate === void 0 ? void 0 : validate(errors, values, context, config))];
                case 9:
                    _g.sent();
                    return [3 /*break*/, 11];
                case 10:
                    e_2 = _g.sent();
                    errors.customValidate = [
                        {
                            rule: 'custom',
                            msg: e_2.message || e_2
                        }
                    ];
                    return [3 /*break*/, 11];
                case 11: return [2 /*return*/, {
                        values: values,
                        errors: formatErrors(errors)
                    }];
            }
        });
    }); }, [__, validate]);
}

export { useValidationResolver as default, useValidationResolver };
