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/timelineSection/index.tsx
var timelineSection_exports = {};
__export(timelineSection_exports, {
  default: () => timelineSection_default
});
module.exports = __toCommonJS(timelineSection_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_dayjs = __toESM(require("dayjs"));
var import_timezone = __toESM(require("dayjs/plugin/timezone"));
var import_utc = __toESM(require("dayjs/plugin/utc"));
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_serve = require("../../serve");
var import_index = require("./index.less");
import_dayjs.default.extend(import_timezone.default);
import_dayjs.default.extend(import_utc.default);
var stateTimeUnix = 1723255863;
var TimelineSection = (props) => {
  const { code, tag, timezone, apiPrefix, isTerminalEnv, interaction } = props;
  const { symbol = "$" } = (0, import_materials.usePisellConfig)();
  const [isShowtimeLineDetail, setIsShowtimeLineDetail] = import_react.default.useState(false);
  const [isSpending, setIsSpending] = import_react.default.useState(true);
  const [timeLineDetail, setTimeLineDetail] = import_react.default.useState(
    null
  );
  const assetActionEnum = (0, import_react.useMemo)(() => {
    return {
      recharge: tag === "point_card" ? import_utils.locales.getText("wallet-pass-gallery.pointsEarned") : import_utils.locales.getText("wallet-pass-gallery.recharge"),
      order_expend: tag === "point_card" ? import_utils.locales.getText("wallet-pass-gallery.pointsRedeemed") : import_utils.locales.getText("wallet-pass-gallery.spending"),
      order_refund: import_utils.locales.getText("wallet-pass-gallery.refund"),
      expire: import_utils.locales.getText("wallet-pass-gallery.expire")
    };
  }, [tag]);
  const formatCurrencyAmount = (0, import_react.useCallback)(
    (amount = "", isNegative = false) => {
      if (!amount) return "";
      if (tag === "good_pass")
        return import_utils.locales.getText("wallet-pass-gallery.goodPass.usedCredit")(
          parseInt(amount)
        );
      const absAmount = amount.startsWith("-") ? amount.substring(1) : amount;
      const showSymbol = !["point_card", "good_pass"].includes(tag);
      return isNegative ? `-${showSymbol ? symbol : ""}${absAmount}` : `${showSymbol ? symbol : ""}${amount}`;
    },
    [symbol, tag]
  );
  const timeTreeNode = (0, import_react.useCallback)(
    (data) => {
      const {
        action = "",
        created_at = "",
        amount = "",
        currency_symbol = ""
      } = data;
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-node" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-node-left" }, /* @__PURE__ */ import_react.default.createElement("p", { className: "pisell-wallet-pass-gallery_timeline-node-action" }, action ? assetActionEnum[action] : ""), created_at && /* @__PURE__ */ import_react.default.createElement("p", { className: "pisell-wallet-pass-gallery_timeline-node-time" }, created_at, (action === "recharge" || action === "order_expend") && /* @__PURE__ */ import_react.default.createElement(
        "button",
        {
          type: "button",
          className: "pisell-wallet-pass-gallery_timeline-node-detail-link",
          onClick: () => {
            setIsShowtimeLineDetail(true);
            setIsSpending(action === "order_expend");
            setTimeLineDetail(data);
          },
          style: {
            background: "transparent",
            border: "none",
            padding: 0
          }
        },
        import_utils.locales.getText("wallet-pass-gallery.viewDetails")
      ))), /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: `pisell-wallet-pass-gallery_timeline-node-amount${action === "recharge" ? " pisell-wallet-pass-gallery_timeline-node-amount-positive" : ""}${action === "order_expend" ? " pisell-wallet-pass-gallery_timeline-node-amount-negative" : ""}`
        },
        amount && currency_symbol ? formatCurrencyAmount(amount, action === "order_expend") : ""
      ));
    },
    [assetActionEnum, formatCurrencyAmount]
  );
  const assetListReq = (0, import_ahooks.useRequest)(
    () => {
      if (apiPrefix === "/shop") {
        return (0, import_serve.getActiveLogs)({
          filter_card_id: code
        });
      }
      return (0, import_serve.getAssetList)({
        num: "99",
        skip: "1",
        card_id: code,
        sourceType: tag,
        tag,
        business_object: "customer"
      });
    },
    {
      ready: !!code,
      refreshDeps: [code, tag]
    }
  );
  const timelineItems = (0, import_react.useMemo)(() => {
    var _a;
    const list = ((_a = assetListReq.data) == null ? void 0 : _a.list) || [];
    if (!Array.isArray(list) || list.length === 0)
      return [];
    const filteredByAction = list.filter(
      (item) => !!assetActionEnum[item.action || ""]
    );
    const filteredByTime = filteredByAction.filter((item) => {
      const itemUnix = import_dayjs.default.tz((item == null ? void 0 : item.created_at) || "", timezone).unix();
      if ((item == null ? void 0 : item.action) === "order_expend" && itemUnix < stateTimeUnix)
        return false;
      if ((item == null ? void 0 : item.tag) === "point_card" && itemUnix < stateTimeUnix) return false;
      return true;
    });
    const nonZeroPoint = filteredByTime.filter(
      (it) => !(it.tag === "point_card" && (it.amount === "0" || parseFloat(it.amount || "0") === 0))
    );
    return nonZeroPoint.map((item) => ({
      children: assetActionEnum[(item == null ? void 0 : item.action) || ""] ? timeTreeNode(item) : null
    }));
  }, [
    assetListReq.data,
    assetActionEnum,
    timeTreeNode,
    timezone,
    stateTimeUnix
  ]);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-subtitle" }, import_utils.locales.getText("wallet-pass-gallery.activelog")), assetListReq.loading ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-loading" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Skeleton, { active: true, paragraph: { rows: 6 }, round: true })) : timelineItems.length > 0 ? /* @__PURE__ */ import_react.default.createElement(
    import_materials.Timeline,
    {
      items: timelineItems,
      className: "pisell-wallet-pass-gallery_timeline-list"
    }
  ) : /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-empty" }, /* @__PURE__ */ import_react.default.createElement(import_materials.PisellEmpty, null)), /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      platform: "pc",
      mobileModalHeight: "auto",
      cancelVisible: false,
      destroyOnClose: true,
      onCancel: () => setIsShowtimeLineDetail(false),
      title: isSpending ? import_utils.locales.getText("wallet-pass-gallery.spendingDetails") : import_utils.locales.getText("wallet-pass-gallery.rechargeDetails"),
      open: isShowtimeLineDetail,
      footer: [
        /* @__PURE__ */ import_react.default.createElement(
          import_materials.Button,
          {
            type: "primary",
            block: true,
            size: "large",
            onClick: () => setIsShowtimeLineDetail(false)
          },
          import_utils.locales.getText("wallet-pass-gallery.done")
        )
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellStatisticList,
      {
        data: [
          {
            label: import_utils.locales.getText("wallet-pass-gallery.date"),
            value: (timeLineDetail == null ? void 0 : timeLineDetail.created_at) || ""
          },
          {
            label: isSpending ? import_utils.locales.getText("wallet-pass-gallery.spending") : import_utils.locales.getText("wallet-pass-gallery.recharge"),
            value: formatCurrencyAmount(
              (timeLineDetail == null ? void 0 : timeLineDetail.amount) || "",
              isSpending
            )
          },
          {
            label: import_utils.locales.getText("wallet-pass-gallery.viewOrder"),
            value: timeLineDetail && (timeLineDetail == null ? void 0 : timeLineDetail.resource_code) ? `#${timeLineDetail == null ? void 0 : timeLineDetail.resource_code}` : (timeLineDetail == null ? void 0 : timeLineDetail.transaction_id) ? `#${timeLineDetail == null ? void 0 : timeLineDetail.transaction_id}` : "",
            isLink: !!(timeLineDetail && (timeLineDetail == null ? void 0 : timeLineDetail.resource_id)),
            onClick: () => {
              var _a, _b;
              if (isTerminalEnv) {
                return (_b = (_a = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a.postMessageToApp) == null ? void 0 : _b.call(_a, {
                  module: "booking",
                  key: "view_order",
                  data: {
                    order_id: timeLineDetail == null ? void 0 : timeLineDetail.resource_id
                  },
                  origin: "/booking"
                });
              }
              const link = apiPrefix === "/shop" ? `/admin/order/orders/detail?id=${timeLineDetail == null ? void 0 : timeLineDetail.resource_id}` : `/order/details?id=${timeLineDetail == null ? void 0 : timeLineDetail.resource_id}`;
              location.replace(link);
            },
            link: timeLineDetail && (timeLineDetail == null ? void 0 : timeLineDetail.resource_id) ? true : void 0
          }
        ],
        labelColor: "#101828",
        valueColor: "#101828",
        backgroundColors: ["#fff"],
        isHasBorder: false
      }
    )
  ));
};
var timelineSection_default = TimelineSection;
