/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
"use client";
import { entries as entries$1 } from "../../../utils/dist/entries.js";
import { fromEntries } from "../../../utils/dist/fromEntries.js";
import { typeMap } from "./ToastContainer.js";
import { Toast } from "./Toast.js";
import { jsx } from "react/jsx-runtime";
import { merge, pick } from "lodash-es";
import { toast } from "react-toastify";
//#region src/components/client/Toast/createToast.tsx
const splitCreateToastArgs = (args) => {
	return {
		options: {
			toastId: args.id,
			...createToastOptionsToToastOptions(pick(args, [
				"closeOnClick",
				"pauseOnHover",
				"onClose",
				"containerId",
				"data",
				"position",
				"autoClose"
			]))
		},
		props: { ...pick(args, [
			"actionButton",
			"onClickActionButton",
			"startIcon",
			"closeIcon",
			"className"
		]) },
		data: { ...pick(args, [
			"variant",
			"message",
			"promise",
			"content"
		]) }
	};
};
const POSTION_MAP = {
	"top-start": "top-left",
	"top-center": "top-center",
	"top-end": "top-right",
	"bottom-start": "bottom-left",
	"bottom-center": "bottom-center",
	"bottom-end": "bottom-right"
};
const reverseTypeMap = fromEntries(entries$1(typeMap).map(([key, value]) => [value, key]));
function makeToastAPI(toastId, originalProps, originalData) {
	return {
		update(args) {
			const { data, options, props } = splitCreateToastArgs(args);
			const variant = data.variant ?? originalData.variant;
			if (variant === "custom") {
				const content = data.content ?? originalData.content;
				return toast.update(toastId, {
					...options,
					render: content
				});
			}
			const message = data.message ?? originalData.message;
			toast.update(toastId, {
				...options,
				type: reverseTypeMap[variant],
				render: ({ closeToast, toastProps: { toastId } }) => /* @__PURE__ */ jsx(Toast, {
					...merge({}, originalProps, props),
					variant,
					message,
					closeToast,
					id: String(toastId)
				})
			});
		},
		close() {
			toast.dismiss(toastId);
		}
	};
}
const createToastOptionsToToastOptions = (toastOptions) => {
	if (!toastOptions) return {};
	const { position, ...rest } = toastOptions;
	return {
		...rest,
		position: position ? POSTION_MAP[position] : void 0
	};
};
function createToast(args) {
	const { data, options, props } = splitCreateToastArgs(args);
	const { variant } = data;
	if (variant === "loading") return makeToastAPI(createLoadingToast(data.message, data.promise, props, options), props, data);
	if (variant === "custom") return makeToastAPI(toast(data.content, options), props, data);
	return makeToastAPI(toast(({ closeToast, toastProps: { toastId } }) => /* @__PURE__ */ jsx(Toast, {
		...props,
		variant,
		message: data.message,
		closeToast,
		id: String(toastId)
	}), {
		...options,
		type: reverseTypeMap[variant]
	}), props, data);
}
const createLoadingToast = (message, promise, props, options) => {
	const variant = "loading";
	const id = toast.loading(({ closeToast, toastProps: { toastId } }) => /* @__PURE__ */ jsx(Toast, {
		...props,
		variant,
		message,
		closeToast,
		id: String(toastId)
	}), {
		...options,
		type: reverseTypeMap[variant]
	});
	let finalVariant;
	let finalMessage;
	promise.then(({ result, message }) => {
		finalVariant = result === "success" ? "success" : "error";
		finalMessage = message;
	}).catch(() => {
		finalVariant = "error";
		finalMessage = "Unknown error occurred";
	}).finally(() => {
		toast.update(id, {
			...options,
			type: reverseTypeMap[finalVariant],
			render: ({ closeToast, toastProps: { toastId } }) => /* @__PURE__ */ jsx(Toast, {
				...props,
				variant: finalVariant,
				message: finalMessage,
				closeToast,
				id: String(toastId)
			})
		});
	});
	return id;
};
//#endregion
export { POSTION_MAP, createToast };
