import { ArrowIcon as a, ArrowIconStyles as o } from "./accelbyte-widgets125.mjs";
import { Spinner as i, SpinnerStyle as u } from "./accelbyte-widgets154.mjs";
import r from "classnames";
import e from "react";
import { Trans as s } from "react-i18next";
import "./accelbyte-widgets548.mjs";
class B extends e.Component {
  constructor(t) {
    super(t), this.onMouseClickOutside = (n) => {
      n.composedPath().includes(this.buttonRef.current) || this.state.open && this.setState({ open: !1 });
    }, this.state = { open: !1 }, this.buttonRef = e.createRef();
  }
  componentDidMount() {
    window.addEventListener("click", this.onMouseClickOutside);
  }
  componentWillUnmount() {
    window.removeEventListener("click", this.onMouseClickOutside);
  }
  renderDropdownBuyNowButton() {
    const { paymentOptions: t } = this.props;
    return /* @__PURE__ */ e.createElement("div", { className: r("purchaseButtonContainer", this.props.className) }, /* @__PURE__ */ e.createElement(
      "button",
      {
        ref: this.buttonRef,
        className: r("purchaseButton btn big btn-primary"),
        disabled: this.props.disabled,
        onClick: () => this.setState({ open: !this.state.open })
      },
      /* @__PURE__ */ e.createElement("span", null, /* @__PURE__ */ e.createElement(s, { i18nKey: "CatalogPurchaseWidget.purchaseButton" }, "Buy Now")),
      /* @__PURE__ */ e.createElement(a, { className: r("arrow", this.state.open ? o.up : o.down) })
    ), /* @__PURE__ */ e.createElement("ul", { className: r("purchaseDropdown", this.state.open && "open") }, t.map((n) => /* @__PURE__ */ e.createElement(
      "li",
      {
        className: "purchaseDropdownOption",
        onClick: () => this.props.onPurchase(n.regionData),
        key: n.regionData.currencyCode
      },
      /* @__PURE__ */ e.createElement(s, { i18nKey: "CatalogPurchaseWidget.purchaseDropdownOption" }, "Buy for..."),
      /* @__PURE__ */ e.createElement("span", null, n.currencySymbol, " ", n.finalPrice)
    ))));
  }
  renderSingleBuyNowButton() {
    const { paymentOptions: t } = this.props;
    return /* @__PURE__ */ e.createElement(
      "button",
      {
        className: r(this.props.className, "purchaseButton btn big btn-primary"),
        disabled: this.props.disabled,
        onClick: () => this.props.onPurchase(t[0].regionData)
      },
      /* @__PURE__ */ e.createElement(s, { i18nKey: "CatalogPurchaseWidget.purchaseButton" }, "Buy Now")
    );
  }
  renderLoadingButton() {
    return /* @__PURE__ */ e.createElement("button", { className: r(this.props.className, "purchaseButton btn big btn-primary"), disabled: !0 }, /* @__PURE__ */ e.createElement(s, { i18nKey: "CatalogPurchaseWidget.purchaseButtonPurchasing" }, "Purchasing..."), /* @__PURE__ */ e.createElement(i, { className: r(u.inline, "purchaseButtonSpinner") }));
  }
  render() {
    const { paymentOptions: t, isPurchasing: n } = this.props;
    return n ? this.renderLoadingButton() : t.length === 1 ? this.renderSingleBuyNowButton() : t.length > 1 ? this.renderDropdownBuyNowButton() : null;
  }
}
export {
  B as PurchaseButton
};
