"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const randomId = require("../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/utils/random-id/random-id.cjs");
const store = require("../../../../../@mantine_store@7.15.2_react@18.3.1/node_modules/@mantine/store/esm/store.cjs");
function getDistributedNotifications(data, defaultPosition, limit) {
  const queue = [];
  const notifications2 = [];
  const count = {};
  for (const item of data) {
    const position = item.position || defaultPosition;
    count[position] = count[position] || 0;
    count[position] += 1;
    if (count[position] <= limit) {
      notifications2.push(item);
    } else {
      queue.push(item);
    }
  }
  return { notifications: notifications2, queue };
}
const createNotificationsStore = () => store.createStore({
  notifications: [],
  queue: [],
  defaultPosition: "bottom-right",
  limit: 5
});
const notificationsStore = createNotificationsStore();
const useNotifications = (store$1 = notificationsStore) => store.useStore(store$1);
function updateNotificationsState(store2, update) {
  const state = store2.getState();
  const notifications2 = update([...state.notifications, ...state.queue]);
  const updated = getDistributedNotifications(notifications2, state.defaultPosition, state.limit);
  store2.setState({
    notifications: updated.notifications,
    queue: updated.queue,
    limit: state.limit,
    defaultPosition: state.defaultPosition
  });
}
function showNotification(notification, store2 = notificationsStore) {
  const id = notification.id || randomId.randomId();
  updateNotificationsState(store2, (notifications2) => {
    if (notification.id && notifications2.some((n) => n.id === notification.id)) {
      return notifications2;
    }
    return [...notifications2, { ...notification, id }];
  });
  return id;
}
function hideNotification(id, store2 = notificationsStore) {
  updateNotificationsState(
    store2,
    (notifications2) => notifications2.filter((notification) => {
      var _a;
      if (notification.id === id) {
        (_a = notification.onClose) == null ? void 0 : _a.call(notification, notification);
        return false;
      }
      return true;
    })
  );
  return id;
}
function updateNotification(notification, store2 = notificationsStore) {
  updateNotificationsState(
    store2,
    (notifications2) => notifications2.map((item) => {
      if (item.id === notification.id) {
        return { ...item, ...notification };
      }
      return item;
    })
  );
  return notification.id;
}
function cleanNotifications(store2 = notificationsStore) {
  updateNotificationsState(store2, () => []);
}
function cleanNotificationsQueue(store2 = notificationsStore) {
  updateNotificationsState(
    store2,
    (notifications2) => notifications2.slice(0, store2.getState().limit)
  );
}
const notifications = {
  show: showNotification,
  hide: hideNotification,
  update: updateNotification,
  clean: cleanNotifications,
  cleanQueue: cleanNotificationsQueue,
  updateState: updateNotificationsState
};
exports.cleanNotifications = cleanNotifications;
exports.cleanNotificationsQueue = cleanNotificationsQueue;
exports.createNotificationsStore = createNotificationsStore;
exports.hideNotification = hideNotification;
exports.notifications = notifications;
exports.notificationsStore = notificationsStore;
exports.showNotification = showNotification;
exports.updateNotification = updateNotification;
exports.updateNotificationsState = updateNotificationsState;
exports.useNotifications = useNotifications;
