var _excluded = ["className", "onOpen", "onCancel", "onOk", "dialogProps", "formProps", "noContainer", "formItemLayout", "onSuccess", "request", "onRequestFailed", "onChange", "onValidateFailed"],
  _excluded2 = ["dataSource", "onFinish", "title"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/* eslint-disable no-unused-expressions */
import React, { useState, useCallback, useEffect, useMemo } from 'react';
import { Modal } from 'antd';
import classNames from 'classnames';
// import net from '@/services';
import Form from "../../compose-form";
import Button from "../default";
import "./index.less";
// const defaultRequestFunction = (...args: any[]) => net.request(...args);
export default function DialogForm(_ref) {
  var _ref$className = _ref.className,
    className = _ref$className === void 0 ? null : _ref$className,
    onOpen = _ref.onOpen,
    onCancel = _ref.onCancel,
    onOk = _ref.onOk,
    dialogProps = _ref.dialogProps,
    formProps = _ref.formProps,
    noContainer = _ref.noContainer,
    _ref$formItemLayout = _ref.formItemLayout,
    formItemLayout = _ref$formItemLayout === void 0 ? {
      labelCol: {
        xs: {
          span: 19
        },
        sm: {
          span: 5
        }
      },
      wrapperCol: {
        xs: {
          span: 19
        },
        sm: {
          span: 19
        }
      }
    } : _ref$formItemLayout,
    onSuccess = _ref.onSuccess,
    request = _ref.request,
    onRequestFailed = _ref.onRequestFailed,
    onChange = _ref.onChange,
    onValidateFailed = _ref.onValidateFailed,
    others = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    actions = _useState2[0],
    setActions = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    dlgVis = _useState4[0],
    setDlgVis = _useState4[1];
  var dataSource = formProps.dataSource,
    onFinish = formProps.onFinish,
    title = formProps.title,
    otherFormProps = _objectWithoutProperties(formProps, _excluded2);
  var hideDlg = useCallback(function () {
    onCancel && onCancel();
    setDlgVis(false);
  }, [onCancel]);
  var showDlg = useCallback(function () {
    setDlgVis(true);
    onOpen && onOpen(others);
  }, [onOpen]);
  var handleFinish = function handleFinish(formValue, currentTarget, response) {
    onSuccess && onSuccess(formValue, currentTarget, response);
    onFinish && onFinish(formValue);
    hideDlg();
  };
  useEffect(function () {
    setActions([{
      uiType: 'button',
      props: {
        children: '取消',
        onClick: hideDlg
      }
    }, {
      uiType: 'submit',
      props: {
        children: '确定',
        type: 'primary'
      }
    }]);
  }, [hideDlg]);
  var ButtonDom = useMemo(function () {
    if (others.hasOwnProperty('to') && others.to === 'null') {
      return null;
    }
    return /*#__PURE__*/React.createElement(Button, _extends({}, others, {
      onClick: showDlg
    }));
  }, [others]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, ButtonDom, dlgVis ? /*#__PURE__*/React.createElement(Form, _extends({}, formItemLayout, otherFormProps, {
    initialValues: formProps.value || formProps.initialValues,
    name: "dialogForm",
    onFinish: handleFinish,
    controls: dataSource,
    actions: actions,
    request: request,
    noContainer: true,
    toastGlobalError: true,
    renderItemList: function renderItemList(controlItems, actionFormItems) {
      return /*#__PURE__*/React.createElement(Modal, _extends({
        visible: dlgVis,
        onCancel: hideDlg,
        footer: actionFormItems
      }, dialogProps, {
        className: classNames('cf-button-form-modal', className, dialogProps === null || dialogProps === void 0 ? void 0 : dialogProps.className)
      }), controlItems);
    }
  })) : null);
}