import { I as __exportAll } from "./portal_tenant_content-DPLnrtOG.mjs";
import { n as useDataSourceRegistryConfig } from "./registry-context-BSJeDI2A.mjs";
import { i as useWidgetsApi, n as useWidgetPreviewContext, t as ErrorState } from "./error-state--Z2OlDFr.mjs";
import { i as getBorderColorField, l as getColorField, m as getPaddingField, n as borderWidthClasses, o as getBorderRadiusField, s as getBorderWidthField, t as borderColorClasses, u as getFontSizeField } from "./registries-kvRRUHcO.mjs";
import { useId, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { jsx, jsxs } from "react/jsx-runtime";
import { ChevronDown, Coins } from "lucide-react";
//#region ../widgets/src/hooks/use-points-ledger.preview.ts
const now = /* @__PURE__ */ new Date();
function daysAgo(days) {
	return (/* @__PURE__ */ new Date(now.getTime() - days * 864e5)).toISOString();
}
const PREVIEW_DATA = {
	balance: 25,
	entries: [
		{
			id: 1,
			description: "Purchase March 3rd",
			amount: 20,
			createdAt: daysAgo(3)
		},
		{
			id: 2,
			description: "Referral Bonus",
			amount: 50,
			createdAt: daysAgo(14)
		},
		{
			id: 3,
			description: "Points Redemption",
			amount: -75,
			createdAt: daysAgo(60)
		},
		{
			id: 4,
			description: "Welcome Reward",
			amount: 30,
			createdAt: daysAgo(90)
		}
	]
};
//#endregion
//#region ../widgets/src/hooks/use-points-ledger.ts
const POINTS_LEDGER_QUERY_KEY = "points-ledger";
function usePointsLedger() {
	const widgetsApi = useWidgetsApi();
	const { isPreview } = useWidgetPreviewContext();
	const registryConfig = useDataSourceRegistryConfig();
	const { baseUrl } = registryConfig;
	const customerId = registryConfig.variables?.customer_id;
	return useQuery({
		queryKey: [
			"portal-widget-use",
			POINTS_LEDGER_QUERY_KEY,
			isPreview ? "preview" : baseUrl,
			customerId ?? null
		],
		queryFn: ({ signal }) => widgetsApi.fetchPointsLedger(customerId ?? "", signal),
		enabled: !isPreview,
		...isPreview && { placeholderData: PREVIEW_DATA }
	});
}
//#endregion
//#region ../widgets/src/widgets/PointsWidget.tsx
var PointsWidget_exports = /* @__PURE__ */ __exportAll({
	PointsWidget: () => PointsWidget,
	pointsWidgetPropertySchema: () => pointsWidgetPropertySchema
});
const formatRelativeTime = (dateString) => {
	const now = /* @__PURE__ */ new Date();
	const date = new Date(dateString);
	const diffMs = now.getTime() - date.getTime();
	const diffDays = Math.floor(diffMs / 864e5);
	if (diffDays < 1) return "Today";
	if (diffDays === 1) return "1 day ago";
	if (diffDays < 7) return `${diffDays} days ago`;
	if (diffDays < 14) return "1 week ago";
	if (diffDays < 30) return `${Math.floor(diffDays / 7)} weeks ago`;
	if (diffDays < 60) return "1 month ago";
	return `${Math.floor(diffDays / 30)} months ago`;
};
const formatBalance = (balance) => {
	return balance.toLocaleString("en-US");
};
function PointsEntryRow({ entry, textColor, accentColor }) {
	const sign = entry.amount >= 0 ? "+" : "-";
	return /* @__PURE__ */ jsxs("div", {
		className: "flex w-full items-center gap-3",
		children: [/* @__PURE__ */ jsxs("div", {
			className: "min-w-0 flex-1",
			children: [/* @__PURE__ */ jsx("p", {
				className: `text-sm text-${textColor} truncate`,
				children: entry.description
			}), /* @__PURE__ */ jsx("p", {
				className: `text-[10px] text-${textColor} leading-tight opacity-70`,
				children: formatRelativeTime(entry.createdAt)
			})]
		}), /* @__PURE__ */ jsx("div", {
			className: `bg-${accentColor}/20 shrink-0 rounded-md px-1.5`,
			children: /* @__PURE__ */ jsxs("span", {
				className: `text-xs font-medium text-${accentColor} opacity-70`,
				children: [
					sign,
					Math.abs(entry.amount).toLocaleString("en-US"),
					"pts"
				]
			})
		})]
	});
}
function PointsWidget({ titleEnabled = true, title = "Points", titleFontSize = "md", titleColor = "foreground", balanceColor = "primary", historyEnabled = true, historyTitle = "History", background = {
	type: "solid",
	color: "background"
}, textColor = "foreground", accentColor = "primary", padding = 4, borderRadius = "md", borderWidth = "none", borderColor = "muted", className, ...props }) {
	const [historyOpen, setHistoryOpen] = useState(false);
	const historyPanelId = useId();
	const backgroundColor = background.color || "background";
	const backgroundImage = (background.resource?.image_url || background.resource?.imageUrl) && background.type === "image" ? `url(${background.resource.image_url || background.resource.imageUrl})` : "none";
	const { data, isLoading, isError } = usePointsLedger();
	return /* @__PURE__ */ jsx("div", {
		className: `@container overflow-hidden rounded-${borderRadius} bg-${backgroundColor} ${borderWidthClasses[borderWidth]} ${borderWidth !== "none" ? borderColorClasses[borderColor] : ""} ${className ?? ""}`,
		style: { backgroundImage },
		...props,
		children: /* @__PURE__ */ jsxs("div", {
			className: `p-${padding} flex flex-col gap-2.5`,
			children: [
				titleEnabled && /* @__PURE__ */ jsx("h2", {
					className: `text-${titleFontSize} font-header font-bold text-${titleColor}`,
					children: title
				}),
				!isLoading && data && /* @__PURE__ */ jsx("span", {
					className: `text-3xl font-semibold text-${balanceColor} font-header leading-snug`,
					children: formatBalance(data.balance)
				}),
				isLoading ? /* @__PURE__ */ jsx("div", {
					className: "flex min-h-[60px] items-center justify-center",
					children: /* @__PURE__ */ jsx("div", { className: "h-8 w-8 animate-spin rounded-full border-2 border-current border-t-transparent" })
				}) : isError ? /* @__PURE__ */ jsx(ErrorState, {}) : historyEnabled && (!data || data.entries.length === 0) ? /* @__PURE__ */ jsxs("div", {
					className: "flex min-h-[60px] flex-col items-center justify-center gap-2",
					children: [/* @__PURE__ */ jsx(Coins, { className: `size-10 text-${textColor} opacity-30` }), /* @__PURE__ */ jsx("p", {
						className: `text-sm font-semibold text-${textColor} opacity-50`,
						children: "No Points Activity"
					})]
				}) : historyEnabled && data && data.entries.length > 0 ? /* @__PURE__ */ jsxs("div", {
					className: "flex flex-col gap-2",
					children: [
						/* @__PURE__ */ jsxs("button", {
							type: "button",
							"aria-expanded": historyOpen,
							"aria-controls": historyPanelId,
							onClick: () => setHistoryOpen((prev) => !prev),
							className: `flex w-full items-center text-xs font-semibold text-${textColor} cursor-pointer`,
							children: [/* @__PURE__ */ jsx("span", {
								className: "flex-1 text-left",
								children: historyTitle
							}), /* @__PURE__ */ jsx(ChevronDown, { className: `size-4 transition-transform duration-200 ${historyOpen ? "rotate-180" : ""}` })]
						}),
						/* @__PURE__ */ jsx("div", { className: `bg-${textColor}/20 h-px w-full` }),
						/* @__PURE__ */ jsx("div", {
							id: historyPanelId,
							className: `flex flex-col gap-3 ${!historyOpen ? "hidden" : ""}`,
							children: data.entries.map((entry) => /* @__PURE__ */ jsx(PointsEntryRow, {
								entry,
								textColor,
								accentColor
							}, entry.id))
						})
					]
				}) : null
			]
		})
	});
}
const pointsWidgetPropertySchema = {
	widgetType: "PointsWidget",
	displayName: "Points",
	fields: [
		{
			key: "titleEnabled",
			label: "Show Title",
			type: "boolean",
			description: "Toggle title visibility",
			defaultValue: true,
			group: "Title"
		},
		{
			key: "title",
			label: "Title",
			type: "text",
			description: "Title text displayed above the balance",
			defaultValue: "Points",
			group: "Title",
			requiresKeyToBeTrue: "titleEnabled"
		},
		getFontSizeField({
			key: "titleFontSize",
			label: "Title Font Size",
			description: "Font size for the title",
			defaultValue: "md",
			group: "Title",
			requiresKeyToBeTrue: "titleEnabled"
		}),
		getColorField({
			key: "titleColor",
			label: "Title Color",
			description: "Color for the title",
			defaultValue: "foreground",
			group: "Title",
			requiresKeyToBeTrue: "titleEnabled"
		}),
		getColorField({
			key: "balanceColor",
			label: "Balance Color",
			description: "Color for the points balance number",
			defaultValue: "primary",
			group: "Balance"
		}),
		{
			key: "historyEnabled",
			label: "Show History",
			type: "boolean",
			description: "Show a collapsible history dropdown below the balance",
			defaultValue: true,
			group: "History"
		},
		{
			key: "historyTitle",
			label: "History Title",
			type: "text",
			description: "Title for the history dropdown",
			defaultValue: "History",
			group: "History",
			requiresKeyToBeTrue: "historyEnabled"
		},
		{
			type: "background",
			key: "background",
			label: "Background",
			description: "Background for the widget",
			defaultValue: "background",
			group: "Design"
		},
		getColorField({
			key: "textColor",
			label: "Text Color",
			description: "Default text color",
			defaultValue: "foreground",
			group: "Design"
		}),
		getColorField({
			key: "accentColor",
			label: "Accent Color",
			description: "Color for points badges and highlights",
			defaultValue: "primary",
			group: "Design"
		}),
		{
			key: "separator",
			type: "separator",
			label: "Separator",
			group: "Design"
		},
		getPaddingField({
			key: "padding",
			label: "Padding",
			description: "Widget padding",
			defaultValue: 4,
			group: "Design"
		}),
		getBorderRadiusField({
			key: "borderRadius",
			label: "Border Radius",
			description: "Widget border radius",
			defaultValue: "md",
			group: "Design"
		}),
		getBorderWidthField({
			key: "borderWidth",
			label: "Border Width",
			description: "Widget border width",
			defaultValue: "none",
			group: "Design"
		}),
		getBorderColorField({
			key: "borderColor",
			label: "Border Color",
			description: "Widget border color",
			defaultValue: "muted",
			group: "Design"
		})
	]
};
//#endregion
export { PointsWidget_exports as n, pointsWidgetPropertySchema as r, PointsWidget as t };

//# sourceMappingURL=PointsWidget-CRvG5LOV.mjs.map