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/footerButton/index.tsx
var footerButton_exports = {};
__export(footerButton_exports, {
  default: () => footerButton_default
});
module.exports = __toCommonJS(footerButton_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_index = require("./index.less");
var FooterButtons = ({
  onRechargeClick,
  platform = "h5",
  appleWalletUrl,
  googleWalletUrl
}) => {
  if (platform === "pc") {
    return null;
  }
  const [addWalletLoading, setAddWalletLoading] = (0, import_react.useState)(false);
  const [wechatModalOpen, setWechatModalOpen] = (0, import_react.useState)(false);
  const [copyUrl, setCopyUrl] = (0, import_react.useState)("");
  const addWalletPass = async (url, type) => {
    if (!url) {
      (0, import_materials.PisellToast)({
        content: import_utils.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 = await fetch(url);
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        const passData = await 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);
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("wallet-pass-gallery.error.addFailed"),
        duration: 3
      });
    } finally {
      setAddWalletLoading(false);
    }
  };
  const onAppleClick = async () => {
    await addWalletPass(appleWalletUrl || "", "apple");
  };
  const onGoogleClick = async () => {
    await addWalletPass(googleWalletUrl || "", "google");
  };
  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);
    (0, import_materials.PisellToast)({
      content: import_utils.locales.getText("wallet-pass-gallery.copySuccess"),
      duration: 2
    });
    setWechatModalOpen(false);
  };
  const renderButtons = () => {
    const buttons = [];
    if (appleWalletUrl || googleWalletUrl) {
      buttons.push(
        /* @__PURE__ */ import_react.default.createElement(
          import_antd.Button,
          {
            loading: addWalletLoading,
            key: "add-wallet",
            onClick: (0, import_utils.isIos)() ? onAppleClick : onGoogleClick,
            size: "large",
            className: "pisell-wallet-pass-gallery_footer-buttons-item pisell-wallet-pass-gallery_footer-buttons-item-secondary"
          },
          (0, import_utils.isIos)() ? import_utils.locales.getText("wallet-pass-gallery.button.addToAppleWallet") : import_utils.locales.getText("wallet-pass-gallery.button.addToGoogleWallet")
        )
      );
    }
    if (onRechargeClick) {
      buttons.push(
        /* @__PURE__ */ import_react.default.createElement(
          import_antd.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"
          },
          import_utils.locales.getText("wallet-pass-gallery.recharge")
        )
      );
    }
    const className = `pisell-wallet-pass-gallery_footer-buttons`;
    return buttons.length > 0 ? /* @__PURE__ */ import_react.default.createElement("div", { className }, buttons) : null;
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_footer" }, renderButtons()), /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      destroyOnClose: true,
      platform,
      mobileModalHeight: "auto",
      headerDivider: false,
      closable: true,
      maskClosable: true,
      onCancel: () => {
        setWechatModalOpen(false);
      },
      open: wechatModalOpen,
      footer: [
        /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { onClick: onCopyClick, block: true, type: "primary", size: "large" }, import_utils.locales.getText("wallet-pass-gallery.copyButton"))
      ]
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_footer-copy-title" }, import_utils.locales.getText("wallet-pass-gallery.copyTitle")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-wallet-pass-gallery_footer-copy-text" }, import_utils.locales.getText("wallet-pass-gallery.copyText"))
  ));
};
var footerButton_default = FooterButtons;
