var _excluded = ["children", "className", "style"];

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

import React, { useContext } from 'react';
import cls from 'clsx';
import { addUnit, getSizeStyle } from '../utils';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
var DEFAULT_ROW_WIDTH = '100%';
var DEFAULT_LAST_ROW_WIDTH = '60%';

var Skeleton = function Skeleton(_ref) {
  var children = _ref.children,
      className = _ref.className,
      style = _ref.style,
      props = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

  var getRowWidth = function getRowWidth(index) {
    var rowWidth = props.rowWidth;

    if (rowWidth === DEFAULT_ROW_WIDTH && index === +props.row - 1 && index !== 0) {
      return DEFAULT_LAST_ROW_WIDTH;
    }

    if (Array.isArray(rowWidth)) {
      return rowWidth[index];
    }

    return rowWidth;
  };

  var getRowHeight = function getRowHeight(index) {
    var rowHeight = props.rowHeight;

    if (Array.isArray(rowHeight)) {
      return rowHeight[index];
    }

    return rowHeight;
  };

  var renderAvatar = function renderAvatar() {
    if (props.avatar) {
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('avatar', props.avatarShape)),
        style: getSizeStyle(props.avatarSize)
      });
    }

    return null;
  };

  var renderTitle = function renderTitle() {
    if (props.title) {
      var width = addUnit(props.titleWidth);
      var height = addUnit(getRowHeight(0));
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('title')),
        style: {
          width: width,
          height: height
        }
      });
    }

    return null;
  };

  var renderRows = function renderRows() {
    return Array(props.row).fill('').map(function (_, i) {
      var width = addUnit(getRowWidth(i));
      var height = addUnit(getRowHeight(i)); // eslint-disable-next-line react/no-array-index-key

      return /*#__PURE__*/React.createElement("div", {
        key: i,
        className: cls(bem('row')),
        style: {
          width: width,
          height: height
        }
      });
    });
  };

  if (!props.loading) return /*#__PURE__*/React.createElement(React.Fragment, null, children);
  return /*#__PURE__*/React.createElement("div", {
    className: cls(className, bem({
      animate: props.animate,
      round: props.round
    })),
    style: style
  }, renderAvatar(), /*#__PURE__*/React.createElement("div", {
    className: cls(bem('content'))
  }, renderTitle(), renderRows()));
};

Skeleton.defaultProps = {
  loading: true,
  animate: true,
  round: true,
  row: 3,
  avatarShape: 'round',
  rowWidth: DEFAULT_ROW_WIDTH
};
export default Skeleton;