// packages/core/src/components/toast/toast-api.ts
import { type ToastType, toastActions } from "./toast-store";

interface ToastOptions {
	description?: string | undefined;
	duration?: number | undefined;
	id?: string | undefined;
}

/** Creates a toast with the given type. Returns the toast ID. */
function createToast(
	title: string,
	type: ToastType = "default",
	options?: ToastOptions,
): string {
	return toastActions.addToast({
		title,
		type,
		description: options?.description,
		duration: options?.duration,
		id: options?.id,
	});
}

/** Imperative toast API. Call toast("message") or toast.success/error/loading. */
export function toast(title: string, options?: ToastOptions): string {
	return createToast(title, "default", options);
}

/** Creates a success toast. */
toast.success = (title: string, options?: ToastOptions): string =>
	createToast(title, "success", options);

/** Creates an error toast. */
toast.error = (title: string, options?: ToastOptions): string =>
	createToast(title, "error", options);

/** Creates a loading toast. */
toast.loading = (title: string, options?: ToastOptions): string =>
	createToast(title, "loading", options);

/** Dismisses a toast by ID. */
toast.dismiss = (id: string): void => toastActions.dismissToast(id);

/** Clears all toasts. */
toast.clear = (): void => toastActions.clearToasts();

/** Updates an existing toast by ID. */
toast.update = (
	id: string,
	data: Partial<{ title: string; description: string; type: ToastType }>,
): void => toastActions.updateToast(id, data);
