import { writable } from 'svelte/store';

/**
 * Toast notification system using DaisyUI
 */

export const toasts = writable([]);

let toastId = 0;

/**
 * Add a toast notification
 * @param {string} message - Toast message
 * @param {'info'|'success'|'warning'|'error'} type - Toast type
 * @param {number} duration - Duration in milliseconds
 */
export function addToast(message, type = 'info', duration = 3000) {
  const id = ++toastId;
  
  toasts.update((items) => [
    ...items,
    { id, message, type, duration },
  ]);
  
  // Auto-remove after duration
  if (duration > 0) {
    setTimeout(() => {
      removeToast(id);
    }, duration);
  }
  
  return id;
}

/**
 * Remove a toast by ID
 * @param {number} id - Toast ID
 */
export function removeToast(id) {
  toasts.update((items) => items.filter((item) => item.id !== id));
}

/**
 * Show info toast
 * @param {string} message
 * @param {number} duration
 */
export function info(message, duration) {
  return addToast(message, 'info', duration);
}

/**
 * Show success toast
 * @param {string} message
 * @param {number} duration
 */
export function success(message, duration) {
  return addToast(message, 'success', duration);
}

/**
 * Show warning toast
 * @param {string} message
 * @param {number} duration
 */
export function warning(message, duration) {
  return addToast(message, 'warning', duration);
}

/**
 * Show error toast
 * @param {string} message
 * @param {number} duration
 */
export function error(message, duration) {
  return addToast(message, 'error', duration);
}

export default {
  add: addToast,
  remove: removeToast,
  info,
  success,
  warning,
  error,
};
