"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _classnames = _interopRequireDefault(require("classnames"));
var _Spinner = _interopRequireDefault(require("../Spinner"));
var _IconsLoader = _interopRequireDefault(require("./IconsLoader"));
var _constants = require("../../constants");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. 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 = _ref => {
  let {
    baseClassName = `${_constants.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 (_react.Children.toArray(children).length && overlay) {
    children = /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(`${baseClassName}__content`, blur && `${baseClassName}__content--blur`),
      children: children
    });
  }
  const {
    loaderType,
    loaderProps
  } = getLoader(loader);
  loaderProps.className = (0, _classnames.default)(`${baseClassName}__indicator`, {
    [`${baseClassName}__indicator--sm`]: loaderType === 'spinner' && spinner && spinner.size === 'sm'
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)(baseClassName, !_react.Children.toArray(children).length && overlay && `${baseClassName}--overlay`, className),
    ...props,
    children: [children, /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(`${baseClassName}__container`, overlay && `${baseClassName}__overlay`, backdrop && `${baseClassName}__backdrop`),
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: (0, _classnames.default)(`${baseClassName}__loader`, `${baseClassName}__loader--${loaderType}`),
        children: [loaderType === 'spinner' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Spinner.default, {
          ...loaderProps
        }), loaderType === 'icons' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_IconsLoader.default, {
          ...loaderProps
        }), text && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: `${baseClassName}__text`,
          children: text
        })]
      })
    })]
  });
};
var _default = exports.default = ContentLoader;