/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __rest, __assign } from 'tslib';
import React__default from 'react';
import { themeable, localeable } from 'amis-core';
import { Controller as Controller$1 } from 'react-hook-form';

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 (React__default.createElement("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 ? (React__default.createElement("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) },
                React__default.createElement("span", null,
                    label,
                    isRequired && label ? (React__default.createElement("span", { className: cx("Form-star") }, "*")) : null))) : null,
            React__default.createElement("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 ? (React__default.createElement("ul", { className: cx("Form-feedback") }, errors.map(function (msg, key) { return (React__default.createElement("li", { key: key }, msg)); }))) : null,
                description ? (React__default.createElement("div", { className: cx("Form-description") }, description)) : null)));
    }
    return (React__default.createElement("div", { "data-role": "form-item", className: cx("Form-item Form-item--normal", className, (_d = {
                'is-error': hasError
            },
            _d["is-required"] = isRequired,
            _d)) },
        label ? (React__default.createElement("label", { className: cx("Form-label", labelClassName) },
            React__default.createElement("span", null,
                label,
                isRequired && label ? (React__default.createElement("span", { className: cx("Form-star") }, "*")) : null))) : null,
        children,
        hasError && errors.length ? (React__default.createElement("ul", { className: cx("Form-feedback") }, errors.map(function (msg, key) { return (React__default.createElement("li", { key: key }, msg)); }))) : null,
        description ? (React__default.createElement("div", { className: cx("Form-description") }, description)) : null));
}
var ThemedFormField = themeable(localeable(React__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 = __rest(props, ["render", "name", "shouldUnregister", "defaultValue", "control", "wrap"]);
    var rules = React__default.useMemo(function () {
        var rules = __assign({}, props.rules);
        if (props.isRequired) {
            rules.required = true;
        }
        return rules;
    }, [props.rules, props.isRequired]);
    return (React__default.createElement(Controller$1, { name: name || '', rules: rules, shouldUnregister: shouldUnregister, defaultValue: defaultValue, control: control, render: function (methods) {
            var _a;
            return wrap === false ? (render(methods)) : (React__default.createElement(ThemedFormField, __assign({}, rest, { hasError: !!methods.fieldState.error, errors: (_a = methods.fieldState.error) === null || _a === void 0 ? void 0 : _a.message }), render(methods)));
        } }));
}

export { Controller, ThemedFormField as default };
