import { useShellStore } from "../stores/shellStore.mjs";
import { WidgetCard } from "./WidgetCard.mjs";
import { PageLoading } from "../UI/feedback/PageLoading.mjs";
import { c } from "react-compiler-runtime";
import React, { Suspense, forwardRef, isValidElement, memo } from "react";
//#region src/dashboard/DashboardWidget.jsx
const isRenderableComponent = (Component) => typeof Component === "function" || Component && typeof Component === "object" && Component !== null && "$$typeof" in Component;
/**
* Widget implementation for dashboards that correctly handles lazy-loading.
*/
const DashboardWidget = forwardRef((props, ref) => {
	const $ = c(81);
	let children;
	let className;
	let deleteCard;
	let id;
	let paramValues;
	let setParams;
	let style;
	let widgetProps;
	let widgetStyle;
	let widgetType;
	if ($[0] !== props) {
		const { widgetType: t0, id: t1, deleteCard: t2, children: t3, params: t4, setParams: t5, ...rest } = props;
		widgetType = t0;
		id = t1;
		deleteCard = t2;
		children = t3;
		paramValues = t4;
		setParams = t5;
		({className, style, ...widgetProps} = rest);
		widgetStyle = { ...widgetProps.style || {} };
		if (style && typeof style === "object") {
			if (Object.prototype.hasOwnProperty.call(style, "height")) widgetStyle.height = style.height;
			if (Object.prototype.hasOwnProperty.call(style, "width")) widgetStyle.width = style.width;
		}
		$[0] = props;
		$[1] = children;
		$[2] = className;
		$[3] = deleteCard;
		$[4] = id;
		$[5] = paramValues;
		$[6] = setParams;
		$[7] = style;
		$[8] = widgetProps;
		$[9] = widgetStyle;
		$[10] = widgetType;
	} else {
		children = $[1];
		className = $[2];
		deleteCard = $[3];
		id = $[4];
		paramValues = $[5];
		setParams = $[6];
		style = $[7];
		widgetProps = $[8];
		widgetStyle = $[9];
		widgetType = $[10];
	}
	let t0;
	if ($[11] !== className) {
		t0 = className ? { className } : {};
		$[11] = className;
		$[12] = t0;
	} else t0 = $[12];
	let t1;
	if ($[13] !== widgetStyle) {
		t1 = Object.keys(widgetStyle).length ? { style: widgetStyle } : {};
		$[13] = widgetStyle;
		$[14] = t1;
	} else t1 = $[14];
	let t2;
	if ($[15] !== t0 || $[16] !== t1 || $[17] !== widgetProps) {
		t2 = {
			...widgetProps,
			...t0,
			...t1
		};
		$[15] = t0;
		$[16] = t1;
		$[17] = widgetProps;
		$[18] = t2;
	} else t2 = $[18];
	const widgetPropsWithClassName = t2;
	const { getWidget, isValidWidget } = useShellStore();
	let t3;
	if ($[19] !== deleteCard || $[20] !== id) {
		t3 = () => {
			deleteCard?.(id);
		};
		$[19] = deleteCard;
		$[20] = id;
		$[21] = t3;
	} else t3 = $[21];
	const onDelete = t3;
	let t4;
	if ($[22] !== getWidget || $[23] !== isValidWidget || $[24] !== widgetType) {
		bb0: {
			if (!isValidWidget?.(widgetType)) {
				let t5;
				if ($[26] === Symbol.for("react.memo_cache_sentinel")) {
					t5 = {};
					$[26] = t5;
				} else t5 = $[26];
				t4 = t5;
				break bb0;
			}
			const res = getWidget(widgetType);
			if (!res) {
				let t5;
				if ($[27] === Symbol.for("react.memo_cache_sentinel")) {
					t5 = {};
					$[27] = t5;
				} else t5 = $[27];
				t4 = t5;
				break bb0;
			}
			if (res.$$typeof === Symbol.for("react.lazy")) {
				t4 = {
					Widget: res,
					params: {}
				};
				break bb0;
			}
			if (res && typeof res === "object" && !isValidElement(res) && !res.then) {
				const widgetComp = res.Widget || res.default || null;
				const t5 = res.params || {};
				let t6;
				if ($[28] !== t5 || $[29] !== widgetComp) {
					t6 = {
						Widget: widgetComp,
						params: t5
					};
					$[28] = t5;
					$[29] = widgetComp;
					$[30] = t6;
				} else t6 = $[30];
				t4 = t6;
				break bb0;
			}
			console.warn(`getWidget returned an unexpected type for "${widgetType}":`, res);
			let t5;
			if ($[31] === Symbol.for("react.memo_cache_sentinel")) {
				t5 = {};
				$[31] = t5;
			} else t5 = $[31];
			t4 = t5;
		}
		$[22] = getWidget;
		$[23] = isValidWidget;
		$[24] = widgetType;
		$[25] = t4;
	} else t4 = $[25];
	const { Widget, params: paramDefs } = t4;
	let t5;
	bb1: {
		if (!paramDefs) {
			let t6;
			if ($[32] !== paramValues) {
				t6 = paramValues || {};
				$[32] = paramValues;
				$[33] = t6;
			} else t6 = $[33];
			t5 = t6;
			break bb1;
		}
		let t6;
		if ($[34] !== paramDefs) {
			t6 = Object.fromEntries(Object.entries(paramDefs).map(_temp));
			$[34] = paramDefs;
			$[35] = t6;
		} else t6 = $[35];
		const defaultParams = t6;
		let t7;
		if ($[36] !== defaultParams || $[37] !== paramValues) {
			t7 = {
				...defaultParams,
				...paramValues
			};
			$[36] = defaultParams;
			$[37] = paramValues;
			$[38] = t7;
		} else t7 = $[38];
		t5 = t7;
	}
	const params = t5;
	let t6;
	if ($[39] !== id || $[40] !== setParams) {
		t6 = (name, value) => setParams(id, { [name]: value });
		$[39] = id;
		$[40] = setParams;
		$[41] = t6;
	} else t6 = $[41];
	const setParam = t6;
	if (!isValidWidget || !getWidget) {
		let t7;
		if ($[42] !== widgetPropsWithClassName) {
			t7 = /* @__PURE__ */ React.createElement(WidgetCard, {
				...widgetPropsWithClassName,
				title: "Error"
			}, "isValidWidget or getWidget functions are not defined");
			$[42] = widgetPropsWithClassName;
			$[43] = t7;
		} else t7 = $[43];
		let t8;
		if ($[44] !== className || $[45] !== ref || $[46] !== style || $[47] !== t7) {
			t8 = /* @__PURE__ */ React.createElement("div", {
				ref,
				className,
				style
			}, t7);
			$[44] = className;
			$[45] = ref;
			$[46] = style;
			$[47] = t7;
			$[48] = t8;
		} else t8 = $[48];
		return t8;
	}
	if (!Widget) {
		let t7;
		if ($[49] !== widgetPropsWithClassName || $[50] !== widgetType) {
			t7 = /* @__PURE__ */ React.createElement(WidgetCard, {
				...widgetPropsWithClassName,
				title: "Error"
			}, "Unknown widget type “", widgetType, "”.");
			$[49] = widgetPropsWithClassName;
			$[50] = widgetType;
			$[51] = t7;
		} else t7 = $[51];
		let t8;
		if ($[52] !== className || $[53] !== ref || $[54] !== style || $[55] !== t7) {
			t8 = /* @__PURE__ */ React.createElement("div", {
				ref,
				className,
				style
			}, t7);
			$[52] = className;
			$[53] = ref;
			$[54] = style;
			$[55] = t7;
			$[56] = t8;
		} else t8 = $[56];
		return t8;
	}
	if (!isRenderableComponent(Widget)) {
		console.error(`DashboardWidget: widget "${widgetType}" resolved to a non-renderable component.`, Widget);
		let t7;
		if ($[57] !== widgetPropsWithClassName || $[58] !== widgetType) {
			t7 = /* @__PURE__ */ React.createElement(WidgetCard, {
				...widgetPropsWithClassName,
				title: "Error"
			}, "Unable to render widget type “", widgetType, "”.");
			$[57] = widgetPropsWithClassName;
			$[58] = widgetType;
			$[59] = t7;
		} else t7 = $[59];
		let t8;
		if ($[60] !== className || $[61] !== ref || $[62] !== style || $[63] !== t7) {
			t8 = /* @__PURE__ */ React.createElement("div", {
				ref,
				className,
				style
			}, t7);
			$[60] = className;
			$[61] = ref;
			$[62] = style;
			$[63] = t7;
			$[64] = t8;
		} else t8 = $[64];
		return t8;
	}
	let t7;
	if ($[65] !== Widget || $[66] !== children || $[67] !== id || $[68] !== onDelete || $[69] !== params || $[70] !== setParam || $[71] !== widgetPropsWithClassName) {
		t7 = /* @__PURE__ */ React.createElement(Widget, {
			...widgetPropsWithClassName,
			id,
			params,
			setParam,
			onDelete
		}, children);
		$[65] = Widget;
		$[66] = children;
		$[67] = id;
		$[68] = onDelete;
		$[69] = params;
		$[70] = setParam;
		$[71] = widgetPropsWithClassName;
		$[72] = t7;
	} else t7 = $[72];
	const widgetElement = t7;
	let t8;
	if ($[73] === Symbol.for("react.memo_cache_sentinel")) {
		t8 = /* @__PURE__ */ React.createElement(PageLoading, null);
		$[73] = t8;
	} else t8 = $[73];
	let t9;
	if ($[74] !== widgetElement) {
		t9 = /* @__PURE__ */ React.createElement(Suspense, { fallback: t8 }, widgetElement);
		$[74] = widgetElement;
		$[75] = t9;
	} else t9 = $[75];
	let t10;
	if ($[76] !== className || $[77] !== ref || $[78] !== style || $[79] !== t9) {
		t10 = /* @__PURE__ */ React.createElement("div", {
			ref,
			className,
			style
		}, t9);
		$[76] = className;
		$[77] = ref;
		$[78] = style;
		$[79] = t9;
		$[80] = t10;
	} else t10 = $[80];
	return t10;
});
DashboardWidget.displayName = "DashboardWidget";
const MemoizedDashboardWidget = memo(DashboardWidget);
MemoizedDashboardWidget.displayName = "DashboardWidget";
function _temp(t0) {
	const [key, t1] = t0;
	const { defaultValue } = t1;
	return [key, defaultValue];
}
//#endregion
export { MemoizedDashboardWidget as default };

//# sourceMappingURL=DashboardWidget.mjs.map