import { _ as _call_super } from "@swc/helpers/_/_call_super";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import React from "react";
import { Context } from "../form/context";
import Cell from "../cell";
import { ComponentDefaults } from "../../utils/typings";
import { isForwardRefComponent } from "../../utils/is-forward-ref-component";
import { SECRET } from "../form/useform";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    required: false,
    name: '',
    label: '',
    rules: [
        {
            required: false,
            message: ''
        }
    ],
    errorMessageAlign: 'left',
    validateTrigger: 'onChange',
    shouldUpdate: false,
    noStyle: false
});
var _React_Component;
export var FormItem = /*#__PURE__*/ function(_superClass) {
    "use strict";
    _inherits(FormItem, _superClass);
    function FormItem(props) {
        _class_call_check(this, FormItem);
        var _this;
        _this = _call_super(this, FormItem, [
            props
        ]), _define_property(_this, "cancelRegister", void 0), _define_property(_this, "componentRef", void 0), _define_property(_this, "eventOff", void 0), // children添加value属性和onChange事件
        _define_property(_this, "getControlled", function(children) {
            var _children_props;
            var _this_context = _this.context, setFieldsValue = _this_context.setFieldsValue, getFieldValue = _this_context.getFieldValue;
            var dispatch = _this.context.getInternal(SECRET).dispatch;
            var _this_props = _this.props, _this_props_name = _this_props.name, name = _this_props_name === void 0 ? '' : _this_props_name;
            if (children === null || children === void 0 ? void 0 : (_children_props = children.props) === null || _children_props === void 0 ? void 0 : _children_props.defaultValue) {
                console.warn('通过 initialValue 设置初始值');
            }
            var fieldValue = getFieldValue(name);
            var _obj;
            var controlled = _object_spread_props(_object_spread({}, children.props), (_obj = {}, _define_property(_obj, _this.props.valuePropName || 'value', fieldValue !== undefined ? fieldValue : _this.props.initialValue), _define_property(_obj, _this.props.trigger || 'onChange', function() {
                for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
                    args[_key] = arguments[_key];
                }
                // args [a, b]
                var originOnChange = children.props[_this.props.trigger || 'onChange'];
                if (originOnChange) {
                    originOnChange.apply(void 0, _to_consumable_array(args));
                }
                var _args = _sliced_to_array(args, 1), next = _args[0];
                if (_this.props.getValueFromEvent) {
                    var _this_props;
                    next = (_this_props = _this.props).getValueFromEvent.apply(_this_props, _to_consumable_array(args));
                }
                setFieldsValue(_define_property({}, name, next), false);
            }), _obj));
            var validateTrigger = _this.props.validateTrigger;
            var validateTriggers = [
                _this.props.trigger || 'onChange'
            ];
            if (validateTrigger) {
                validateTriggers = typeof validateTrigger === 'string' ? [
                    validateTrigger
                ] : _to_consumable_array(validateTrigger);
                validateTriggers.forEach(function(trigger) {
                    var originTrigger = controlled[trigger];
                    controlled[trigger] = function() {
                        for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
                            args[_key] = arguments[_key];
                        }
                        if (originTrigger) {
                            originTrigger.apply(void 0, _to_consumable_array(args));
                        }
                        if (_this.props.rules && _this.props.rules.length) {
                            dispatch({
                                name: _this.props.name
                            });
                        }
                    };
                });
            }
            if (isForwardRefComponent(children)) {
                controlled.ref = function(componentInstance) {
                    var originRef = children.ref;
                    if (originRef) {
                        if (typeof originRef === 'function') {
                            originRef(componentInstance);
                        }
                        if ('current' in originRef) {
                            originRef.current = componentInstance;
                        }
                    }
                    _this.componentRef = componentInstance;
                };
            }
            return controlled;
        }), _define_property(_this, "refresh", function() {
            _this.setState(function(param) {
                var resetCount = param.resetCount;
                return {
                    resetCount: resetCount + 1
                };
            });
        }), _define_property(_this, "onStoreChange", function(type) {
            if (type === 'reset') {
                _this.context.errors[_this.props.name] = [];
                _this.refresh();
            } else {
                _this.forceUpdate();
            }
        }), _define_property(_this, "renderLayout", function(childNode) {
            var _$_object_spread = _object_spread({}, defaultProps, _this.props), label = _$_object_spread.label, name = _$_object_spread.name, required = _$_object_spread.required, rules = _$_object_spread.rules, className = _$_object_spread.className, style = _$_object_spread.style, errorMessageAlign = _$_object_spread.errorMessageAlign, align = _$_object_spread.align;
            var requiredInRules = rules === null || rules === void 0 ? void 0 : rules.some(function(rule) {
                return rule.required;
            });
            var item = name ? _this.context.errors[name] : [];
            var starPosition = _this.context.starPosition;
            var renderStar = (required || requiredInRules) && /*#__PURE__*/ React.createElement("i", {
                className: "required"
            });
            var renderLabel = /*#__PURE__*/ React.createElement(React.Fragment, null, starPosition === 'left' ? renderStar : null, label, starPosition === 'right' ? renderStar : null);
            return /*#__PURE__*/ React.createElement(Cell, {
                className: "nut-form-item ".concat(className),
                style: style,
                align: align,
                onClick: function(e) {
                    return _this.props.onClick && _this.props.onClick(e, _this.componentRef);
                }
            }, label ? /*#__PURE__*/ React.createElement("div", {
                className: "nut-cell-title nut-form-item-label"
            }, renderLabel) : null, /*#__PURE__*/ React.createElement("div", {
                className: "nut-cell-value nut-form-item-body"
            }, /*#__PURE__*/ React.createElement("div", {
                className: "nut-form-item-body-slots"
            }, childNode), item && item.length > 0 && /*#__PURE__*/ React.createElement("div", {
                className: "nut-form-item-body-tips",
                style: {
                    textAlign: errorMessageAlign
                }
            }, item[0].message)));
        });
        _this.componentRef = /*#__PURE__*/ React.createRef();
        _this.state = {
            resetCount: 1
        };
        return _this;
    }
    _create_class(FormItem, [
        {
            key: "componentDidMount",
            value: function componentDidMount() {
                // Form设置initialValues时的处理
                var _this_context_getInternal = this.context.getInternal(SECRET), _this_context_getInternal_store = _this_context_getInternal.store, store = _this_context_getInternal_store === void 0 ? {} : _this_context_getInternal_store, setInitialValues = _this_context_getInternal.setInitialValues;
                if (this.props.initialValue && this.props.name && !Object.keys(store).includes(this.props.name)) {
                    setInitialValues(_object_spread_props(_object_spread({}, store), _define_property({}, this.props.name, this.props.initialValue)), true);
                }
                // 注册组件实例到FormStore
                var _this_context_getInternal1 = this.context.getInternal(SECRET), registerField = _this_context_getInternal1.registerField, registerUpdate = _this_context_getInternal1.registerUpdate;
                this.cancelRegister = registerField(this);
                // 这里需要增加事件监听，因为此实现属于依赖触发
                this.eventOff = registerUpdate(this, this.props.shouldUpdate);
            }
        },
        {
            key: "componentWillUnmount",
            value: function componentWillUnmount() {
                if (this.cancelRegister) {
                    this.cancelRegister();
                }
                if (this.eventOff) {
                    this.eventOff();
                }
            }
        },
        {
            key: "render",
            value: function render() {
                var children = this.props.children;
                var child = Array.isArray(children) ? children[0] : children;
                var returnChildNode;
                if (!this.props.shouldUpdate) {
                    returnChildNode = /*#__PURE__*/ React.cloneElement(child, this.getControlled(child));
                } else {
                    returnChildNode = child(this.context);
                }
                return /*#__PURE__*/ React.createElement(React.Fragment, {
                    key: this.state.resetCount
                }, this.props.noStyle ? returnChildNode : this.renderLayout(returnChildNode));
            }
        }
    ]);
    return FormItem;
}(_React_Component = React.Component);
_define_property(FormItem, "defaultProps", defaultProps);
_define_property(FormItem, "contextType", Context);
