import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import React, { useState } from "react";
import { Button } from "antd";
import { isIos, locales } from "@pisell/utils";
import { PisellModal, PisellToast } from "@pisell/materials";
import "./index.less";
//#region src/plus/walletPassGallery/components/footerButton/index.tsx
/**
* 通用底部按钮组件
* 根据step不同显示不同的按钮组
*/
const FooterButtons = ({ onRechargeClick, platform = "h5", appleWalletUrl, googleWalletUrl }) => {
	if (platform === "pc") return null;
	const [addWalletLoading, setAddWalletLoading] = useState(false);
	const [wechatModalOpen, setWechatModalOpen] = useState(false);
	const [copyUrl, setCopyUrl] = useState("");
	const addWalletPass = function() {
		var _ref = _asyncToGenerator(function* (url, type) {
			if (!url) {
				PisellToast({
					content: locales.getText("wallet-pass-gallery.urlEmpty") || "钱包链接为空",
					duration: 2
				});
				return false;
			}
			if (isWechat()) {
				setWechatModalOpen(true);
				setCopyUrl(url);
				return false;
			}
			setAddWalletLoading(true);
			try {
				if (type === "apple") {
					const response = yield fetch(url);
					if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
					const passData = yield response.arrayBuffer();
					const blob = new Blob([passData], { type: "application/vnd.apple.pkpass" });
					const downloadUrl = URL.createObjectURL(blob);
					const a = document.createElement("a");
					a.href = downloadUrl;
					a.download = "pass.pkpass";
					a.click();
					URL.revokeObjectURL(downloadUrl);
				} else window.open(url);
			} catch (error) {
				console.error("Failed to add wallet:", error);
				PisellToast({
					content: locales.getText("wallet-pass-gallery.error.addFailed"),
					duration: 3
				});
			} finally {
				setAddWalletLoading(false);
			}
		});
		return function addWalletPass(_x, _x2) {
			return _ref.apply(this, arguments);
		};
	}();
	const onAppleClick = function() {
		var _ref2 = _asyncToGenerator(function* () {
			yield addWalletPass(appleWalletUrl || "", "apple");
		});
		return function onAppleClick() {
			return _ref2.apply(this, arguments);
		};
	}();
	const onGoogleClick = function() {
		var _ref3 = _asyncToGenerator(function* () {
			yield addWalletPass(googleWalletUrl || "", "google");
		});
		return function onGoogleClick() {
			return _ref3.apply(this, arguments);
		};
	}();
	const isWechat = () => {
		return /MicroMessenger/i.test(window.navigator.userAgent);
	};
	const httpCopyToClipboard = (text) => {
		const textArea = document.createElement("textarea");
		textArea.value = text;
		textArea.style.position = "fixed";
		textArea.style.top = "-10000px";
		textArea.style.left = "-10000px";
		document.body.appendChild(textArea);
		textArea.focus();
		textArea.select();
		document.execCommand("copy");
		document.body.removeChild(textArea);
	};
	const copyToClipboard = (text) => {
		if (navigator.clipboard) return navigator.clipboard.writeText(text);
		else return httpCopyToClipboard(text);
	};
	const onCopyClick = () => {
		copyToClipboard(copyUrl);
		PisellToast({
			content: locales.getText("wallet-pass-gallery.copySuccess"),
			duration: 2
		});
		setWechatModalOpen(false);
	};
	const renderButtons = () => {
		const buttons = [];
		if (appleWalletUrl || googleWalletUrl) buttons.push(/* @__PURE__ */ React.createElement(Button, {
			loading: addWalletLoading,
			key: "add-wallet",
			onClick: isIos() ? onAppleClick : onGoogleClick,
			size: "large",
			className: "pisell-wallet-pass-gallery_footer-buttons-item pisell-wallet-pass-gallery_footer-buttons-item-secondary"
		}, isIos() ? locales.getText("wallet-pass-gallery.button.addToAppleWallet") : locales.getText("wallet-pass-gallery.button.addToGoogleWallet")));
		if (onRechargeClick) buttons.push(/* @__PURE__ */ React.createElement(Button, {
			key: "recharge",
			type: "primary",
			onClick: onRechargeClick,
			size: "large",
			className: "pisell-wallet-pass-gallery_footer-buttons-item pisell-wallet-pass-gallery_footer-buttons-item-primary"
		}, locales.getText("wallet-pass-gallery.recharge")));
		return buttons.length > 0 ? /* @__PURE__ */ React.createElement("div", { className: `pisell-wallet-pass-gallery_footer-buttons` }, buttons) : null;
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_footer" }, renderButtons()), /* @__PURE__ */ React.createElement(PisellModal, {
		destroyOnClose: true,
		platform,
		mobileModalHeight: "auto",
		headerDivider: false,
		closable: true,
		maskClosable: true,
		onCancel: () => {
			setWechatModalOpen(false);
		},
		open: wechatModalOpen,
		footer: [/* @__PURE__ */ React.createElement(Button, {
			onClick: onCopyClick,
			block: true,
			type: "primary",
			size: "large"
		}, locales.getText("wallet-pass-gallery.copyButton"))]
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_footer-copy-title" }, locales.getText("wallet-pass-gallery.copyTitle")), /* @__PURE__ */ React.createElement("div", { className: "pisell-wallet-pass-gallery_footer-copy-text" }, locales.getText("wallet-pass-gallery.copyText"))));
};
//#endregion
export { FooterButtons as default };
