"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

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

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

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

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 _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 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 = (0, _react.useContext)(_ConfigProviderContext.default),
      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.default.createElement("div", {
        className: (0, _clsx.default)(bem('avatar', props.avatarShape)),
        style: (0, _utils.getSizeStyle)(props.avatarSize)
      });
    }

    return null;
  };

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

    return null;
  };

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

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

  if (!props.loading) return children;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(className, bem({
      animate: props.animate,
      round: props.round
    })),
    style: style
  }, renderAvatar(), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('content'))
  }, renderTitle(), renderRows()));
};

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