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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var reactHookForm = require('react-hook-form');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
function FormField(props) {
    var _a, _b, _c, _d;
    var mode = props.mode, children = props.children, cx = props.classnames, className = props.className; props.style; var hasError = props.hasError, isRequired = props.isRequired, label = props.label, labelAlign = props.labelAlign, labelClassName = props.labelClassName, description = props.description;
    var errors = Array.isArray(props.errors)
        ? props.errors
        : props.errors
            ? [props.errors]
            : [];
    if (mode === 'horizontal') {
        var horizontal = props.horizontal || {
            leftFixed: true
        };
        return (_J$X_("div", { "data-role": "form-item", className: cx("Form-item Form-item--horizontal", className, (_a = {
                    'is-error': hasError
                },
                _a["is-required"] = isRequired,
                _a['Form-item--horizontal-justify'] = horizontal.justify,
                _a)) },
            label !== false ? (_J$X_("label", { className: cx("Form-label", (_b = {},
                    _b["Form-itemColumn--".concat(typeof horizontal.leftFixed === 'string'
                        ? horizontal.leftFixed
                        : 'normal')] = horizontal.leftFixed,
                    _b["Form-itemColumn--".concat(horizontal.left)] = !horizontal.leftFixed,
                    _b['Form-label--left'] = labelAlign === 'left',
                    _b), labelClassName) },
                _J$X_("span", null,
                    label,
                    isRequired && label ? (_J$X_("span", { className: cx("Form-star") }, "*")) : null))) : null,
            _J$X_("div", { className: cx("Form-value", (_c = {},
                    // [`Form-itemColumn--offset${getWidthRate(horizontal.offset)}`]: !label && label !== false,
                    _c["Form-itemColumn--".concat(horizontal.right)] = !horizontal.leftFixed &&
                        !!horizontal.right &&
                        horizontal.right !== 12 - horizontal.left,
                    _c)) },
                children,
                hasError && errors.length ? (_J$X_("ul", { className: cx("Form-feedback") }, errors.map(function (msg, key) { return (_J$X_("li", { key: key }, msg)); }))) : null,
                description ? (_J$X_("div", { className: cx("Form-description") }, description)) : null)));
    }
    return (_J$X_("div", { "data-role": "form-item", className: cx("Form-item Form-item--normal", className, (_d = {
                'is-error': hasError
            },
            _d["is-required"] = isRequired,
            _d)) },
        label ? (_J$X_("label", { className: cx("Form-label", labelClassName) },
            _J$X_("span", null,
                label,
                isRequired && label ? (_J$X_("span", { className: cx("Form-star") }, "*")) : null))) : null,
        children,
        hasError && errors.length ? (_J$X_("ul", { className: cx("Form-feedback") }, errors.map(function (msg, key) { return (_J$X_("li", { key: key }, msg)); }))) : null,
        description ? (_J$X_("div", { className: cx("Form-description") }, description)) : null));
}
var ThemedFormField = amisCore.themeable(amisCore.localeable(React__default["default"].memo(FormField)));
function Controller(props) {
    var render = props.render, name = props.name, shouldUnregister = props.shouldUnregister, defaultValue = props.defaultValue, control = props.control, wrap = props.wrap, rest = tslib.__rest(props, ["render", "name", "shouldUnregister", "defaultValue", "control", "wrap"]);
    var rules = React__default["default"].useMemo(function () {
        var rules = tslib.__assign({}, props.rules);
        if (props.isRequired) {
            rules.required = true;
        }
        return rules;
    }, [props.rules, props.isRequired]);
    return (_J$X_(reactHookForm.Controller, { name: name || '', rules: rules, shouldUnregister: shouldUnregister, defaultValue: defaultValue, control: control, render: function (methods) {
            var _a;
            return wrap === false ? (render(methods)) : (_J$X_(ThemedFormField, tslib.__assign({}, rest, { hasError: !!methods.fieldState.error, errors: (_a = methods.fieldState.error) === null || _a === void 0 ? void 0 : _a.message }), render(methods)));
        } }));
}

exports.Controller = Controller;
exports["default"] = ThemedFormField;
