import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { Children } from 'react';
import classNames from 'classnames';
import Spinner from '../Spinner';
import IconsLoader from './IconsLoader';
import { CLS_PREFIX } from '../../constants';

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(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 (Children.toArray(children).length && overlay) {
    children = /*#__PURE__*/React.createElement("div", {
      className: classNames("".concat(baseClassName, "__content"), blur && "".concat(baseClassName, "__content--blur"))
    }, children);
  }

  const {
    loaderType,
    loaderProps
  } = getLoader(loader);
  loaderProps.className = classNames("".concat(baseClassName, "__indicator"), {
    ["".concat(baseClassName, "__indicator--sm")]: loaderType === 'spinner' && spinner && spinner.size === 'sm'
  });
  return /*#__PURE__*/React.createElement("div", _extends({
    className: classNames(baseClassName, !Children.toArray(children).length && overlay && "".concat(baseClassName, "--overlay"), className)
  }, props), children, /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(baseClassName, "__container"), overlay && "".concat(baseClassName, "__overlay"), backdrop && "".concat(baseClassName, "__backdrop"))
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames("".concat(baseClassName, "__loader"), "".concat(baseClassName, "__loader--").concat(loaderType))
  }, loaderType === 'spinner' && /*#__PURE__*/React.createElement(Spinner, loaderProps), loaderType === 'icons' && /*#__PURE__*/React.createElement(IconsLoader, loaderProps), text && /*#__PURE__*/React.createElement("div", {
    className: "".concat(baseClassName, "__text")
  }, text))));
};

export default ContentLoader;