import "antd/es/cascader/style";
import _Cascader from "antd/es/cascader";
import "antd/es/button/style";
import _Button from "antd/es/button";
import "antd/es/space/style";
import _Space from "antd/es/space";
import "antd/es/form/style";
import _Form from "antd/es/form";
import React, { useMemo } from 'react';
import Text from '../text';
import Input from '../input';
import classnames from 'classnames';
import { setFormData } from '@parallel-line/utils';
import { isUndefined, get, isFunction } from 'lodash-es';

var Form = function Form(props) {
  var onFinish = function onFinish() {
    var formData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var params = setFormData(formData);
    props.onFinish(params);
  };

  var prefixCls = "".concat(props.prefixCls, "-form");
  return /*#__PURE__*/React.createElement(_Form, {
    colon: false,
    form: props.form,
    onFinish: onFinish,
    className: classnames(prefixCls, props.className)
  }, props.children, /*#__PURE__*/React.createElement(_Form.Item, {
    className: "".concat(prefixCls, "-drawer-footer")
  }, /*#__PURE__*/React.createElement(_Space, null, /*#__PURE__*/React.createElement(_Button, {
    onClick: props.onCancel
  }, "\u53D6\u6D88"), /*#__PURE__*/React.createElement(_Button, {
    type: "primary",
    htmlType: "submit",
    loading: props.loading
  }, "\u786E\u5B9A"))));
};

Form.defaultProps = {
  prefixCls: 'pxx',
  onFinish: function onFinish() {},
  onCancel: function onCancel() {}
};

var Items = function Items(props) {
  var render = useMemo(function () {
    var _props$list;

    var componentMap = {
      Cascader: _Cascader,
      Input: Input
    };
    return (_props$list = props.list) === null || _props$list === void 0 ? void 0 : _props$list.map(function (t, key) {
      var Component = componentMap[t.type || 'Input'];
      var initialValue = isUndefined(t.initialValue) ? get(props.initialValues, t.name) : t.initialValue;
      var formItemProps = {
        initialValue: initialValue,
        name: t.name,
        rules: t.rules,
        label: t.label,
        hidden: t.hidden,
        noStyle: t.noStyle,
        key: t.name || key
      };
      if (t.required) formItemProps['required'] = t.required;
      return /*#__PURE__*/React.createElement(_Form.Item, Object.assign({}, formItemProps), isFunction(t.render) ? t.render(t, initialValue, props.initialValues) : /*#__PURE__*/React.createElement(Component, Object.assign({}, t.props)));
    });
  }, [props.list]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, render);
};

Items.defaultProps = {
  prefixCls: 'pxx',
  list: []
};

var Card = function Card(props) {
  var prefixCls = "".concat(props.prefixCls, "-form");
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-card")
  }, !!props.title && /*#__PURE__*/React.createElement(Text, {
    block: true,
    fs: 16,
    fw: 700,
    mb: 24,
    ml: 16
  }, props.title), /*#__PURE__*/React.createElement(Items, {
    list: props.list,
    initialValues: props.initialValues
  }));
};

Card.defaultProps = {
  prefixCls: 'pxx'
};
Form.Card = Card;
Form.Items = Items;
export default Form;