function _extends() { _extends = Object.assign || 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); }

import React, { useContext, useMemo } from 'react';
import clsx from 'clsx';
import { addUnit, getSizeStyle } from '../utils';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var SpinIcon = function SpinIcon(_ref) {
  var bem = _ref.bem;
  return /*#__PURE__*/React.createElement(React.Fragment, null, Array(12).fill(null) // eslint-disable-next-line react/no-array-index-key
  .map(function (_, index) {
    return /*#__PURE__*/React.createElement("i", {
      key: index,
      className: clsx(bem('line', String(index + 1)))
    });
  }));
};

var CircularIcon = function CircularIcon(_ref2) {
  var bem = _ref2.bem;
  return /*#__PURE__*/React.createElement("svg", {
    className: clsx(bem('circular')),
    viewBox: "25 25 50 50"
  }, /*#__PURE__*/React.createElement("circle", {
    cx: "50",
    cy: "50",
    r: "20",
    fill: "none"
  }));
};

var BallIcon = function BallIcon(_ref3) {
  var bem = _ref3.bem;
  return /*#__PURE__*/React.createElement("div", {
    className: clsx(bem('ball'))
  }, /*#__PURE__*/React.createElement("div", null), /*#__PURE__*/React.createElement("div", null), /*#__PURE__*/React.createElement("div", null));
};

var Icon = function Icon(bem) {
  return {
    spinner: /*#__PURE__*/React.createElement(SpinIcon, {
      bem: bem
    }),
    circular: /*#__PURE__*/React.createElement(CircularIcon, {
      bem: bem
    }),
    ball: /*#__PURE__*/React.createElement(BallIcon, {
      bem: bem
    })
  };
};

var Loading = function Loading(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('loading', prefixCls),
      bem = _createNamespace[0];

  var className = props.className,
      type = props.type,
      vertical = props.vertical,
      color = props.color,
      size = props.size,
      textColor = props.textColor,
      children = props.children,
      textSize = props.textSize;
  var spinnerStyle = useMemo(function () {
    return _extends({
      color: color
    }, getSizeStyle(size));
  }, [color, size]);

  var renderText = function renderText() {
    if (children) {
      return /*#__PURE__*/React.createElement("span", {
        className: clsx(bem('text')),
        style: {
          fontSize: addUnit(textSize),
          color: textColor != null ? textColor : color
        }
      }, children);
    }

    return null;
  };

  return /*#__PURE__*/React.createElement("div", {
    className: clsx(className, bem([type, {
      vertical: vertical
    }])),
    style: props.style
  }, /*#__PURE__*/React.createElement("span", {
    className: clsx(bem('spinner', type)),
    style: spinnerStyle
  }, Icon(bem)[type]), renderText());
};

Loading.defaultProps = {
  type: 'circular'
};
export default Loading;