"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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");

// 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 = "".concat(_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)("".concat(baseClassName, "__content"), blur && "".concat(baseClassName, "__content--blur"))
    }, children);
  }

  const {
    loaderType,
    loaderProps
  } = getLoader(loader);
  loaderProps.className = (0, _classnames.default)("".concat(baseClassName, "__indicator"), {
    ["".concat(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 && "".concat(baseClassName, "--overlay"), className)
  }, props), children, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)("".concat(baseClassName, "__container"), overlay && "".concat(baseClassName, "__overlay"), backdrop && "".concat(baseClassName, "__backdrop"))
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)("".concat(baseClassName, "__loader"), "".concat(baseClassName, "__loader--").concat(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: "".concat(baseClassName, "__text")
  }, text))));
};

var _default = ContentLoader;
exports.default = _default;