"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("antd/es/cascader/style");

var _cascader = _interopRequireDefault(require("antd/es/cascader"));

require("antd/es/button/style");

var _button = _interopRequireDefault(require("antd/es/button"));

require("antd/es/space/style");

var _space = _interopRequireDefault(require("antd/es/space"));

require("antd/es/form/style");

var _form = _interopRequireDefault(require("antd/es/form"));

var _react = _interopRequireWildcard(require("react"));

var _text = _interopRequireDefault(require("../text"));

var _input = _interopRequireDefault(require("../input"));

var _classnames = _interopRequireDefault(require("classnames"));

var _utils = require("@parallel-line/utils");

var _lodashEs = require("lodash-es");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

  var prefixCls = "".concat(props.prefixCls, "-form");
  return /*#__PURE__*/_react.default.createElement(_form.default, {
    colon: false,
    form: props.form,
    onFinish: onFinish,
    className: (0, _classnames.default)(prefixCls, props.className)
  }, props.children, /*#__PURE__*/_react.default.createElement(_form.default.Item, {
    className: "".concat(prefixCls, "-drawer-footer")
  }, /*#__PURE__*/_react.default.createElement(_space.default, null, /*#__PURE__*/_react.default.createElement(_button.default, {
    onClick: props.onCancel
  }, "\u53D6\u6D88"), /*#__PURE__*/_react.default.createElement(_button.default, {
    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 = (0, _react.useMemo)(function () {
    var _props$list;

    var componentMap = {
      Cascader: _cascader.default,
      Input: _input.default
    };
    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 = (0, _lodashEs.isUndefined)(t.initialValue) ? (0, _lodashEs.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.default.createElement(_form.default.Item, Object.assign({}, formItemProps), (0, _lodashEs.isFunction)(t.render) ? t.render(t, initialValue, props.initialValues) : /*#__PURE__*/_react.default.createElement(Component, Object.assign({}, t.props)));
    });
  }, [props.list]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, render);
};

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

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

Card.defaultProps = {
  prefixCls: 'pxx'
};
Form.Card = Card;
Form.Items = Items;
var _default = Form;
exports.default = _default;