import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { usePaymentListContext } from "../context.js";
import React from "react";
import { Button } from "antd";
import { formatAmount } from "@pisell/utils";
import { Iconfont } from "@pisell/materials";
import classNames from "classnames";
import "./index.less";
//#region src/pro/pisellPaymentList/Item/index.tsx
/**
* PisellPaymentListItem 组件
* 支付方式列表项组件，用于展示单个支付方式
*/
function PisellPaymentListItem({ method, amountSymbol: propAmountSymbol, customAmount: propCustomAmount, disabled: propDisabled, selected: propSelected, onSelect: propOnSelect, onEdit: propOnEdit, getText: propGetText, className, style, containerStyle: propContainerStyle, titleStyle: propTitleStyle, iconConfig: propIconConfig, surchargeConfig: propSurchargeConfig, actionConfig: propActionConfig }) {
	var _ref;
	const context = usePaymentListContext();
	console.log("PisellPaymentListItemContext", context);
	const amountSymbol = propAmountSymbol !== null && propAmountSymbol !== void 0 ? propAmountSymbol : context === null || context === void 0 ? void 0 : context.amountSymbol;
	const customAmount = propCustomAmount !== null && propCustomAmount !== void 0 ? propCustomAmount : context === null || context === void 0 ? void 0 : context.customAmount;
	const disabled = (_ref = propDisabled !== null && propDisabled !== void 0 ? propDisabled : context === null || context === void 0 ? void 0 : context.disabled) !== null && _ref !== void 0 ? _ref : false;
	const getText = propGetText !== null && propGetText !== void 0 ? propGetText : context === null || context === void 0 ? void 0 : context.getText;
	const onSelect = propOnSelect !== null && propOnSelect !== void 0 ? propOnSelect : context === null || context === void 0 ? void 0 : context.onSelect;
	const onEdit = propOnEdit !== null && propOnEdit !== void 0 ? propOnEdit : context === null || context === void 0 ? void 0 : context.onEdit;
	const selected = propSelected !== null && propSelected !== void 0 ? propSelected : (context === null || context === void 0 ? void 0 : context.selectedId) ? method.id === context.selectedId : false;
	const mergeItemConfig = () => {
		var _baseConfig$actionCon, _methodConfig$actionC, _baseConfig$actionCon2, _methodConfig$actionC2, _baseConfig$actionCon3, _methodConfig$actionC3, _context$itemConfig;
		const baseConfig = (context === null || context === void 0 ? void 0 : context.itemConfig) || {};
		const methodConfig = method.itemConfig || {};
		const mergedConfig = {
			containerStyle: _objectSpread2(_objectSpread2(_objectSpread2({}, baseConfig.containerStyle), methodConfig.containerStyle), propContainerStyle),
			titleStyle: _objectSpread2(_objectSpread2(_objectSpread2({}, baseConfig.titleStyle), methodConfig.titleStyle), propTitleStyle),
			iconConfig: _objectSpread2(_objectSpread2(_objectSpread2({}, baseConfig.iconConfig), methodConfig.iconConfig), propIconConfig),
			surchargeConfig: _objectSpread2(_objectSpread2(_objectSpread2({}, baseConfig.surchargeConfig), methodConfig.surchargeConfig), propSurchargeConfig),
			actionConfig: _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, baseConfig.actionConfig), methodConfig.actionConfig), propActionConfig), {}, {
				buttonStyle: _objectSpread2(_objectSpread2(_objectSpread2({}, (_baseConfig$actionCon = baseConfig.actionConfig) === null || _baseConfig$actionCon === void 0 ? void 0 : _baseConfig$actionCon.buttonStyle), (_methodConfig$actionC = methodConfig.actionConfig) === null || _methodConfig$actionC === void 0 ? void 0 : _methodConfig$actionC.buttonStyle), propActionConfig === null || propActionConfig === void 0 ? void 0 : propActionConfig.buttonStyle),
				arrowStyle: _objectSpread2(_objectSpread2(_objectSpread2({}, (_baseConfig$actionCon2 = baseConfig.actionConfig) === null || _baseConfig$actionCon2 === void 0 ? void 0 : _baseConfig$actionCon2.arrowStyle), (_methodConfig$actionC2 = methodConfig.actionConfig) === null || _methodConfig$actionC2 === void 0 ? void 0 : _methodConfig$actionC2.arrowStyle), propActionConfig === null || propActionConfig === void 0 ? void 0 : propActionConfig.arrowStyle),
				radioStyle: _objectSpread2(_objectSpread2(_objectSpread2({}, (_baseConfig$actionCon3 = baseConfig.actionConfig) === null || _baseConfig$actionCon3 === void 0 ? void 0 : _baseConfig$actionCon3.radioStyle), (_methodConfig$actionC3 = methodConfig.actionConfig) === null || _methodConfig$actionC3 === void 0 ? void 0 : _methodConfig$actionC3.radioStyle), propActionConfig === null || propActionConfig === void 0 ? void 0 : propActionConfig.radioStyle)
			})
		};
		if ((context === null || context === void 0 || (_context$itemConfig = context.itemConfig) === null || _context$itemConfig === void 0 || (_context$itemConfig = _context$itemConfig.actionConfig) === null || _context$itemConfig === void 0 ? void 0 : _context$itemConfig.style) === "radio") mergedConfig.actionConfig.style = "radio";
		return mergedConfig;
	};
	const { containerStyle, titleStyle, iconConfig, surchargeConfig, actionConfig } = mergeItemConfig();
	/**
	* 计算手续费金额
	*/
	const calculateSurcharge = (method) => {
		if (!(method === null || method === void 0 ? void 0 : method.is_surcharge)) return 0;
		const baseAmount = Number(customAmount) || 0;
		if (Number(method.percentage)) return baseAmount * Number(method.percentage) + Number(method.fixed || 0);
		return Number(method.fixed || 0);
	};
	/**
	* 获取容器样式
	*/
	const getContainerStyle = () => {
		if (!containerStyle) return {};
		const styles = {};
		const actionStyle = getActionStyle();
		if (selected && containerStyle.selectedBackgroundColor) styles.backgroundColor = containerStyle.selectedBackgroundColor;
		else if (containerStyle.backgroundColor) styles.backgroundColor = containerStyle.backgroundColor;
		if (actionStyle === "radio") if (selected && containerStyle.selectedBorder) styles.border = containerStyle.selectedBorder;
		else styles.border = "1px solid #EAECF0";
		else if (selected && containerStyle.selectedBorder) styles.border = containerStyle.selectedBorder;
		else if (containerStyle.border) styles.border = containerStyle.border;
		if (containerStyle.padding !== void 0) styles.padding = containerStyle.padding;
		if (containerStyle.borderRadius !== void 0) styles.borderRadius = containerStyle.borderRadius;
		if (containerStyle.gap !== void 0) styles.gap = containerStyle.gap;
		return styles;
	};
	/**
	* 获取标题样式
	*/
	const getTitleStyle = () => {
		if (!titleStyle) return {};
		const styles = {};
		if (selected && titleStyle.selectedColor) styles.color = titleStyle.selectedColor;
		else if (titleStyle.color) styles.color = titleStyle.color;
		if (titleStyle.fontSize) styles.fontSize = titleStyle.fontSize;
		if (titleStyle.fontWeight) styles.fontWeight = titleStyle.fontWeight;
		return styles;
	};
	/**
	* 获取标题文本
	*/
	const getTitleText = () => {
		return (titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.text) || method.name;
	};
	/**
	* 获取 Surcharge 样式
	*/
	const getSurchargeStyle = () => {
		if (!surchargeConfig) return {};
		const styles = {};
		if (selected && surchargeConfig.selectedColor) styles.color = surchargeConfig.selectedColor;
		else if (surchargeConfig.color) styles.color = surchargeConfig.color;
		if (surchargeConfig.fontSize) styles.fontSize = surchargeConfig.fontSize;
		if (surchargeConfig.fontWeight) styles.fontWeight = surchargeConfig.fontWeight;
		return styles;
	};
	/**
	* 是否显示 Surcharge
	*/
	const shouldShowSurcharge = () => {
		if ((surchargeConfig === null || surchargeConfig === void 0 ? void 0 : surchargeConfig.show) === false) return false;
		if ((surchargeConfig === null || surchargeConfig === void 0 ? void 0 : surchargeConfig.show) === true) return true;
		return !!(method === null || method === void 0 ? void 0 : method.is_surcharge) && calculateSurcharge(method) !== 0;
	};
	/**
	* 获取 Surcharge 标签文本
	*/
	const getSurchargeLabel = () => {
		return (surchargeConfig === null || surchargeConfig === void 0 ? void 0 : surchargeConfig.label) || (getText === null || getText === void 0 ? void 0 : getText("pisell2.payment-options.surcharge-label", "Surcharge")) || "Surcharge";
	};
	/**
	* 处理编辑按钮点击事件
	*/
	const handleEditClick = (e) => {
		e.preventDefault();
		e.stopPropagation();
		if (disabled || !onEdit) return;
		onEdit(_objectSpread2(_objectSpread2({}, method), {}, { surcharge: String(formatAmount(calculateSurcharge(method), 2, amountSymbol)) }));
	};
	/**
	* 是否显示编辑按钮
	*/
	const shouldShowEditButton = () => {
		return (surchargeConfig === null || surchargeConfig === void 0 ? void 0 : surchargeConfig.enableEdit) === true;
	};
	/**
	* 获取编辑按钮样式
	*/
	const getEditButtonStyle = () => {
		if (!(surchargeConfig === null || surchargeConfig === void 0 ? void 0 : surchargeConfig.editButtonStyle)) return {};
		const editStyle = surchargeConfig.editButtonStyle;
		const styles = {};
		if (editStyle.backgroundColor) styles.backgroundColor = editStyle.backgroundColor;
		if (editStyle.iconColor) styles.color = editStyle.iconColor;
		if (editStyle.padding !== void 0) styles.padding = editStyle.padding;
		if (editStyle.borderRadius !== void 0) styles.borderRadius = editStyle.borderRadius;
		if (editStyle.border) styles.border = editStyle.border;
		if (editStyle.marginTop !== void 0) styles.marginTop = editStyle.marginTop;
		if (editStyle.marginBottom !== void 0) styles.marginBottom = editStyle.marginBottom;
		if (editStyle.marginLeft !== void 0) styles.marginLeft = editStyle.marginLeft;
		if (editStyle.marginRight !== void 0) styles.marginRight = editStyle.marginRight;
		return styles;
	};
	/**
	* 获取编辑按钮图标大小
	*/
	const getEditButtonIconSize = () => {
		var _surchargeConfig$edit;
		return (surchargeConfig === null || surchargeConfig === void 0 || (_surchargeConfig$edit = surchargeConfig.editButtonStyle) === null || _surchargeConfig$edit === void 0 ? void 0 : _surchargeConfig$edit.iconSize) || "14px";
	};
	/**
	* 获取编辑按钮图标类型
	*/
	const getEditButtonIcon = () => {
		var _surchargeConfig$edit2;
		return (surchargeConfig === null || surchargeConfig === void 0 || (_surchargeConfig$edit2 = surchargeConfig.editButtonStyle) === null || _surchargeConfig$edit2 === void 0 ? void 0 : _surchargeConfig$edit2.icon) || "pisell2-edit";
	};
	/**
	* 是否显示右侧操作区域
	*/
	const shouldShowAction = () => {
		if ((actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.show) !== void 0) return actionConfig.show;
		return true;
	};
	/**
	* 获取操作样式（单选模式或按钮模式）
	*/
	const getActionStyle = () => {
		if (actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.style) return actionConfig.style;
		return "radio";
	};
	/**
	* 获取按钮类型（按钮或箭头，仅在按钮模式下有效）
	*/
	const getButtonType = () => {
		if (actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.buttonType) return actionConfig.buttonType;
		return "arrow";
	};
	/**
	* 处理右侧按钮点击事件
	*/
	const handleRightButtonClick = (e) => {
		const actionStyle = getActionStyle();
		const buttonType = getButtonType();
		if (actionStyle === "button" && buttonType === "arrow") return;
		e.preventDefault();
		e.stopPropagation();
		if (disabled || !onSelect) return;
		onSelect(_objectSpread2(_objectSpread2({}, method), {}, { surcharge: String(formatAmount(calculateSurcharge(method), 2, amountSymbol)) }));
	};
	/**
	* 获取图标容器样式
	*/
	const getIconContainerStyle = (defaultBackground) => {
		const styles = { background: (iconConfig === null || iconConfig === void 0 ? void 0 : iconConfig.backgroundColor) || defaultBackground };
		if (iconConfig === null || iconConfig === void 0 ? void 0 : iconConfig.size) {
			styles.width = iconConfig.size;
			styles.height = iconConfig.size;
		}
		return styles;
	};
	/**
	* 获取图标样式
	*/
	const getIconStyle = (defaultIconSize) => {
		return {
			fontSize: (iconConfig === null || iconConfig === void 0 ? void 0 : iconConfig.iconSize) || defaultIconSize,
			color: (iconConfig === null || iconConfig === void 0 ? void 0 : iconConfig.iconColor) || "#ffffff"
		};
	};
	/**
	* 渲染支付图标
	*/
	const renderPaymentIcon = (method) => {
		const iconTypeMap = {
			card: "pisell2-custom",
			cash: "pisell2-cash",
			custom: "pisell2-credit"
		};
		const backgroundMap = {
			card: "#FE8279",
			cash: "#52C41A",
			custom: "#D277E4"
		};
		const iconType = (iconConfig === null || iconConfig === void 0 ? void 0 : iconConfig.iconType) || iconTypeMap[method.type] || iconTypeMap.custom;
		const defaultBackground = backgroundMap[method.type] || backgroundMap.custom;
		return /* @__PURE__ */ React.createElement("div", {
			className: "pisell-payment-list-item-icon-container",
			style: getIconContainerStyle(defaultBackground)
		}, /* @__PURE__ */ React.createElement(Iconfont, {
			type: iconType,
			style: getIconStyle("26px")
		}));
	};
	/**
	* 渲染左侧内容
	*/
	const renderLeftContent = () => /* @__PURE__ */ React.createElement("div", {
		className: "pisell-payment-list-item-left",
		onClick: (e) => {
			if (shouldShowEditButton()) handleEditClick(e);
		}
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-payment-list-item-icon-container" }, renderPaymentIcon(method)), /* @__PURE__ */ React.createElement("div", { className: "pisell-payment-list-item-info" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-payment-list-item-info-inner" }, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-payment-list-item-name",
		style: getTitleStyle()
	}, getTitleText()), /* @__PURE__ */ React.createElement("div", { className: "pisell-payment-list-item-info-surcharge" }, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-payment-list-item-surcharge-text",
		style: getSurchargeStyle()
	}, shouldShowSurcharge() && getSurchargeLabel() + ":", shouldShowSurcharge() && formatAmount(calculateSurcharge(method), 2, amountSymbol), shouldShowEditButton() && /* @__PURE__ */ React.createElement("div", {
		className: classNames("pisell-payment-list-item-edit-button", { "pisell-payment-list-item-edit-button-disabled": disabled }),
		style: getEditButtonStyle()
	}, /* @__PURE__ */ React.createElement(Iconfont, {
		type: getEditButtonIcon(),
		style: { fontSize: getEditButtonIconSize() }
	})))))));
	/**
	* 渲染右侧内容
	*/
	const renderRightContent = () => {
		if (!shouldShowAction()) return null;
		const actionStyle = getActionStyle();
		const buttonType = getButtonType();
		if (actionStyle === "radio") {
			const radioStyle = (actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.radioStyle) || {};
			const size = radioStyle.size || 16;
			const checkedBorderColor = radioStyle.checkedBorderColor || "#7F56D9";
			const checkedBackgroundColor = radioStyle.checkedBackgroundColor || "#F9F5FF";
			const checkedDotColor = radioStyle.checkedDotColor || "#7F56D9";
			const uncheckedBorderColor = radioStyle.uncheckedBorderColor || "#D0D5DD";
			const uncheckedBackgroundColor = radioStyle.uncheckedBackgroundColor || "#FFFFFF";
			const radioContainerStyle = {
				width: size,
				height: size,
				position: "relative",
				backgroundColor: selected ? checkedBackgroundColor : uncheckedBackgroundColor,
				borderRadius: "50%",
				border: `1px solid ${selected ? checkedBorderColor : uncheckedBorderColor}`,
				display: "flex",
				alignItems: "center",
				justifyContent: "center",
				flexShrink: 0
			};
			return /* @__PURE__ */ React.createElement("div", { className: "pisell-payment-list-item-right" }, /* @__PURE__ */ React.createElement("div", {
				className: "pisell-payment-list-item-radio",
				style: radioContainerStyle
			}, selected && /* @__PURE__ */ React.createElement("div", { style: {
				width: size / 2.67,
				height: size / 2.67,
				backgroundColor: checkedDotColor,
				borderRadius: "50%"
			} })));
		}
		if (buttonType === "button") {
			const buttonStyle = (actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.buttonStyle) || {};
			const buttonText = buttonStyle.text || (getText === null || getText === void 0 ? void 0 : getText("pay", "Pay")) || "Pay";
			const buttonCustomStyle = {};
			if (buttonStyle.color) buttonCustomStyle.color = buttonStyle.color;
			if (buttonStyle.fontSize) buttonCustomStyle.fontSize = buttonStyle.fontSize;
			if (buttonStyle.fontWeight) buttonCustomStyle.fontWeight = buttonStyle.fontWeight;
			if (buttonStyle.backgroundColor) buttonCustomStyle.backgroundColor = buttonStyle.backgroundColor;
			if (buttonStyle.padding) buttonCustomStyle.padding = buttonStyle.padding;
			if (buttonStyle.borderRadius) buttonCustomStyle.borderRadius = buttonStyle.borderRadius;
			if (buttonStyle.border) buttonCustomStyle.border = buttonStyle.border;
			return /* @__PURE__ */ React.createElement("div", {
				className: "pisell-payment-list-item-right",
				onClick: handleRightButtonClick
			}, /* @__PURE__ */ React.createElement(Button, {
				type: "primary",
				className: "pisell-payment-list-item-pay-button",
				disabled,
				style: buttonCustomStyle
			}, buttonText));
		}
		const arrowStyle = (actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.arrowStyle) || {};
		const iconType = arrowStyle.icon || "pisell2-a-arrow-circle-right2";
		const iconColor = arrowStyle.iconColor || (disabled ? "#D0D5DD" : "#7F56DA");
		const iconSize = arrowStyle.iconSize || "30px";
		const arrowContainerStyle = {};
		if (arrowStyle.backgroundColor) arrowContainerStyle.backgroundColor = arrowStyle.backgroundColor;
		if (arrowStyle.padding) arrowContainerStyle.padding = arrowStyle.padding;
		if (arrowStyle.borderRadius) arrowContainerStyle.borderRadius = arrowStyle.borderRadius;
		if (arrowStyle.border) arrowContainerStyle.border = arrowStyle.border;
		return /* @__PURE__ */ React.createElement("div", { className: "pisell-payment-list-item-right" }, /* @__PURE__ */ React.createElement("div", {
			className: "pisell-payment-list-item-selection-button",
			style: arrowContainerStyle
		}, /* @__PURE__ */ React.createElement(Iconfont, {
			type: iconType,
			style: {
				fontSize: iconSize,
				color: iconColor
			}
		})));
	};
	const actionStyle = getActionStyle();
	const buttonType = getButtonType();
	/**
	* 处理容器点击事件
	*/
	const handleContainerClick = () => {
		if (disabled) return;
		if (actionStyle === "radio" || actionStyle === "button" && buttonType === "arrow") {
			if (typeof onSelect === "function") onSelect(_objectSpread2(_objectSpread2({}, method), {}, { surcharge: String(formatAmount(calculateSurcharge(method), 2, amountSymbol)) }));
		}
	};
	const containerProps = {
		className: classNames("pisell-payment-list-item", {
			"pisell-payment-list-item-pointer": !disabled,
			"pisell-payment-list-item-disabled": disabled
		}, className),
		style: _objectSpread2(_objectSpread2({}, getContainerStyle()), style),
		onClick: handleContainerClick
	};
	const Container = actionStyle === "radio" ? "div" : "button";
	const extraProps = actionStyle === "radio" ? {} : { type: "button" };
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Container, _objectSpread2(_objectSpread2({}, containerProps), extraProps), renderLeftContent(), renderRightContent()));
}
//#endregion
export { PisellPaymentListItem as default };
