import { m as mountComponent, u as usePopupState } from "./mount-component-2b0f7b23.js"; import { c as createNamespace, y as pick, z as makeStringProp, n as numericProp, a1 as makeNumberProp, G as unknownProp, I as Icon, k as isDef, e as extend, N as inBrowser, i as isObject } from "./index-487cde8c.js"; import { P as Popup } from "./index-eef3af38.js"; import { L as Loading } from "./index-217c49a0.js"; import { d as defineComponent, w as watch, o as onMounted, G as onUnmounted, c as createVNode, m as mergeProps, r as ref, g as getCurrentInstance } from "./index-5e4623ce.js"; let lockCount = 0; function lockClick(lock) { if (lock) { if (!lockCount) { document.body.classList.add("van-toast--unclickable"); } lockCount++; } else if (lockCount) { lockCount--; if (!lockCount) { document.body.classList.remove("van-toast--unclickable"); } } } const [name, bem] = createNamespace("toast"); const popupInheritProps = ["show", "overlay", "teleport", "transition", "overlayClass", "overlayStyle", "closeOnClickOverlay", "zIndex"]; const toastProps = { icon: String, show: Boolean, type: makeStringProp("text"), overlay: Boolean, message: numericProp, iconSize: numericProp, duration: makeNumberProp(2e3), position: makeStringProp("middle"), teleport: [String, Object], wordBreak: String, className: unknownProp, iconPrefix: String, transition: makeStringProp("van-fade"), loadingType: String, forbidClick: Boolean, overlayClass: unknownProp, overlayStyle: Object, closeOnClick: Boolean, closeOnClickOverlay: Boolean, zIndex: numericProp }; var stdin_default = defineComponent({ name, props: toastProps, emits: ["update:show"], setup(props, { emit, slots }) { let timer; let clickable = false; const toggleClickable = () => { const newValue = props.show && props.forbidClick; if (clickable !== newValue) { clickable = newValue; lockClick(clickable); } }; const updateShow = (show) => emit("update:show", show); const onClick = () => { if (props.closeOnClick) { updateShow(false); } }; const clearTimer = () => clearTimeout(timer); const renderIcon = () => { const { icon, type, iconSize, iconPrefix, loadingType } = props; const hasIcon = icon || type === "success" || type === "fail"; if (hasIcon) { return createVNode(Icon, { "name": icon || type, "size": iconSize, "class": bem("icon"), "classPrefix": iconPrefix }, null); } if (type === "loading") { return createVNode(Loading, { "class": bem("loading"), "size": iconSize, "type": loadingType }, null); } }; const renderMessage = () => { const { type, message } = props; if (slots.message) { return createVNode("div", { "class": bem("text") }, [slots.message()]); } if (isDef(message) && message !== "") { return type === "html" ? createVNode("div", { "key": 0, "class": bem("text"), "innerHTML": String(message) }, null) : createVNode("div", { "class": bem("text") }, [message]); } }; watch(() => [props.show, props.forbidClick], toggleClickable); watch(() => [props.show, props.type, props.message, props.duration], () => { clearTimer(); if (props.show && props.duration > 0) { timer = setTimeout(() => { updateShow(false); }, props.duration); } }); onMounted(toggleClickable); onUnmounted(toggleClickable); return () => createVNode(Popup, mergeProps({ "class": [bem([props.position, props.wordBreak === "normal" ? "break-normal" : props.wordBreak, { [props.type]: !props.icon }]), props.className], "lockScroll": false, "onClick": onClick, "onClosed": clearTimer, "onUpdate:show": updateShow }, pick(props, popupInheritProps)), { default: () => [renderIcon(), renderMessage()] }); } }); const defaultOptions = { icon: "", type: "text", message: "", className: "", overlay: false, onClose: void 0, onOpened: void 0, duration: 2e3, teleport: "body", iconSize: void 0, iconPrefix: void 0, position: "middle", transition: "van-fade", forbidClick: false, loadingType: void 0, overlayClass: "", overlayStyle: void 0, closeOnClick: false, closeOnClickOverlay: false }; let queue = []; let allowMultiple = false; let currentOptions = extend({}, defaultOptions); const defaultOptionsMap = /* @__PURE__ */ new Map(); function parseOptions(message) { if (isObject(message)) { return message; } return { message }; } function createInstance() { const { instance, unmount } = mountComponent({ setup() { const message = ref(""); const { open, state, close, toggle } = usePopupState(); const onClosed = () => { }; const render = () => { const attrs = { onClosed, "onUpdate:show": toggle }; return createVNode(stdin_default, mergeProps(state, attrs), null); }; watch(message, (val) => { state.message = val; }); getCurrentInstance().render = render; return { open, close, message }; } }); return instance; } function getInstance() { if (!queue.length || allowMultiple) { const instance = createInstance(); queue.push(instance); } return queue[queue.length - 1]; } function showToast(options = {}) { if (!inBrowser) { return {}; } const toast = getInstance(); const parsedOptions = parseOptions(options); toast.open(extend({}, currentOptions, defaultOptionsMap.get(parsedOptions.type || currentOptions.type), parsedOptions)); return toast; } export { stdin_default as a, showToast as s };