"use strict";

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

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _tslib() {
  const data = require("tslib");

  _tslib = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

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

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

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

const DEFAULT_ROW_WIDTH = '100%';
const DEFAULT_LAST_ROW_WIDTH = '60%';

const Skeleton = _a => {
  var {
    children,
    className,
    style
  } = _a,
      props = (0, _tslib().__rest)(_a, ["children", "className", "style"]);
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('skeleton', prefixCls);

  const getRowWidth = index => {
    const {
      rowWidth
    } = props;

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

  const getRowHeight = index => {
    const {
      rowHeight
    } = props;

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

    return rowHeight;
  };

  const renderAvatar = () => {
    if (props.avatar) {
      return (0, _jsxRuntime().jsx)("div", {
        className: (0, _clsx().default)(bem('avatar', props.avatarShape)),
        style: (0, _utils.getSizeStyle)(props.avatarSize)
      });
    }

    return null;
  };

  const renderTitle = () => {
    if (props.title) {
      const width = (0, _utils.addUnit)(props.titleWidth);
      const height = (0, _utils.addUnit)(getRowHeight(0));
      return (0, _jsxRuntime().jsx)("div", {
        className: (0, _clsx().default)(bem('title')),
        style: {
          width,
          height
        }
      });
    }

    return null;
  };

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

    return (0, _jsxRuntime().jsx)("div", {
      className: (0, _clsx().default)(bem('row')),
      style: {
        width,
        height
      }
    }, i);
  });

  if (!props.loading) return (0, _jsxRuntime().jsx)(_jsxRuntime().Fragment, {
    children: children
  });
  return (0, _jsxRuntime().jsxs)("div", Object.assign({
    className: (0, _clsx().default)(className, bem({
      animate: props.animate,
      round: props.round
    })),
    style: style
  }, {
    children: [renderAvatar(), (0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem('content'))
    }, {
      children: [renderTitle(), renderRows()]
    }))]
  }));
};

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