import { r as requestCached } from "./request-cache-KCNHp_RJ.mjs";
import { a as getDb } from "./loader-CB6YHeul.mjs";
import { t as getWidgetComponents$1 } from "./components-B_sATNUc.mjs";

//#region src/widgets/index.ts
/**
* Get a widget area by name, with all its widgets.
*
* Single query with a left join rather than area-then-widgets so the
* common case costs one round-trip. An area with no widgets yields one
* row with null widget columns, which we skip when mapping.
*/
async function getWidgetArea(name) {
	return requestCached(`widget-area:${name}`, async () => {
		const rows = await (await getDb()).selectFrom("_emdash_widget_areas as a").leftJoin("_emdash_widgets as w", "w.area_id", "a.id").select([
			"a.id as a_id",
			"a.name as a_name",
			"a.label as a_label",
			"a.description as a_description",
			"w.id as w_id",
			"w.type as w_type",
			"w.title as w_title",
			"w.content as w_content",
			"w.menu_name as w_menu_name",
			"w.component_id as w_component_id",
			"w.component_props as w_component_props",
			"w.area_id as w_area_id",
			"w.sort_order as w_sort_order",
			"w.created_at as w_created_at"
		]).where("a.name", "=", name).orderBy("w.sort_order", "asc").execute();
		const first = rows[0];
		if (!first) return null;
		const widgets = [];
		for (const row of rows) {
			if (row.w_id === null) continue;
			const widgetRow = {
				id: row.w_id,
				type: row.w_type,
				title: row.w_title,
				content: row.w_content,
				menu_name: row.w_menu_name,
				component_id: row.w_component_id,
				component_props: row.w_component_props,
				area_id: row.w_area_id,
				sort_order: row.w_sort_order,
				created_at: row.w_created_at
			};
			widgets.push(rowToWidget(widgetRow));
		}
		return {
			id: first.a_id,
			name: first.a_name,
			label: first.a_label,
			description: first.a_description ?? void 0,
			widgets
		};
	});
}
/**
* Get all widget areas with their widgets
*/
async function getWidgetAreas() {
	const db = await getDb();
	const areaRows = await db.selectFrom("_emdash_widget_areas").selectAll().execute();
	const widgetRows = await db.selectFrom("_emdash_widgets").selectAll().$castTo().orderBy("sort_order", "asc").execute();
	const widgetsByArea = /* @__PURE__ */ new Map();
	for (const row of widgetRows) {
		if (!widgetsByArea.has(row.area_id)) widgetsByArea.set(row.area_id, []);
		widgetsByArea.get(row.area_id).push(rowToWidget(row));
	}
	return areaRows.map((areaRow) => ({
		id: areaRow.id,
		name: areaRow.name,
		label: areaRow.label,
		description: areaRow.description ?? void 0,
		widgets: widgetsByArea.get(areaRow.id) || []
	}));
}
/**
* Get available widget components (for admin UI)
*/
function getWidgetComponents() {
	return getWidgetComponents$1();
}
/**
* Convert a widget row to the API type
*/
function rowToWidget(row) {
	const widget = {
		id: row.id,
		type: row.type,
		title: row.title ?? void 0
	};
	if (row.type === "content" && row.content) try {
		widget.content = JSON.parse(row.content);
	} catch {}
	if (row.type === "menu" && row.menu_name) widget.menuName = row.menu_name;
	if (row.type === "component" && row.component_id) {
		widget.componentId = row.component_id;
		if (row.component_props) try {
			widget.componentProps = JSON.parse(row.component_props);
		} catch {}
	}
	return widget;
}

//#endregion
export { rowToWidget as i, getWidgetAreas as n, getWidgetComponents as r, getWidgetArea as t };
//# sourceMappingURL=widgets-hWcIicX8.mjs.map