import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _extends from "@babel/runtime/helpers/extends";
var _excluded = ["name", "required", "labelFor"],
    _excluded2 = ["title"];
import React from 'react'; // @ts-ignore

import bgDefault from './assets/r2g7rm.jpg';
import DocumentTitle from '@uiw-admin/document-title';
import { Form, Row, Col, Button } from 'uiw';
import useSWR from 'swr';
import { request } from '@uiw-admin/utils';
import './styles/index.css';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export default (props => {
  var {
    align = 'center',
    classNameWarp = '',
    styleWarp = {},
    classNameBody = '',
    styleBody = {},
    footer,
    bg = bgDefault,
    logo = bgDefault,
    children,
    projectName = 'UIW Admin',
    onSuccess: _onSuccess = () => null,
    btnProps = {},
    api,
    onBefore,
    requestConfig,
    buttons,
    saveField,
    fields,
    isDefaultFields = true,
    defaultFieldsConfig,
    styleContainer = {}
  } = props;
  var {
    userName = 'username',
    passWord = 'password'
  } = saveField || {};
  var userNameLabel = ((defaultFieldsConfig || {})['userName'] || {})['label'] || '账号';
  var passWordLabel = ((defaultFieldsConfig || {})['passWord'] || {})['label'] || '密码';
  userName = ((defaultFieldsConfig || {})['userName'] || {})['name'] || userName;
  passWord = ((defaultFieldsConfig || {})['passWord'] || {})['name'] || passWord;
  var [store, setStore] = React.useState();
  var {
    isValidating
  } = useSWR(store ? [api, _extends({
    method: 'POST',
    body: store
  }, requestConfig || {})] : null, request, {
    revalidateOnFocus: false,
    onSuccess: resp => _onSuccess(resp, store)
  });
  var defaultFields = [_extends({
    name: userName,
    label: "" + userNameLabel,
    labelFor: userName,
    required: true,
    children: /*#__PURE__*/_jsx("input", {
      type: "text",
      disabled: !!isValidating,
      id: userName,
      placeholder: "\u8BF7\u8F93\u5165" + userNameLabel,
      className: "form-field"
    })
  }, (defaultFieldsConfig == null ? void 0 : defaultFieldsConfig.userName) || {}), _extends({
    name: passWord,
    label: "" + passWordLabel,
    labelFor: passWord,
    required: true,
    children: /*#__PURE__*/_jsx("input", {
      disabled: !!isValidating,
      id: passWord,
      type: "password",
      placeholder: "\u8BF7\u8F93\u5165" + passWordLabel,
      className: "form-field"
    })
  }, (defaultFieldsConfig == null ? void 0 : defaultFieldsConfig.passWord) || {})];
  var fieldArr = defaultFields;

  if (fields && Array.isArray(fields)) {
    if (isDefaultFields) {
      fieldArr = defaultFields.concat(fields);
    } else {
      fieldArr = fields;
    }
  }

  return /*#__PURE__*/_jsxs("div", {
    className: "uiw-loayout-login",
    style: _extends({
      background: "url(" + bg + ")"
    }, styleContainer),
    children: [/*#__PURE__*/_jsx(DocumentTitle, {
      title: projectName
    }), /*#__PURE__*/_jsx("div", {
      style: styleWarp,
      className: "uiw-loayout-login-warp " + classNameWarp + " uiw-loayout-login-warp-" + align,
      children: /*#__PURE__*/_jsxs("div", {
        className: "uiw-loayout-login-body " + classNameBody,
        style: styleBody,
        children: [logo && /*#__PURE__*/_jsx("img", {
          className: "logo",
          src: logo
        }), projectName && /*#__PURE__*/_jsx("span", {
          className: "uiw-title",
          children: projectName
        }), children ? children : /*#__PURE__*/_jsx(Form, {
          resetOnSubmit: false,
          onSubmit: _ref => {
            var {
              current
            } = _ref;
            var errorObj = {};
            fieldArr.forEach(item => {
              if (item.verification && typeof item.verification === 'function') {
                var result = item.verification(current[item.name], current);

                if (typeof result === 'string' && result) {
                  errorObj[item.name] = result;
                }
              } else if (item.required && !current[userName]) {
                errorObj[item.name] = item.label + "\u4E0D\u80FD\u4E3A\u7A7A";
              }
            });

            if (Object.keys(errorObj).length > 0) {
              var err = new Error();
              err.filed = errorObj;
              throw err;
            } else {
              if (typeof onBefore === 'function') {
                var result = onBefore(current);

                if (typeof result === 'object') {
                  setStore(_extends({}, current, result || {}, {
                    swr_Rest_Time: new Date().getTime()
                  }));
                  return;
                }

                if (!result) {
                  return;
                }
              }

              setStore(_extends({}, current, {
                swr_Rest_Time: new Date().getTime()
              }));
            }
          },
          onSubmitError: error => {
            if (error.filed) {
              return _extends({}, error.filed);
            }

            return null;
          },
          fields: fieldArr.reduce((pre, current) => {
            if (!current || current && !current.name) {
              return _extends({}, pre);
            }

            var {
              name,
              labelFor
            } = current,
                rest = _objectWithoutPropertiesLoose(current, _excluded);

            return _extends({}, pre, {
              [name]: _extends({
                labelFor: name || labelFor
              }, rest)
            });
          }, {}),
          children: _ref2 => {
            var {
              fields,
              canSubmit
            } = _ref2;
            return /*#__PURE__*/_jsxs("div", {
              children: [fieldArr.map((item, index) => {
                return /*#__PURE__*/_jsx(Row, {
                  children: /*#__PURE__*/_jsx(Col, {
                    children: fields[item.name]
                  })
                }, index);
              }), /*#__PURE__*/_jsx(Row, {
                children: buttons && buttons.length > 0 ? buttons.map((item, idx) => {
                  var {
                    title
                  } = item,
                      rest = _objectWithoutPropertiesLoose(item, _excluded2);

                  return /*#__PURE__*/_jsx(Button, _extends({
                    loading: !!isValidating && item.htmlType === 'submit',
                    disabled: !canSubmit(),
                    className: "btns",
                    type: "danger"
                  }, rest, {
                    children: title
                  }), idx);
                }) : /*#__PURE__*/_jsx(Button, _extends({
                  disabled: !canSubmit(),
                  loading: !!isValidating,
                  className: "btns",
                  block: true,
                  style: {
                    marginTop: 20
                  },
                  htmlType: "submit",
                  type: "dark"
                }, btnProps, {
                  children: "\u767B\u5F55"
                }))
              })]
            });
          }
        })]
      })
    }), footer ? footer : /*#__PURE__*/_jsx("div", {
      className: "copyright-footer",
      children: "\u7248\u6743\u6240\u6709 copyright \xA9 2022 uiw admin"
    })]
  });
});
//# sourceMappingURL=index.js.map