// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { Children } from 'react';
import classNames from 'classnames';
import Spinner from '../Spinner';
import IconsLoader from './IconsLoader';
import { CLS_PREFIX } from '../../constants';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
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 = _ref => {
  let {
    baseClassName = `${CLS_PREFIX}content-loader`,
    className,
    text,
    overlay = false,
    children,
    backdrop = false,
    blur = false,
    loading = true,
    loader = 'spinner',
    spinner,
    ...props
  } = _ref;
  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__*/_jsx("div", {
      className: classNames(`${baseClassName}__content`, blur && `${baseClassName}__content--blur`),
      children: children
    });
  }
  const {
    loaderType,
    loaderProps
  } = getLoader(loader);
  loaderProps.className = classNames(`${baseClassName}__indicator`, {
    [`${baseClassName}__indicator--sm`]: loaderType === 'spinner' && spinner && spinner.size === 'sm'
  });
  return /*#__PURE__*/_jsxs("div", {
    className: classNames(baseClassName, !Children.toArray(children).length && overlay && `${baseClassName}--overlay`, className),
    ...props,
    children: [children, /*#__PURE__*/_jsx("div", {
      className: classNames(`${baseClassName}__container`, overlay && `${baseClassName}__overlay`, backdrop && `${baseClassName}__backdrop`),
      children: /*#__PURE__*/_jsxs("div", {
        className: classNames(`${baseClassName}__loader`, `${baseClassName}__loader--${loaderType}`),
        children: [loaderType === 'spinner' && /*#__PURE__*/_jsx(Spinner, {
          ...loaderProps
        }), loaderType === 'icons' && /*#__PURE__*/_jsx(IconsLoader, {
          ...loaderProps
        }), text && /*#__PURE__*/_jsx("div", {
          className: `${baseClassName}__text`,
          children: text
        })]
      })
    })]
  });
};
export default ContentLoader;