import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import Toast from "./toast.js";
import { renderImperatively } from "./renderImperatively.js";
import React from "react";
//#region src/components/pisellToast/squareToast/methods.tsx
let currentHandler = null;
let currentTimeout = null;
const defaultProps = {
	duration: 2e3,
	mask: false,
	icon: "info"
};
function mergeProps(a, b) {
	const filteredB = Object.keys(b).reduce((acc, key) => {
		if (b[key] !== void 0) acc[key] = b[key];
		return acc;
	}, {});
	return _objectSpread2(_objectSpread2({}, a), filteredB);
}
const show = (configOrContent, duration, afterClose) => {
	let props;
	if (typeof configOrContent === "string" || React.isValidElement(configOrContent)) props = mergeProps(defaultProps, _objectSpread2(_objectSpread2({ content: configOrContent }, duration !== void 0 && { duration }), afterClose !== void 0 && { afterClose }));
	else props = mergeProps(defaultProps, configOrContent || {});
	const element = /* @__PURE__ */ React.createElement(Toast, _objectSpread2(_objectSpread2({}, props), {}, { onCancel: () => {
		currentHandler = null;
	} }));
	if (currentHandler) {
		var _currentHandler$isRen;
		if ((_currentHandler$isRen = currentHandler.isRendered) === null || _currentHandler$isRen === void 0 ? void 0 : _currentHandler$isRen.call(currentHandler)) currentHandler.replace(element);
		else {
			currentHandler.close();
			currentHandler = renderImperatively(element);
		}
	} else currentHandler = renderImperatively(element);
	if (currentTimeout) window.clearTimeout(currentTimeout);
	if (props.duration !== 0) currentTimeout = window.setTimeout(() => {
		clear();
	}, props.duration);
	return { close: () => {
		if (currentHandler) {
			currentHandler.close();
			currentHandler = null;
		}
		if (currentTimeout) {
			window.clearTimeout(currentTimeout);
			currentTimeout = null;
		}
	} };
};
const clear = () => {
	if (currentHandler) {
		currentHandler.close();
		currentHandler = null;
	}
	if (currentTimeout) {
		window.clearTimeout(currentTimeout);
		currentTimeout = null;
	}
};
const success = (content, duration, afterClose) => {
	return show({
		content,
		icon: "success",
		duration,
		afterClose
	});
};
const info = (content, duration, afterClose) => {
	return show({
		content,
		icon: "info",
		duration,
		afterClose
	});
};
const loading = (content, duration, afterClose) => {
	return show({
		content,
		icon: "loading",
		duration,
		afterClose
	});
};
const fail = (content, duration, afterClose) => {
	return show({
		content,
		icon: "error",
		duration,
		afterClose
	});
};
const hide = () => {
	clear();
};
//#endregion
export { clear, fail, hide, info, loading, show, success };
