const require_chunk = require("./chunk-9hOWP6kD.cjs");
const require_registry_context = require("./registry-context-DJ5xiVnt.cjs");
const require_error_state = require("./error-state-DJq7C-23.cjs");
const require_registries = require("./registries-CpUM406S.cjs");
const require_preview_context = require("./preview-context-BWCl-xyj.cjs");
let react = require("react");
let _tanstack_react_query = require("@tanstack/react-query");
let react_jsx_runtime = require("react/jsx-runtime");
let lucide_react = require("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,
			amount: 20,
			createdAt: daysAgo(3),
			transactionType: "new_order",
			hasSource: true
		},
		{
			id: 2,
			amount: 50,
			createdAt: daysAgo(14),
			transactionType: "referral_bonus",
			hasSource: true
		},
		{
			id: 3,
			amount: -75,
			createdAt: daysAgo(60),
			transactionType: null,
			hasSource: true
		},
		{
			id: 4,
			amount: 30,
			createdAt: daysAgo(90),
			transactionType: "welcome_reward",
			hasSource: true
		}
	]
};
//#endregion
//#region ../widgets/src/hooks/use-points-ledger.ts
const POINTS_LEDGER_QUERY_KEY = "points-ledger";
function usePointsLedger() {
	const widgetsApi = require_error_state.useWidgetsApi();
	const { isPreview } = require_preview_context.useWidgetPreviewContext();
	const registryConfig = require_registry_context.useDataSourceRegistryConfig();
	const { baseUrl } = registryConfig;
	const customerId = registryConfig.variables?.customer_id;
	return (0, _tanstack_react_query.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__ */ require_chunk.__exportAll({
	PointsWidget: () => PointsWidget,
	pointsWidgetPropertySchema: () => pointsWidgetPropertySchema
});
const formatBalance = (balance) => {
	return balance.toLocaleString("en-US");
};
function formatTransactionType(entry) {
	if (entry.transactionType) return entry.transactionType.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
	if (entry.hasSource) return entry.amount > 0 ? "Points Awarded" : "Points Redeemed";
	return "Transaction";
}
function formatEntryDate(dateString) {
	if (!dateString) return "";
	return new Date(dateString).toLocaleDateString(void 0, {
		month: "short",
		day: "numeric",
		year: "numeric"
	});
}
function PointsEntryRow({ entry, isLast, textColor, accentColor, backgroundColor }) {
	const isPositive = entry.amount >= 0;
	const prefix = isPositive ? "+" : "";
	return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
		className: "flex flex-row items-stretch gap-3",
		children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
			className: "relative flex w-2 flex-col items-center",
			children: [!isLast && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `bg-${textColor}/40 absolute top-3 -bottom-3 left-1/2 w-px -translate-x-1/2` }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `border-${textColor}/60 bg-${backgroundColor} relative mt-3 h-2 w-2 shrink-0 rounded-full border` })]
		}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
			className: "flex flex-1 flex-row items-start justify-between py-1.5",
			children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
				className: "flex min-w-0 flex-col",
				children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
					className: `text-sm font-medium text-${textColor} truncate`,
					children: formatTransactionType(entry)
				}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
					className: `text-sm text-${textColor} opacity-70`,
					children: formatEntryDate(entry.createdAt)
				})]
			}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
				className: `text-sm font-medium ${isPositive ? `text-${accentColor}` : "text-destructive"}`,
				children: [prefix, entry.amount.toLocaleString("en-US")]
			})]
		})]
	});
}
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] = (0, react.useState)(false);
	const historyPanelId = (0, react.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__ */ (0, react_jsx_runtime.jsx)("div", {
		className: `@container overflow-hidden rounded-${borderRadius} bg-${backgroundColor} ${require_registries.borderWidthClasses[borderWidth]} ${borderWidth !== "none" ? require_registries.borderColorClasses[borderColor] : ""} ${className ?? ""}`,
		style: { backgroundImage },
		...props,
		children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
			className: `p-${padding} flex flex-col gap-2.5`,
			children: [
				titleEnabled && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
					className: `text-${titleFontSize} font-header font-bold text-${titleColor}`,
					children: title
				}),
				!isLoading && data && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
					className: `text-3xl font-semibold text-${balanceColor} font-header leading-snug`,
					children: formatBalance(data.balance)
				}),
				isLoading ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
					className: "flex min-h-[60px] items-center justify-center",
					children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "h-8 w-8 animate-spin rounded-full border-2 border-current border-t-transparent" })
				}) : isError ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_error_state.ErrorState, {}) : historyEnabled && (!data || data.entries.length === 0) ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
					className: "flex min-h-[60px] flex-col items-center justify-center gap-2",
					children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.Coins, { className: `size-10 text-${textColor} opacity-30` }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
						className: `text-sm font-semibold text-${textColor} opacity-50`,
						children: "No Points Activity"
					})]
				}) : historyEnabled && data && data.entries.length > 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
					className: "flex flex-col gap-2",
					children: [
						/* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsx)("span", {
								className: "flex-1 text-left",
								children: historyTitle
							}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(lucide_react.ChevronDown, { className: `size-4 transition-transform duration-200 ${historyOpen ? "rotate-180" : ""}` })]
						}),
						/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `bg-${textColor}/20 h-px w-full` }),
						/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
							id: historyPanelId,
							className: `flex flex-col ${!historyOpen ? "hidden" : ""}`,
							children: data.entries.map((entry, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(PointsEntryRow, {
								entry,
								isLast: index === data.entries.length - 1,
								textColor,
								accentColor,
								backgroundColor
							}, 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"
		},
		require_registries.getFontSizeField({
			key: "titleFontSize",
			label: "Title Font Size",
			description: "Font size for the title",
			defaultValue: "md",
			group: "Title",
			requiresKeyToBeTrue: "titleEnabled"
		}),
		require_registries.getColorField({
			key: "titleColor",
			label: "Title Color",
			description: "Color for the title",
			defaultValue: "foreground",
			group: "Title",
			requiresKeyToBeTrue: "titleEnabled"
		}),
		require_registries.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"
		},
		require_registries.getColorField({
			key: "textColor",
			label: "Text Color",
			description: "Default text color",
			defaultValue: "foreground",
			group: "Design"
		}),
		require_registries.getColorField({
			key: "accentColor",
			label: "Accent Color",
			description: "Color for positive points amounts",
			defaultValue: "primary",
			group: "Design"
		}),
		{
			key: "separator",
			type: "separator",
			label: "Separator",
			group: "Design"
		},
		require_registries.getPaddingField({
			key: "padding",
			label: "Padding",
			description: "Widget padding",
			defaultValue: 4,
			group: "Design"
		}),
		require_registries.getBorderRadiusField({
			key: "borderRadius",
			label: "Border Radius",
			description: "Widget border radius",
			defaultValue: "md",
			group: "Design"
		}),
		require_registries.getBorderWidthField({
			key: "borderWidth",
			label: "Border Width",
			description: "Widget border width",
			defaultValue: "none",
			group: "Design"
		}),
		require_registries.getBorderColorField({
			key: "borderColor",
			label: "Border Color",
			description: "Widget border color",
			defaultValue: "muted",
			group: "Design"
		})
	]
};
//#endregion
Object.defineProperty(exports, "PointsWidget", {
	enumerable: true,
	get: function() {
		return PointsWidget;
	}
});
Object.defineProperty(exports, "PointsWidget_exports", {
	enumerable: true,
	get: function() {
		return PointsWidget_exports;
	}
});
Object.defineProperty(exports, "pointsWidgetPropertySchema", {
	enumerable: true,
	get: function() {
		return pointsWidgetPropertySchema;
	}
});

//# sourceMappingURL=PointsWidget-Byd2-eEi.cjs.map