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/pay.tsx
var pay_exports = {};
__export(pay_exports, {
  default: () => pay_default
});
module.exports = __toCommonJS(pay_exports);
var import_utils = require("@pisell/utils");
var import_react = __toESM(require("react"));
var import_fail = __toESM(require("./component/fail"));
var import_failCustom = __toESM(require("./component/fail/failCustom"));
var import_network = __toESM(require("./component/fail/network"));
var import_unknow = __toESM(require("./component/fail/unknow"));
var import_header = __toESM(require("./component/header"));
var import_helper = require("./helper");
var import_manual = __toESM(require("./component/manual"));
var import_signature = __toESM(require("./component/signature"));
var import_const = require("./const");
var import_manufacturer = require("./manufacturer");
var import_store = require("./store");
var import_index = require("./index.less");
var Mapping = {
  Fail: import_fail.default,
  Network: import_network.default,
  Manual: import_manual.default,
  Signature: import_signature.default,
  FailCustom: import_failCustom.default,
  UnknowFail: import_unknow.default
};
var Pay = ({
  api,
  onChange: onChangeStatus,
  className,
  terminal,
  net,
  isMobile,
  tyroUrl,
  tyroVersion,
  formatAmount = (number, precision, symbol) => `${symbol}${number}`,
  client
}) => {
  const data = (0, import_store.useContextData)();
  const dataRef = (0, import_react.useRef)(data);
  const dispatch = (0, import_store.useDispatch)();
  const ref = (0, import_react.useRef)(false);
  const statusRef = (0, import_react.useRef)(data.status);
  const comonentRef = (0, import_react.useRef)(data.component);
  const backupRef = (0, import_react.useRef)(data.backup);
  const clearRef = (0, import_react.useRef)(0);
  (0, import_react.useEffect)(() => {
    comonentRef.current = data.component;
  }, [data.component]);
  (0, import_react.useEffect)(() => {
    backupRef.current = data.backup;
  }, [data.backup]);
  (0, import_react.useEffect)(() => {
    dataRef.current = data;
  }, [data]);
  const Comm = Mapping[data.component];
  const onChange = (0, import_react.useCallback)(
    (status, params, other) => {
      if (status === import_const.StatusEnum.Success || status === import_const.StatusEnum.Fail) {
        dispatch((0, import_store.updateStatus)(status));
      }
      onChangeStatus(status, params, other);
    },
    [dispatch, onChangeStatus]
  );
  (0, import_react.useEffect)(() => {
    if (statusRef.current === "success") {
      const status = statusRef.current;
      const params = (0, import_helper.getPayParams)(dataRef.current, status);
      clearRef.current = setTimeout(() => {
        onChange(status, params);
      }, 1e3);
    }
  }, [onChange]);
  (0, import_react.useEffect)(() => {
    dispatch((0, import_store.updateState)({ net }));
    if (!ref.current) {
      ref.current = true;
      if (backupRef.current && net) {
        const { ...data2 } = backupRef.current;
        dispatch((0, import_store.backUpFree)({ key: Date.now() }));
        dispatch((0, import_store.updateState)({ net }));
        if (data2.status === "success") {
          const status = data2.status;
          const params = (0, import_helper.getPayParams)(dataRef.current, status);
          clearRef.current = setTimeout(() => {
            onChange(status, params);
          }, 1e3);
        }
      }
      return;
    }
    if (net) {
      const { ...data2 } = backupRef.current;
      dispatch((0, import_store.backUpFree)({ key: Date.now() }));
      dispatch((0, import_store.updateState)({ net }));
      if (data2.status === "success") {
        const status = data2.status;
        const params = (0, import_helper.getPayParams)(dataRef.current, status);
        clearRef.current = setTimeout(() => {
          onChange(status, params);
        }, 1e3);
      }
      return;
    }
    dispatch((0, import_store.updateState)({ form: "Network" }));
    dispatch((0, import_store.backUpProduction)({}));
    dispatch((0, import_store.updateState)({ net, status: "warn" }));
    dispatch((0, import_store.updateComponent)("Network"));
  }, [dispatch, net, onChange]);
  (0, import_react.useEffect)(() => {
    return () => {
      if (clearRef.current) {
        clearTimeout(clearRef.current);
      }
    };
  }, []);
  console.log("data =>>>>>> ", data);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, data.render ? /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: `pisell-lowcode__eft-pay-box ${className}`,
      style: data.type === "step" ? {} : { display: "none" }
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_header.default,
      {
        status: data.status,
        title: data.title,
        subTitle: data.subTitle
      }
    ),
    data.eftpos === "payo" ? /* @__PURE__ */ import_react.default.createElement(import_manufacturer.Payo, { api, onChange }, data.warn && data.status !== import_const.StatusEnum.Success && client === import_const.ClientEnum.Merchant ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-warn" }, data.warn) : null, data.status === import_const.StatusEnum.Success ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, import_utils.locales.getText("page.eftpos-pay.text.transaction.end")) : null) : null,
    data.eftpos === "tyro" ? /* @__PURE__ */ import_react.default.createElement(
      import_manufacturer.Tyro,
      {
        api,
        onChange,
        key: data.key,
        url: tyroUrl,
        version: tyroVersion
      }
    ) : null,
    data.eftpos === "windcave" ? /* @__PURE__ */ import_react.default.createElement(
      import_manufacturer.Windcave,
      {
        api,
        onChange,
        key: data.key,
        formatAmount
      }
    ) : null,
    data.eftpos === "linkly" ? /* @__PURE__ */ import_react.default.createElement(import_manufacturer.Linkly, { key: data.key, api, onChange }) : null
  ) : null, data.type === import_const.PayRouteEnum.Unset && Comm ? /* @__PURE__ */ import_react.default.createElement(
    Comm,
    {
      onChange,
      api,
      className,
      isMobile,
      formatAmount
    }
  ) : null);
};
var pay_default = Pay;
