"use strict";

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

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

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

var _tableInfo = _interopRequireDefault(require("./components/tableInfo"));

var _headerInfo = _interopRequireDefault(require("./components/headerInfo"));

var _baseInfo = _interopRequireDefault(require("./components/baseInfo"));

var _InfoGroup = _interopRequireDefault(require("./components/InfoGroup"));

require("./index.scss");

var _utils = require("./utils");

var _utils2 = require("@teamix/utils");

var _hooks = require("@teamix/hooks");

var _ = require("..");

var _typing = require("./typing");

Object.keys(_typing).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _typing[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return _typing[key];
    }
  });
});
var _excluded = ["type", "url", "method", "formatResult", "formatParams", "params", "actionRef", "header", "onSuccess", "onError", "beforeRequest", "extendParams", "className", "style", "columns", "layout", "dataSource", "loading", "extra", "context"],
    _excluded2 = ["context"];

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

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

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; }

var ProInfo = function ProInfo(props) {
  var _ref;

  var _props$type = props.type,
      type = _props$type === void 0 ? 'base' : _props$type,
      url = props.url,
      _props$method = props.method,
      method = _props$method === void 0 ? 'post' : _props$method,
      formatResult = props.formatResult,
      formatParams = props.formatParams,
      params = props.params,
      propsActionRef = props.actionRef,
      _props$header = props.header,
      header = _props$header === void 0 ? {} : _props$header,
      onSuccess = props.onSuccess,
      onError = props.onError,
      beforeRequest = props.beforeRequest,
      extendParams = props.extendParams,
      className = props.className,
      style = props.style,
      columns = props.columns,
      layout = props.layout,
      dataSource = props.dataSource,
      userLoading = props.loading,
      extra = props.extra,
      infoContext = props.context,
      others = _objectWithoutProperties(props, _excluded);

  var _useContext = (0, _react.useContext)(_utils.ProInfoGroupContext),
      contextUrl = _useContext.url,
      contextMethod = _useContext.method,
      contextParams = _useContext.params,
      contextFormatResult = _useContext.formatResult,
      contextVisible = _useContext.visible,
      contextSize = _useContext.size,
      contextOnSuccess = _useContext.onSuccess,
      contextOnError = _useContext.onError,
      contextFormatParams = _useContext.formatParams,
      contextBeforeRequest = _useContext.beforeRequest,
      contextExtendParams = _useContext.extendParams,
      _useContext$context = _useContext.context,
      contextContext = _useContext$context === void 0 ? {} : _useContext$context; // 获取容器宽度


  var ref = (0, _react.useRef)();
  var size = contextVisible ? contextSize : (0, _hooks.useSize)(ref);

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      loading = _useState2[0],
      setLoading = _useState2[1]; // 请求数据


  var _useState3 = (0, _react.useState)(),
      _useState4 = _slicedToArray(_useState3, 2),
      data = _useState4[0],
      setData = _useState4[1];

  var requestDataKey = (contextMethod !== null && contextMethod !== void 0 ? contextMethod : method).toLowerCase() === 'get' ? 'params' : 'data';

  var requestConfig = _defineProperty({
    url: (_ref = contextUrl !== null && contextUrl !== void 0 ? contextUrl : url) !== null && _ref !== void 0 ? _ref : '',
    method: contextMethod !== null && contextMethod !== void 0 ? contextMethod : method,
    formatResult: contextFormatResult !== null && contextFormatResult !== void 0 ? contextFormatResult : formatResult,
    onSuccess: contextOnSuccess !== null && contextOnSuccess !== void 0 ? contextOnSuccess : onSuccess,
    onError: contextOnError !== null && contextOnError !== void 0 ? contextOnError : onError,
    formatParams: contextFormatParams !== null && contextFormatParams !== void 0 ? contextFormatParams : formatParams,
    beforeRequest: contextBeforeRequest !== null && contextBeforeRequest !== void 0 ? contextBeforeRequest : beforeRequest,
    extendParams: contextExtendParams !== null && contextExtendParams !== void 0 ? contextExtendParams : extendParams
  }, requestDataKey, contextParams !== null && contextParams !== void 0 ? contextParams : params);

  (0, _react.useEffect)(function () {
    if (url || contextUrl) {
      setLoading(false);
      request();
    } else {
      setLoading(false);
    }
  }, []);
  var context = (0, _react.useMemo)(function () {
    return _objectSpread(_objectSpread({}, infoContext), {}, {
      record: dataSource !== null && dataSource !== void 0 ? dataSource : data
    }, contextContext);
  }, [dataSource, data]); // 获取数据

  var request = function request() {
    setLoading(true);
    (0, _utils2.doCommonRequest)(requestConfig).then(function (resp) {
      setData(resp);
    }).finally(function () {
      setLoading(false);
    });
  };

  var actionRef = (0, _react.useRef)(); // 将 Actions 绑定到传入的 propsActionRef 中

  if (propsActionRef) {
    propsActionRef.current = actionRef.current;
  } // 绑定 Actions


  (0, _utils.useActionType)(actionRef, {
    refresh: function refresh() {
      request();
    }
  });

  var getExtra = function getExtra() {
    if (!extra) {
      return undefined;
    }

    if ( /*#__PURE__*/(0, _react.isValidElement)(extra)) {
      return extra;
    }

    var _extra$context = extra.context,
        actionContext = _extra$context === void 0 ? {} : _extra$context,
        others = _objectWithoutProperties(extra, _excluded2);

    return _objectSpread(_objectSpread({}, others), {}, {
      context: _objectSpread(_objectSpread({}, context), actionContext)
    });
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    ref: ref,
    style: style,
    className: (0, _utils2.cls)('teamix-pro-info', className, 'teamix-test-request'),
    "data-teamix-test-request": "url=".concat(url, "&formatResult=").concat(!!formatResult),
    "data-teamix-test-params": (0, _utils2.getTestRequestParams)(params),
    "data-teamix-test-data": (0, _utils2.getTestRequestData)(columns)
  }, /*#__PURE__*/_react.default.createElement(_.ProCard, _objectSpread(_objectSpread(_objectSpread({
    compacted: true,
    hoveredShadow: false,
    bordered: false,
    backgroundColor: "transparent"
  }, header), others), {}, {
    extra: getExtra()
  }), type === 'table' && /*#__PURE__*/_react.default.createElement(_tableInfo.default, {
    dataSource: dataSource,
    columns: columns,
    loading: userLoading || loading,
    result: data,
    actionRef: actionRef,
    layout: layout,
    context: context
  }), type === 'header' && /*#__PURE__*/_react.default.createElement(_headerInfo.default, {
    dataSource: dataSource,
    columns: columns,
    header: header,
    loading: userLoading || loading,
    result: data,
    actionRef: actionRef,
    layout: layout,
    size: size,
    context: context
  }), type === 'base' && /*#__PURE__*/_react.default.createElement(_baseInfo.default, {
    dataSource: dataSource,
    columns: columns,
    loading: userLoading || loading,
    result: data,
    actionRef: actionRef,
    layout: layout,
    size: size,
    context: context
  })));
};

ProInfo.Group = _InfoGroup.default;
ProInfo.ProInfoGroup = _InfoGroup.default;
var _default = ProInfo;
exports.default = _default;