const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_serve = require("../../serve.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
let dayjs_plugin_timezone = require("dayjs/plugin/timezone");
dayjs_plugin_timezone = require_runtime.__toESM(dayjs_plugin_timezone);
let dayjs_plugin_utc = require("dayjs/plugin/utc");
dayjs_plugin_utc = require_runtime.__toESM(dayjs_plugin_utc);
require("./index.less");
//#region src/plus/walletPassGallery/components/timelineSection/index.tsx
dayjs.default.extend(dayjs_plugin_timezone.default);
dayjs.default.extend(dayjs_plugin_utc.default);
const stateTimeUnix = 1723255863;
const TimelineSection = (props) => {
	const { code, tag, timezone, apiPrefix, isTerminalEnv, interaction } = props;
	const { symbol = "$" } = (0, _pisell_materials.usePisellConfig)();
	const [isShowtimeLineDetail, setIsShowtimeLineDetail] = react.default.useState(false);
	const [isSpending, setIsSpending] = react.default.useState(true);
	const [timeLineDetail, setTimeLineDetail] = react.default.useState(null);
	const assetActionEnum = (0, react.useMemo)(() => {
		return {
			recharge: tag === "point_card" ? _pisell_utils.locales.getText("wallet-pass-gallery.pointsEarned") : _pisell_utils.locales.getText("wallet-pass-gallery.recharge"),
			order_expend: tag === "point_card" ? _pisell_utils.locales.getText("wallet-pass-gallery.pointsRedeemed") : _pisell_utils.locales.getText("wallet-pass-gallery.spending"),
			order_refund: _pisell_utils.locales.getText("wallet-pass-gallery.refund"),
			expire: _pisell_utils.locales.getText("wallet-pass-gallery.expire")
		};
	}, [tag]);
	const formatCurrencyAmount = (0, react.useCallback)((amount = "", isNegative = false) => {
		if (!amount) return "";
		if (tag === "good_pass") return _pisell_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, react.useCallback)((data) => {
		const { action = "", created_at = "", amount = "", currency_symbol = "" } = data;
		return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-node" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-node-left" }, /* @__PURE__ */ react.default.createElement("p", { className: "pisell-wallet-pass-gallery_timeline-node-action" }, action ? assetActionEnum[action] : ""), created_at && /* @__PURE__ */ react.default.createElement("p", { className: "pisell-wallet-pass-gallery_timeline-node-time" }, created_at, (action === "recharge" || action === "order_expend") && /* @__PURE__ */ 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
			}
		}, _pisell_utils.locales.getText("wallet-pass-gallery.viewDetails")))), /* @__PURE__ */ 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, ahooks.useRequest)(() => {
		if (apiPrefix === "/shop") return require_serve.getActiveLogs({ filter_card_id: code });
		return require_serve.getAssetList({
			num: "99",
			skip: "1",
			card_id: code,
			sourceType: tag,
			tag,
			business_object: "customer"
		});
	}, {
		ready: !!code,
		refreshDeps: [code, tag]
	});
	const timelineItems = (0, react.useMemo)(() => {
		var _assetListReq$data;
		const list = ((_assetListReq$data = assetListReq.data) === null || _assetListReq$data === void 0 ? void 0 : _assetListReq$data.list) || [];
		if (!Array.isArray(list) || list.length === 0) return [];
		return list.filter((item) => !!assetActionEnum[item.action || ""]).filter((item) => {
			const itemUnix = dayjs.default.tz((item === null || item === void 0 ? void 0 : item.created_at) || "", timezone).unix();
			if ((item === null || item === void 0 ? void 0 : item.action) === "order_expend" && itemUnix < stateTimeUnix) return false;
			if ((item === null || item === void 0 ? void 0 : item.tag) === "point_card" && itemUnix < stateTimeUnix) return false;
			return true;
		}).filter((it) => !(it.tag === "point_card" && (it.amount === "0" || parseFloat(it.amount || "0") === 0))).map((item) => ({ children: assetActionEnum[(item === null || item === void 0 ? void 0 : item.action) || ""] ? timeTreeNode(item) : null }));
	}, [
		assetListReq.data,
		assetActionEnum,
		timeTreeNode,
		timezone,
		stateTimeUnix
	]);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-subtitle" }, _pisell_utils.locales.getText("wallet-pass-gallery.activelog")), assetListReq.loading ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-loading" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Skeleton, {
		active: true,
		paragraph: { rows: 6 },
		round: true
	})) : timelineItems.length > 0 ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Timeline, {
		items: timelineItems,
		className: "pisell-wallet-pass-gallery_timeline-list"
	}) : /* @__PURE__ */ react.default.createElement("div", { className: "pisell-wallet-pass-gallery_timeline-empty" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellEmpty, null)), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		platform: "pc",
		mobileModalHeight: "auto",
		cancelVisible: false,
		destroyOnClose: true,
		onCancel: () => setIsShowtimeLineDetail(false),
		title: isSpending ? _pisell_utils.locales.getText("wallet-pass-gallery.spendingDetails") : _pisell_utils.locales.getText("wallet-pass-gallery.rechargeDetails"),
		open: isShowtimeLineDetail,
		footer: [/* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "primary",
			block: true,
			size: "large",
			onClick: () => setIsShowtimeLineDetail(false)
		}, _pisell_utils.locales.getText("wallet-pass-gallery.done"))]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellStatisticList, {
		data: [
			{
				label: _pisell_utils.locales.getText("wallet-pass-gallery.date"),
				value: (timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.created_at) || ""
			},
			{
				label: isSpending ? _pisell_utils.locales.getText("wallet-pass-gallery.spending") : _pisell_utils.locales.getText("wallet-pass-gallery.recharge"),
				value: formatCurrencyAmount((timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.amount) || "", isSpending)
			},
			{
				label: _pisell_utils.locales.getText("wallet-pass-gallery.viewOrder"),
				value: timeLineDetail && (timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.resource_code) ? `#${timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.resource_code}` : (timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.transaction_id) ? `#${timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.transaction_id}` : "",
				isLink: !!(timeLineDetail && (timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.resource_id)),
				onClick: () => {
					if (isTerminalEnv) {
						var _interaction$utils, _interaction$utils$po;
						return interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$po = _interaction$utils.postMessageToApp) === null || _interaction$utils$po === void 0 ? void 0 : _interaction$utils$po.call(_interaction$utils, {
							module: "booking",
							key: "view_order",
							data: { order_id: timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.resource_id },
							origin: "/booking"
						});
					}
					const link = apiPrefix === "/shop" ? `/admin/order/orders/detail?id=${timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.resource_id}` : `/order/details?id=${timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.resource_id}`;
					location.replace(link);
				},
				link: timeLineDetail && (timeLineDetail === null || timeLineDetail === void 0 ? void 0 : timeLineDetail.resource_id) ? true : void 0
			}
		],
		labelColor: "#101828",
		valueColor: "#101828",
		backgroundColors: ["#fff"],
		isHasBorder: false
	})));
};
//#endregion
exports.default = TimelineSection;
