var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/walletPassGallery/components/usageRule/index.tsx
var usageRule_exports = {};
__export(usageRule_exports, {
  default: () => usageRule_default
});
module.exports = __toCommonJS(usageRule_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_index = require("./index.less");
var { Text } = import_materials.Typography;
function UsageRules({ className = "", style, dataSource }) {
  const {
    extension_data = [],
    customer_order_behavior_count = 0,
    total_order_behavior_count = 0
  } = dataSource || {};
  const creditsData = (0, import_react.useMemo)(() => {
    const usageCreditsItem = extension_data.find(
      (item) => item.field_key === "usage_credits"
    );
    return usageCreditsItem == null ? void 0 : usageCreditsItem.value;
  }, [extension_data]);
  const rules = (0, import_react.useMemo)(() => {
    if (!creditsData) return [];
    const rules2 = [];
    if (creditsData.max_per_day > 0) {
      rules2.push({
        type: "daily",
        limit: creditsData.max_per_day,
        description: "",
        // Will be filled by localized text
        resetTime: ""
        // Will be filled by localized text
      });
    }
    if (creditsData.max_per_week > 0) {
      rules2.push({
        type: "weekly",
        limit: creditsData.max_per_week,
        description: "",
        // Will be filled by localized text
        resetTime: ""
        // Will be filled by localized text
      });
    }
    if (creditsData.max_per_month > 0) {
      rules2.push({
        type: "monthly",
        limit: creditsData.max_per_month,
        description: "",
        // Will be filled by localized text
        resetTime: ""
        // Will be filled by localized text
      });
    }
    return rules2;
  }, [creditsData]);
  const personalLimit = (0, import_react.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: ""
      // Will be filled by localized text
    };
  }, [creditsData, total_order_behavior_count, customer_order_behavior_count]);
  const interpolateText = (text, variables) => {
    let ret = text.replace(/\{(\w+)\}/g, (match, key) => {
      return variables[key] !== void 0 ? variables[key] : match;
    });
    return ret;
  };
  const getText = (key, fallback) => {
    var _a;
    return ((_a = import_utils.locales) == null ? void 0 : _a.getText(key)) || fallback || key;
  };
  const getRuleDescription = (type, limit) => {
    if (limit === 0 && type === "monthly") {
      return getText("wallet-pass-gallery.monthlyLimitUnlimited", "无限制");
    }
    const templateKey = `wallet-pass-gallery.${type}LimitDesc`;
    const template = getText(templateKey, "");
    return interpolateText(template, { limit });
  };
  const getRuleResetTime = (type) => {
    const resetKey = `wallet-pass-gallery.${type}Reset`;
    return getText(resetKey, "");
  };
  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 processedRules = rules ? rules.map((rule) => ({
    ...rule,
    description: getRuleDescription(rule.type, rule.limit),
    resetTime: getRuleResetTime(rule.type)
  })) : defaultRules;
  const finalRules = processedRules;
  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__ */ import_react.default.createElement("div", { className: `pisell-wallet-pass-gallery_usage-rules ${className}`, style }, /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-title" }, getText("wallet-pass-gallery.usageRules", "使用规则")), /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-subtitle" }, getText(
    "wallet-pass-gallery.usageRulesSubtitle",
    "请仔细阅读以下使用条件"
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-section" }, /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-section-title" }, getText("wallet-pass-gallery.timeUsageLimits", "时间使用限制")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-list" }, finalRules.length > 0 ? finalRules.map((rule, index) => /* @__PURE__ */ import_react.default.createElement("div", { key: index, className: "pisell-wallet-pass-gallery_usage-rules-item" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-main-line" }, /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-label" }, getRuleLabel(rule.type)), /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-description" }, rule.description)), rule.resetTime && /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-reset" }, rule.resetTime)))) : /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-content" }, /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-description" }, getText(
    "wallet-pass-gallery.monthlyLimitUnlimited",
    "无限制"
  )))))), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-section" }, /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-section-title" }, getText("wallet-pass-gallery.totalQuantityControl", "总量控制")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_usage-rules-item-content" }, personalLimit && personalLimit.total > 0 ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-label" }, getText("wallet-pass-gallery.personalLimit", "个人限额:")), /* @__PURE__ */ import_react.default.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__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-note" }, getText(
    "wallet-pass-gallery.voucherExpireNote",
    "用完后此券将自动失效"
  ))) : /* @__PURE__ */ import_react.default.createElement(Text, { className: "pisell-wallet-pass-gallery_usage-rules-item-description" }, getText("wallet-pass-gallery.monthlyLimitUnlimited", "无限制"))))));
}
var usageRule_default = UsageRules;
