"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _client = require("react-dom/client");
var _react = require("react");
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _Layer = _interopRequireDefault(require("../Layer"));
var _ToastView = _interopRequireDefault(require("./ToastView"));
var _PanelView = _interopRequireDefault(require("./PanelView"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2025. WebPros International GmbH. All rights reserved.

let toastId = 0;
const Toaster = /*#__PURE__*/(0, _react.forwardRef)(function Toaster(_ref, ref) {
  let {
    baseClassName = `${_constants.CLS_PREFIX}toaster`,
    className,
    toasts: externalToasts,
    maxToastsNumber,
    position = 'top-end',
    view = 'toast',
    onToastClose,
    onToastGroupClose,
    groupConfigs,
    isPanelCollapsed,
    onPanelCollapseStateChange,
    ...props
  } = _ref;
  const getExternalToasts = () => (externalToasts ?? []).slice(0, maxToastsNumber);
  const isControlled = Boolean(externalToasts);
  const [internalToasts, setToasts] = (0, _react.useState)(getExternalToasts);
  const toasts = isControlled ? getExternalToasts() : internalToasts;
  const remove = key => {
    setToasts(toasts => toasts.filter(toast => toast.key !== key));
  };
  (0, _react.useImperativeHandle)(ref, () => ({
    add(props) {
      const key = `toast-${toastId++}`;
      setToasts(toasts => [{
        key,
        ...props
      }, ...toasts].slice(0, maxToastsNumber));
      return key;
    },
    update(key, props) {
      setToasts(toasts => toasts.map(toast => toast.key === key ? {
        ...props,
        key
      } : toast));
      return key;
    },
    remove,
    clear() {
      setToasts([]);
    }
  }), [maxToastsNumber]);
  const handleToastClose = toastProps => {
    if (!isControlled) {
      remove(toastProps.key);
    }
    toastProps.onClose?.();
    onToastClose?.(toastProps.key);
  };
  const handleToastGroupClose = keys => {
    if (!isControlled) {
      setToasts(toasts => toasts.filter(toast => !keys.includes(toast.key)));
    }
    onToastGroupClose?.(keys);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layer.default, {
    level: _constants.Z_INDEX_TOASTER,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(baseClassName, className, `${baseClassName}--${view}-view`, {
        [`${baseClassName}--${position}`]: view === 'toast'
      }),
      ...props,
      children: view === 'toast' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_ToastView.default, {
        toasts: toasts,
        position: position,
        onToastClose: handleToastClose,
        baseClassName: `${baseClassName}-toast-view`
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_PanelView.default, {
        toasts: toasts,
        onToastClose: handleToastClose,
        onToastGroupClose: handleToastGroupClose,
        baseClassName: `${baseClassName}-panel-view`,
        groupConfigs: groupConfigs,
        isCollapsed: isPanelCollapsed,
        onCollapseStateChange: onPanelCollapseStateChange
      })
    })
  });
});
Toaster.create = function () {
  let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  let container = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.body;
  const containerElement = document.createElement('div');
  container.appendChild(containerElement);
  const toasterApi = {};
  const root = (0, _client.createRoot)(containerElement);
  root.render(/*#__PURE__*/(0, _jsxRuntime.jsx)(Toaster, {
    ref: ref => Object.assign(toasterApi, ref),
    ...props
  }));
  return toasterApi;
};

/**
 * `Toaster` is used for notifying about events happened in the system. For example, about results of performed operations.
 * @since 0.0.42
 */
var _default = exports.default = Toaster;