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/components/pay/toB/store/hooks.ts
var hooks_exports = {};
__export(hooks_exports, {
  useOSPayment: () => useOSPayment,
  useOSPaymentEffect: () => useOSPaymentEffect,
  useToBPay: () => useToBPay
});
module.exports = __toCommonJS(hooks_exports);
var import_react = __toESM(require("react"));
var import_index = require("./index");
var import_useEngineContext = require("../../../../hooks/useEngineContext");
var import_utils = require("./utils");
var import_pisellos = require("../../../ticketBooking/hooks/pisellos");
var useToBPay = () => {
  const context = import_react.default.useContext(import_index.ToBPayContext);
  if (!context) {
    throw new Error("useToBPay must be used within a ToBPayProvider");
  }
  return context;
};
var useOSPayment = () => {
  const pisellOS = (0, import_useEngineContext.usePisellOS)();
  return pisellOS.getModule("payment");
};
var useOSPaymentEffect = () => {
  const pisellOS = (0, import_useEngineContext.usePisellOS)();
  const { setOrder, setPayMethodGroupList } = useToBPay();
  const { executeIfActive } = (0, import_pisellos.useActiveExecute)();
  const unlisten1 = (0, import_react.useRef)(null);
  const unlisten2 = (0, import_react.useRef)(null);
  const osPaymentEffectOn = () => {
    const wrappedOnPaymentMethodsChanged = executeIfActive(onPaymentMethodsChanged);
    const wrappedOnOrderChanged = executeIfActive(onOrderChanged);
    unlisten1.current = pisellOS.effects.on(
      "payment:onPaymentMethodsChanged",
      wrappedOnPaymentMethodsChanged
    );
    unlisten2.current = pisellOS.effects.on("payment:onOrderChanged", wrappedOnOrderChanged);
  };
  const osPaymentEffectOff = () => {
    var _a, _b;
    if (unlisten1.current) {
      (_a = unlisten1.current) == null ? void 0 : _a.call(unlisten1);
    }
    if (unlisten2.current) {
      (_b = unlisten2.current) == null ? void 0 : _b.call(unlisten2);
    }
  };
  const onPaymentMethodsChanged = async (res) => {
    console.log("tob_pay_onPaymentMethodsChanged", res);
    const { newMethods } = res || {};
    if ((newMethods == null ? void 0 : newMethods.length) > 0) {
      setPayMethodGroupList((0, import_utils.handlePayMethodList)(newMethods));
    } else {
      setPayMethodGroupList([]);
    }
  };
  const onOrderChanged = async (res) => {
    console.log("tob_pay_onOrderChanged", res);
    setOrder((res == null ? void 0 : res.order) || {});
  };
  return {
    osPaymentEffectOn,
    osPaymentEffectOff
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useOSPayment,
  useOSPaymentEffect,
  useToBPay
});
