const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_model = require("../../../model.js");
const require_serve = require("./serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/pay/toC/PaymentMethods/MWCreditCard/List/index.tsx
/**
* @title: 信用卡列表
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:04
*/
const MWCreditCardList = (props, ref) => {
	const { state } = (0, react.useContext)(require_model.Context);
	const [open, setOpen] = (0, react.useState)(false);
	const [currentCard, setCurrentCard] = (0, react.useState)(0);
	const [loading, setLoading] = (0, react.useState)(false);
	const [creditCardList, setCreditCardList] = (0, react.useState)([]);
	const [manage, setManage] = (0, react.useState)(false);
	const [order, setOrder] = (0, react.useState)(null);
	/**
	* @title: 获取卡列表
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:07
	*/
	const _getCreditCardList = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			const cardList = yield require_serve.getCreditCardList();
			if (!cardList.length) {
				var _props$onAdd;
				(_props$onAdd = props.onAdd) === null || _props$onAdd === void 0 || _props$onAdd.call(props);
				setLoading(false);
				return;
			}
			setCreditCardList(cardList);
			if (cardList.length) setCurrentCard(cardList[0].id);
			setLoading(false);
		});
		return function _getCreditCardList() {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title: 初始化
	* @description:
	* @param {any} _order
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:08
	*/
	const init = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (_order) {
			if (!creditCardList.length) setLoading(true);
			setOrder(_order);
			setOpen(true);
			setManage(false);
			setCurrentCard(0);
			_getCreditCardList();
		});
		return function init(_x) {
			return _ref2.apply(this, arguments);
		};
	}();
	/**
	* @title: 设置卡
	* @description:
	* @param {number} id
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:09
	*/
	const cardChange = (id) => {
		setCurrentCard(id);
	};
	/**
	* @title: 关闭弹窗
	* @description: 供外部调用，直接关闭卡列表弹窗
	*/
	const close = () => {
		setOpen(false);
	};
	(0, react.useImperativeHandle)(ref, () => ({
		init,
		close
	}));
	/**
	* @title: 自定义选项
	* @description:
	* @param {any} d
	* @param {boolean} active
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:09
	*/
	const renderOption = (d, active) => {
		return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellRow, {
			key: d.id,
			platform: "h5",
			gap: 12,
			centerContentFill: true,
			leftContent: /* @__PURE__ */ react.default.createElement("img", {
				className: "credit-card-type-image",
				src: (0, _pisell_materials.getBankCardTypeImg)(d.card_number)
			}),
			centerContent: /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("credit-card-name", { "text-active-color": active }) }, d.card_number),
			rightContent: manage ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
				type: "pisell2-trash-01",
				className: "remove-card-icon",
				onClick: () => {
					changeCreditCard(d.id);
				}
			}) : void 0
		});
	};
	/**
	* @title: 点击取消
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:09
	*/
	const onCancel = () => {
		if (manage) {
			setManage(false);
			resetCards();
			return;
		}
		setOpen(false);
	};
	/**
	* @title: 重置卡的删除状态
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:10
	*/
	const resetCards = () => {
		let _list = [...creditCardList];
		_list.forEach((d) => {
			if (d.delete) d.delete = 0;
		});
		setCreditCardList(_list);
	};
	/**
	* @title: 批量删除卡
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:10
	*/
	const handleBatchDeleteCards = () => {
		let deleteList = creditCardList.filter((d) => d.delete === 1);
		let newList = creditCardList.filter((d) => d.delete !== 1);
		let ids = deleteList.map((d) => d.id);
		if (ids.length) {
			setCreditCardList(newList);
			require_serve.batchDeleteCards({ card_ids: ids });
		}
	};
	/**
	* @title: 点击确认
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:11
	*/
	const { run: onOk } = (0, ahooks.useDebounceFn)(() => {
		var _props$onChange;
		if (manage) {
			setManage(false);
			handleBatchDeleteCards();
			return;
		}
		let cur = creditCardList.find((d) => d.id === currentCard);
		(_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, cur);
	}, { wait: 300 });
	const renderList = (0, react.useMemo)(() => {
		return creditCardList.filter((d) => d.delete !== 1);
	}, [creditCardList]);
	const changeCreditCard = (id) => {
		let _list = [...creditCardList];
		_list.forEach((d) => {
			if (d.id === id) d.delete = 1;
		});
		setCreditCardList(_list);
	};
	const okText = (0, react.useMemo)(() => {
		var _state$payGroup;
		let current = (_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.otherPayment;
		if (current === null || current === void 0 ? void 0 : current.surcharge) {
			var _state$payGroup2;
			return /* @__PURE__ */ react.default.createElement("div", { className: "" }, _pisell_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, { value: ((_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.wait_amount) || 0 }), " +", _pisell_utils.locales.getText("tocPay.text.surcharge"));
		} else {
			var _state$payGroup3;
			return /* @__PURE__ */ react.default.createElement("div", { className: "" }, _pisell_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, { value: ((_state$payGroup3 = state.payGroup) === null || _state$payGroup3 === void 0 ? void 0 : _state$payGroup3.wait_amount) || 0 }));
		}
	}, [state.payGroup]);
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		title: /* @__PURE__ */ react.default.createElement("div", null, _pisell_utils.locales.getText("tocPay.text.creditorDebitCard")),
		open,
		onCancel,
		cancelVisible: false,
		okText: manage ? _pisell_utils.locales.getText("tocPay.text.done") : okText,
		onOk,
		okButtonProps: {
			disabled: !currentCard,
			loading: props.btnLoading,
			style: {
				display: "flex",
				justifyContent: "center",
				alignItems: "center"
			}
		},
		bodyStyle: { padding: 24 }
	}, !manage && /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellLoading, {
		loading,
		mask: true
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellCheckboxGroup, {
		platform: "h5",
		options: renderList,
		value: currentCard,
		iconPosition: "right",
		mode: "single",
		rowKey: "id",
		className: "credit-card-list",
		gap: 12,
		onChange: cardChange,
		optionClassName: "credit-card-item",
		renderOption
	})), !loading && /* @__PURE__ */ react.default.createElement(antd.Button, {
		icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-plus" }),
		type: "dashed",
		className: "credit-card-add-button",
		onClick: props.onAdd
	}, /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("tocPay.text.addCreditOrDebitCard")))), manage && /* @__PURE__ */ react.default.createElement("div", { className: "credit-card-list manage-credit-card-list" }, renderList.map((d) => /* @__PURE__ */ react.default.createElement("div", {
		className: "credit-card-item manage-credit-card",
		key: d.id
	}, renderOption(d)))), !manage && renderList.length ? /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "text",
		className: "credit-card-manage-button",
		onClick: () => {
			setManage(!manage);
		}
	}, _pisell_utils.locales.getText("tocPay.text.manageCard")) : null);
};
var List_default = (0, react.forwardRef)(MWCreditCardList);
//#endregion
exports.default = List_default;
