"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

var _Spinner = _interopRequireDefault(require("../Spinner"));

var _IconsLoader = _interopRequireDefault(require("./IconsLoader"));

var _constants = require("../../constants");

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

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
const getLoader = loader => {
  let loaderType;
  let loaderProps = {};

  if (typeof loader === 'object') {
    ({
      type: loaderType,
      ...loaderProps
    } = loader);
  }

  if (typeof loader === 'string') {
    loaderType = loader;
  }

  return {
    loaderType,
    loaderProps
  };
};
/**
 * `ContentLoader` component is used as placeholder which indicates that the system is loading requested content.
 * @since 1.1.0
 */


const ContentLoader = ({
  baseClassName = `${_constants.CLS_PREFIX}content-loader`,
  className,
  text,
  overlay = false,
  children,
  backdrop = false,
  blur = false,
  loading = true,
  loader = 'spinner',
  spinner,
  ...props
}) => {
  if (!loading) {
    // workaround for FunctionComponent type which doesn't allow ReactNode as return type
    // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544
    const tmp = children;
    return tmp;
  }

  if (_react.Children.toArray(children).length && overlay) {
    children = /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)(`${baseClassName}__content`, blur && `${baseClassName}__content--blur`)
    }, children);
  }

  const {
    loaderType,
    loaderProps
  } = getLoader(loader);
  loaderProps.className = (0, _classnames.default)(`${baseClassName}__indicator`, {
    [`${baseClassName}__indicator--sm`]: loaderType === 'spinner' && spinner && spinner.size === 'sm'
  });
  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    className: (0, _classnames.default)(baseClassName, !_react.Children.toArray(children).length && overlay && `${baseClassName}--overlay`, className)
  }, props), children, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(`${baseClassName}__container`, overlay && `${baseClassName}__overlay`, backdrop && `${baseClassName}__backdrop`)
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(`${baseClassName}__loader`, `${baseClassName}__loader--${loaderType}`)
  }, loaderType === 'spinner' && /*#__PURE__*/_react.default.createElement(_Spinner.default, loaderProps), loaderType === 'icons' && /*#__PURE__*/_react.default.createElement(_IconsLoader.default, loaderProps), text && /*#__PURE__*/_react.default.createElement("div", {
    className: `${baseClassName}__text`
  }, text))));
};

var _default = ContentLoader;
exports.default = _default;