"use strict";

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

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

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

var _one = require("remax/one");

var _classnames = _interopRequireDefault(require("classnames"));

var _common = require("./../common");

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 _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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

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

var prefixCls = (0, _common.getPrefixCls)('skeleton');

var Skeleton = function Skeleton(props) {
  var _classNames7;

  var _props$title = props.title,
      title = _props$title === void 0 ? true : _props$title,
      titleColor = props.titleColor,
      backgroundColor = props.backgroundColor,
      active = props.active,
      _props$avatar = props.avatar,
      avatar = _props$avatar === void 0 ? false : _props$avatar,
      _props$loading = props.loading,
      loading = _props$loading === void 0 ? true : _props$loading,
      _props$image = props.image,
      image = _props$image === void 0 ? false : _props$image,
      _props$paragraph = props.paragraph,
      paragraph = _props$paragraph === void 0 ? {
    rows: 3,
    width: [80, 'auto', 200]
  } : _props$paragraph,
      _props$repetitions = props.repetitions,
      repetitions = _props$repetitions === void 0 ? 1 : _props$repetitions,
      space = props.space,
      customize = props.customize,
      fade = props.fade,
      style = props.style,
      children = props.children;

  var _React$useState = _react.default.useState(false),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      loadingEnd = _React$useState2[0],
      setLoadingEnd = _React$useState2[1];

  var _React$useState3 = _react.default.useState(false),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      renderStart = _React$useState4[0],
      setRenderStart = _React$useState4[1];

  (0, _react.useEffect)(function () {
    if (fade) {
      if (!loading) {
        setTimeout(function () {
          setLoadingEnd(true);
        }, 300);
      }

      if (loading) {
        setLoadingEnd(false);
        setRenderStart(false);
      }
    }
  }, [loading]);
  (0, _react.useEffect)(function () {
    if (fade) {
      if (loadingEnd) {
        setTimeout(function () {
          setRenderStart(true);
        }, 100);
      }
    }
  }, [loadingEnd]);
  var rows = (0, _react.useMemo)(function () {
    return paragraph.rows ? _toConsumableArray(new Array(paragraph.rows).keys()) : [];
  }, [paragraph]);
  var rowsWidth = (0, _react.useMemo)(function () {
    var _a;

    if (typeof paragraph.width === 'number') {
      return _toConsumableArray(new Array(rows.length).fill("".concat(paragraph.width, "px")));
    }

    if (typeof paragraph.width === 'string') {
      return _toConsumableArray(new Array(rows.length).fill(paragraph.width));
    }

    if (Array.isArray(paragraph.width)) {
      return (_a = paragraph.width) === null || _a === void 0 ? void 0 : _a.map(function (i) {
        if (typeof i === 'number') {
          return "".concat(i, "px");
        } else if (typeof i === 'string') {
          return i;
        } else {
          return 'auto';
        }
      });
    }
  }, [paragraph]);

  var renderImage = function renderImage(visible) {
    var _classNames;

    return visible ? /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-image"), true), _defineProperty(_classNames, "".concat(prefixCls, "-active"), active), _classNames))
    }) : null;
  };

  var renderAvatar = function renderAvatar(visible) {
    var _classNames2;

    return visible ? /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-avatar"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-active"), active), _classNames2))
    }) : null;
  };

  var renderTitle = function renderTitle(visible) {
    var _classNames3;

    return visible ? /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)((_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefixCls, "-title"), true), _defineProperty(_classNames3, "".concat(prefixCls, "-active"), active), _classNames3)),
      style: {
        backgroundColor: titleColor
      }
    }) : null;
  };

  var renderParagraph = function renderParagraph() {
    return rows.map(function (item, index) {
      var _classNames4;

      return /*#__PURE__*/_react.default.createElement(_one.View, {
        key: item,
        className: (0, _classnames.default)((_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefixCls, "-row"), true), _defineProperty(_classNames4, "".concat(prefixCls, "-active"), active), _classNames4)),
        style: {
          width: rowsWidth === null || rowsWidth === void 0 ? void 0 : rowsWidth[index]
        }
      });
    });
  };

  var renderSkeleton = function renderSkeleton() {
    var skeleton = _toConsumableArray(new Array(repetitions).keys());

    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, skeleton === null || skeleton === void 0 ? void 0 : skeleton.map(function (i, index) {
      return /*#__PURE__*/_react.default.createElement(_one.View, {
        key: i,
        className: "".concat(prefixCls, "-item"),
        style: {
          marginBottom: index !== skeleton.length - 1 ? space : 0
        }
      }, customize ? customize : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderImage(image), renderAvatar(avatar), renderTitle(title), renderParagraph()));
    }));
  };

  if (fade) {
    var _classNames6;

    return /*#__PURE__*/_react.default.createElement(_one.View, {
      className: prefixCls
    }, !loadingEnd ? /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)("".concat(prefixCls, "-placeholder"), _defineProperty({}, "".concat(prefixCls, "-hidden"), !loading)),
      style: Object.assign({
        backgroundColor: backgroundColor
      }, style)
    }, renderSkeleton()) : /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)((_classNames6 = {}, _defineProperty(_classNames6, "".concat(prefixCls, "-container"), true), _defineProperty(_classNames6, "".concat(prefixCls, "-container-active"), renderStart), _classNames6))
    }, children));
  }

  return /*#__PURE__*/_react.default.createElement(_one.View, {
    className: prefixCls
  }, loading ? /*#__PURE__*/_react.default.createElement(_one.View, {
    className: "".concat(prefixCls, "-placeholder"),
    style: Object.assign({
      backgroundColor: backgroundColor
    }, style)
  }, renderSkeleton()) : /*#__PURE__*/_react.default.createElement(_one.View, {
    className: (0, _classnames.default)((_classNames7 = {}, _defineProperty(_classNames7, "".concat(prefixCls, "-container"), true), _defineProperty(_classNames7, "".concat(prefixCls, "-container-active"), true), _classNames7))
  }, children));
};

var _default = Skeleton;
exports.default = _default;