var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/hooks.tsx
var hooks_exports = {};
__export(hooks_exports, {
  useFail: () => useFail,
  useStoreRef: () => useStoreRef
});
module.exports = __toCommonJS(hooks_exports);
var import_utils = require("@pisell/utils");
var import_react = require("react");
var import_const = require("./const");
var import_store = require("./store");
var import_const2 = require("./const");
var Max4004 = 1;
var useStoreRef = () => {
  const data = (0, import_store.useContextData)();
  const {
    order_id,
    device,
    pay,
    number,
    mode,
    net,
    status,
    symbol,
    amount,
    eftpos,
    client
  } = data;
  const dataRef = (0, import_react.useRef)(data);
  const deviceRef = (0, import_react.useRef)(device);
  const payRef = (0, import_react.useRef)(pay);
  const numRef = (0, import_react.useRef)(number);
  const orderIdRef = (0, import_react.useRef)(order_id);
  const modeRef = (0, import_react.useRef)(mode);
  const statusRef = (0, import_react.useRef)(status);
  const netRef = (0, import_react.useRef)(net);
  const symbolRef = (0, import_react.useRef)(symbol);
  const amountRef = (0, import_react.useRef)(amount);
  const eftposRef = (0, import_react.useRef)(eftpos);
  const clientRef = (0, import_react.useRef)(client);
  (0, import_react.useEffect)(() => {
    dataRef.current = data;
  }, [data]);
  (0, import_react.useEffect)(() => {
    deviceRef.current = device;
  }, [device]);
  (0, import_react.useEffect)(() => {
    payRef.current = pay;
  }, [pay]);
  (0, import_react.useEffect)(() => {
    numRef.current = number;
  }, [number]);
  (0, import_react.useEffect)(() => {
    orderIdRef.current = order_id;
  }, [order_id]);
  (0, import_react.useEffect)(() => {
    modeRef.current = mode;
  }, [mode]);
  (0, import_react.useEffect)(() => {
    statusRef.current = status;
  }, [status]);
  (0, import_react.useEffect)(() => {
    netRef.current = net;
  }, [net]);
  (0, import_react.useEffect)(() => {
    symbolRef.current = symbol;
  }, [symbol]);
  (0, import_react.useEffect)(() => {
    amountRef.current = amount;
  }, [amount]);
  (0, import_react.useEffect)(() => {
    eftposRef.current = eftpos;
  }, [eftpos]);
  (0, import_react.useEffect)(() => {
    clientRef.current = client;
  }, [client]);
  return {
    deviceRef,
    payRef,
    numRef,
    orderIdRef,
    modeRef,
    statusRef,
    netRef,
    symbolRef,
    amountRef,
    eftposRef,
    clientRef,
    dataRef
  };
};
var useFail = (index) => {
  const { steps, custom } = (0, import_store.useContextData)();
  const { modeRef, clientRef } = useStoreRef();
  const dispatch = (0, import_store.useDispatch)();
  const ref = (0, import_react.useRef)((custom == null ? void 0 : custom.retry) || 0);
  (0, import_react.useEffect)(() => {
    ref.current = (custom == null ? void 0 : custom.retry) || 0;
  }, [custom == null ? void 0 : custom.retry]);
  return (0, import_react.useCallback)(
    (res, isMark = false) => {
      if (!res || !res.code) {
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "unknow"
            // unknow know
          })
        );
        dispatch((0, import_store.updateComponent)("UnknowFail"));
        return;
      }
      const [code] = [`${res.code}`];
      console.log("code---->>>>>>>", code);
      if (code === import_const.PayStatus.Unknown) {
        steps[index].title = import_utils.locales.getText("page.eftpos-pay.text.failed")(
          modeRef.current
        );
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.fail"
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.PayTimeout) {
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.fail.timeout"
        );
        steps[index].title = import_utils.locales.getText("page.eftpos-pay.text.failed")(
          modeRef.current
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.Timeout) {
        if (isMark) {
          if (clientRef.current === import_const2.ClientEnum.User) {
            steps[index].status = "fail";
            steps[index].title = import_utils.locales.getText(
              "page.eftpos-pay.text.timedout.title"
            );
            steps[index].fail = import_utils.locales.getText(
              "page.eftpos-pay.text.transaction.eftpos.fail.other"
            );
            steps[index].custom = steps[index].custom ? { ...steps[index].custom, isPosNetworkError: true } : { isPosNetworkError: true };
            dispatch((0, import_store.updateSteps)(steps));
            dispatch((0, import_store.updateComponent)("Fail"));
            dispatch(
              (0, import_store.updateCustom)({
                errorStatus: "know"
                // unknow know
              })
            );
            return;
          }
          steps[index].status = "fail";
          dispatch((0, import_store.updateSteps)(steps));
          dispatch(
            (0, import_store.updateState)({
              manual: {
                title: import_utils.locales.getText("page.eftpos-pay.text.timedout.title"),
                text: import_utils.locales.getText("page.eftpos-pay.text.timedout.text")(
                  modeRef.current
                )
              }
            })
          );
          dispatch(
            (0, import_store.updateCustom)({
              errorStatus: "know"
              // unknow know
            })
          );
          dispatch((0, import_store.updateComponent)("Manual"));
          return;
        }
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.pay.fail.timeout"
        );
        steps[index].title = import_utils.locales.getText("page.eftpos-pay.text.failed")(
          modeRef.current
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.NoPay) {
        if (ref.current > Max4004) {
          dispatch(
            (0, import_store.updateState)({
              number: ""
            })
          );
          dispatch(
            (0, import_store.updateCustom)({
              retry: ref.current
            })
          );
          steps[index].title = import_utils.locales.getText("page.eftpos-pay.text.failed")(
            modeRef.current
          );
          steps[index].fail = import_utils.locales.getText(
            "page.eftpos-pay.text.transaction.fail"
          );
          steps[index].status = "fail";
          dispatch((0, import_store.updateSteps)(steps));
          dispatch((0, import_store.updateComponent)("Fail"));
          return;
        }
        ref.current += 1;
        dispatch(
          (0, import_store.updateState)({
            number: ""
          })
        );
        dispatch(
          (0, import_store.updateCustom)({
            retry: ref.current
          })
        );
        dispatch((0, import_store.updateKey)(Date.now()));
        return;
      }
      if (code === import_const.PayStatus.PayOtherEftposFailed) {
        steps[index].title = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.fail.terminal.title"
        );
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.fail.terminal"
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.PayCardErrorFailed) {
        steps[index].title = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.fail.cancel.title"
        );
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.fail.cancel"
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.NoNetWork || code === import_const.PayStatus.SocketError) {
        steps[index].title = import_utils.locales.getText(
          "page.eftpos-pay.text.interrupted"
        );
        steps[index].warn = clientRef.current === import_const2.ClientEnum.Merchant ? `${import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.eftpos.fail.title"
        )}
${import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.eftpos.fail.power"
        )}
${import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.eftpos.fail.device"
        )}
${import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.eftpos.fail.network"
        )}
${import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.eftpos.fail.if"
        )(modeRef.current)}` : import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.eftpos.fail.other"
        );
        steps[index].status = "warn";
        steps[index].custom = steps[index].custom ? { ...steps[index].custom, isPosNetworkError: true } : { isPosNetworkError: true };
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.PayBankRefuses) {
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.back.declined.text"
        );
        steps[index].title = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.back.declined.title"
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.SignatureDeclined) {
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.user.declined.text"
        );
        steps[index].title = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.user.declined.title"
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      if (code === import_const.PayStatus.UserConfigError || code === import_const.PayStatus.PairingFailure) {
        steps[index].title = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.user.config.title"
        );
        steps[index].fail = import_utils.locales.getText(
          "page.eftpos-pay.text.transaction.user.config.text"
        );
        steps[index].status = "fail";
        dispatch((0, import_store.updateSteps)(steps));
        dispatch((0, import_store.updateComponent)("Fail"));
        dispatch(
          (0, import_store.updateCustom)({
            errorStatus: "know"
            // unknow know
          })
        );
        return;
      }
      steps[index].title = import_utils.locales.getText("page.eftpos-pay.text.failed")(
        modeRef.current
      );
      steps[index].status = "fail";
      steps[index].fail = import_utils.locales.getText(
        "page.eftpos-pay.text.transaction.fail"
      );
      dispatch((0, import_store.updateSteps)(steps));
      dispatch(
        (0, import_store.updateCustom)({
          errorStatus: "unknow"
          // unknow know
        })
      );
      dispatch((0, import_store.updateComponent)("UnknowFail"));
    },
    [dispatch, index, steps]
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFail,
  useStoreRef
});
