import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import React, { useMemo } from "react";
import { locales } from "@pisell/utils";
import { Typography } from "@pisell/materials";
import "./index.less";
//#region src/plus/walletPassGallery/components/usageRule/index.tsx
const { Text } = Typography;
function UsageRules({ className = "", style, dataSource }) {
	const { extension_data = [], customer_order_behavior_count = 0, total_order_behavior_count = 0 } = dataSource || {};
	const creditsData = useMemo(() => {
		const usageCreditsItem = extension_data.find((item) => item.field_key === "usage_credits");
		return usageCreditsItem === null || usageCreditsItem === void 0 ? void 0 : usageCreditsItem.value;
	}, [extension_data]);
	const rules = useMemo(() => {
		if (!creditsData) return [];
		const rules = [];
		if (creditsData.max_per_day > 0) rules.push({
			type: "daily",
			limit: creditsData.max_per_day,
			description: "",
			resetTime: ""
		});
		if (creditsData.max_per_week > 0) rules.push({
			type: "weekly",
			limit: creditsData.max_per_week,
			description: "",
			resetTime: ""
		});
		if (creditsData.max_per_month > 0) rules.push({
			type: "monthly",
			limit: creditsData.max_per_month,
			description: "",
			resetTime: ""
		});
		return rules;
	}, [creditsData]);
	const personalLimit = useMemo(() => {
		if (!creditsData) return void 0;
		const totalCredits = creditsData.total_credits || 0;
		const perUserLimit = creditsData.per_user_limit || 0;
		let total = 0;
		if (totalCredits > 0 && perUserLimit > 0) total = Math.min(totalCredits, perUserLimit);
		else total = Math.max(totalCredits, perUserLimit);
		let total_remaining = 0;
		if (totalCredits > 0) total_remaining = totalCredits - (total_order_behavior_count || 0);
		let perUser_remaining = 0;
		if (perUserLimit > 0) perUser_remaining = perUserLimit - (customer_order_behavior_count || 0);
		let remaining = 0;
		if (total_remaining > 0 && perUser_remaining > 0) remaining = Math.min(total_remaining, perUser_remaining);
		else if (total_remaining === 0) if (totalCredits === 0) remaining = perUser_remaining;
		else remaining = 0;
		else if (perUser_remaining === 0) remaining = 0;
		return {
			total,
			remaining,
			note: ""
		};
	}, [
		creditsData,
		total_order_behavior_count,
		customer_order_behavior_count
	]);
	const interpolateText = (text, variables) => {
		return text.replace(/\{(\w+)\}/g, (match, key) => {
			return variables[key] !== void 0 ? variables[key] : match;
		});
	};
	const getText = (key, fallback) => {
		return (locales === null || locales === void 0 ? void 0 : locales.getText(key)) || fallback || key;
	};
	const getRuleDescription = (type, limit) => {
		if (limit === 0 && type === "monthly") return getText("wallet-pass-gallery.monthlyLimitUnlimited", "无限制");
		return interpolateText(getText(`wallet-pass-gallery.${type}LimitDesc`, ""), { limit });
	};
	const getRuleResetTime = (type) => {
		return getText(`wallet-pass-gallery.${type}Reset`, "");
	};
	const defaultRules = [
		{
			type: "daily",
			limit: 3,
			description: getRuleDescription("daily", 3),
			resetTime: getRuleResetTime("daily")
		},
		{
			type: "weekly",
			limit: 15,
			description: getRuleDescription("weekly", 15),
			resetTime: getRuleResetTime("weekly")
		},
		{
			type: "monthly",
			limit: 60,
			description: getRuleDescription("monthly", 60),
			resetTime: getRuleResetTime("monthly")
		}
	];
	const finalRules = rules ? rules.map((rule) => _objectSpread2(_objectSpread2({}, rule), {}, {
		description: getRuleDescription(rule.type, rule.limit),
		resetTime: getRuleResetTime(rule.type)
	})) : defaultRules;
	const getRuleLabel = (type) => {
		switch (type) {
			case "daily": return getText("wallet-pass-gallery.dailyLimit", "每日限制:");
			case "weekly": return getText("wallet-pass-gallery.weeklyLimit", "每周限制:");
			case "monthly": return getText("wallet-pass-gallery.monthlyLimit", "每月限制:");
			default: return getText("wallet-pass-gallery.dailyLimit", "限制:");
		}
	};
	if (!creditsData) return null;
	return /* @__PURE__ */ React.createElement("div", {
		className: `pisell-wallet-pass-gallery_usage-rules ${className}`,
		style
	}, /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-title" }, getText("wallet-pass-gallery.usageRules", "使用规则")), /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-subtitle" }, getText("wallet-pass-gallery.usageRulesSubtitle", "请仔细阅读以下使用条件")), /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-section" }, /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-section-title" }, getText("wallet-pass-gallery.timeUsageLimits", "时间使用限制")), /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-list" }, finalRules.length > 0 ? finalRules.map((rule, index) => /* @__PURE__ */ React.createElement("div", {
		key: index,
		className: "pisell-wallet-pass-gallery_usage-rules-item"
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-content" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-main-line" }, /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-label" }, getRuleLabel(rule.type)), /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-description" }, rule.description)), rule.resetTime && /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-reset" }, rule.resetTime)))) : /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-content" }, /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-description" }, getText("wallet-pass-gallery.monthlyLimitUnlimited", "无限制")))))), /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-section" }, /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-section-title" }, getText("wallet-pass-gallery.totalQuantityControl", "总量控制")), /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-content" }, personalLimit && personalLimit.total > 0 ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-label" }, getText("wallet-pass-gallery.personalLimit", "个人限额:")), /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-description" }, interpolateText(getText("wallet-pass-gallery.personalLimitDesc", "您最多可使用此券{total}次(剩余{remaining}次)"), {
		total: personalLimit.total,
		remaining: personalLimit.remaining
	})), /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-note" }, getText("wallet-pass-gallery.voucherExpireNote", "用完后此券将自动失效"))) : /* @__PURE__ */ React.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-description" }, getText("wallet-pass-gallery.monthlyLimitUnlimited", "无限制"))))));
}
//#endregion
export { UsageRules as default };
