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/eftposPay/app.tsx
var app_exports = {};
__export(app_exports, {
  default: () => app_default
});
module.exports = __toCommonJS(app_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_amount = __toESM(require("./amount"));
var import_const = require("./const");
var import_device = __toESM(require("./device"));
var import_pay = __toESM(require("./pay"));
var import_store = require("./store");
var App = ({
  action,
  api,
  device_number,
  terminal,
  tyroUrl,
  tyroVersion,
  onChangeParams,
  isMobile,
  channel,
  formatAmount = (number, precision, symbol) => `${symbol}${number}`,
  ...props
}) => {
  const data = (0, import_store.useContextData)();
  const dispatch = (0, import_store.useDispatch)();
  const [current, setCurrent] = (0, import_react.useState)(action);
  const statusRef = (0, import_react.useRef)(data.status);
  const network = (0, import_react.useMemo)(
    () => typeof terminal.network === "boolean" ? terminal.network : true,
    [terminal == null ? void 0 : terminal.network]
  );
  const { online } = (0, import_ahooks.useNetwork)();
  const netRef = (0, import_react.useRef)([network, online]);
  const net = (0, import_react.useMemo)(() => {
    let networkChange = false, onlineChange = false;
    if (netRef.current[0] !== network) {
      netRef.current[0] = network;
      networkChange = true;
    }
    if (netRef.current[1] !== online) {
      netRef.current[1] = online;
      onlineChange = true;
    }
    if (networkChange) {
      return network;
    }
    return online;
  }, [network, online]);
  (0, import_react.useEffect)(() => {
    statusRef.current = data.status;
  }, [data.status]);
  const client = (0, import_react.useMemo)(() => {
    if (channel === "kiosk") {
      return import_const.ClientEnum.User;
    }
    return import_const.ClientEnum.Merchant;
  }, [channel]);
  (0, import_react.useEffect)(() => {
    dispatch((0, import_store.updateClient)(client));
  }, [client]);
  (0, import_react.useEffect)(() => {
    onChangeParams && onChangeParams({
      ...data,
      ...data.eftpos === "tyro" ? { custom: { ...data.custom, btnList: [], list: [] } } : {},
      form: import_const.RestoreEumn.Restore
    });
  }, [data, onChangeParams]);
  const onChange = (0, import_react.useCallback)(
    (status, params, other) => {
      if (status === "page" && typeof params === "string") {
        if (["amount", "deviceList", "pay"].includes(params)) {
          setCurrent(params);
          dispatch((0, import_store.updateState)({ action: params }));
        }
      }
      console.log("状态切换 = ", status, params, other);
      props.onChangeStatus(status, params, other);
    },
    []
  );
  const onClose = () => {
    onChange("fail", {
      number: data.number,
      uniquePaymentNumber: data.number,
      device: data.device,
      order_id: data.order_id
    });
  };
  if (current === import_const.RouteEnum.DeviceList) {
    return /* @__PURE__ */ import_react.default.createElement(
      import_device.default,
      {
        api,
        onChange,
        onClose,
        isMobile,
        className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
        net,
        device_number,
        formatAmount,
        client
      }
    );
  }
  if (current === import_const.RouteEnum.Amount) {
    return /* @__PURE__ */ import_react.default.createElement(
      import_amount.default,
      {
        onChange,
        onClose,
        formatAmount,
        isMobile,
        className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
        net,
        client
      }
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_pay.default,
    {
      api,
      tyroUrl,
      tyroVersion,
      onChange,
      className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
      terminal,
      isMobile,
      net,
      formatAmount,
      client
    }
  );
};
var app_default = App;
