function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { locales } from '@pisell/utils';
import { useCallback, useEffect, useRef } from 'react';
import { PayStatus } from "./const";
import { useContextData, useDispatch, updateComponent, updateSteps, updateState, updateCustom, updateKey } from "./store";
import { ClientEnum } from "./const";
var Max4004 = 1;

// 穿越 react deep
export var useStoreRef = function useStoreRef() {
  var data = useContextData();
  var order_id = data.order_id,
    device = data.device,
    pay = data.pay,
    number = data.number,
    mode = data.mode,
    net = data.net,
    status = data.status,
    symbol = data.symbol,
    amount = data.amount,
    eftpos = data.eftpos,
    client = data.client;
  var dataRef = useRef(data);

  // 设备信息
  var deviceRef = useRef(device);

  // 支付金额信息
  var payRef = useRef(pay);

  // 支付流水号
  var numRef = useRef(number);

  // 订单id
  var orderIdRef = useRef(order_id);

  // 退款 or 支付
  var modeRef = useRef(mode);

  // 页面状态 成功 or 失败 or 加载 ...
  var statusRef = useRef(status);

  // 网络状态
  var netRef = useRef(net);

  // 金额单位
  var symbolRef = useRef(symbol);

  // 原始支付金额
  var amountRef = useRef(amount);

  // 设备类型
  var eftposRef = useRef(eftpos);
  var clientRef = useRef(client);
  useEffect(function () {
    dataRef.current = data;
  }, [data]);
  useEffect(function () {
    deviceRef.current = device;
  }, [device]);
  useEffect(function () {
    payRef.current = pay;
  }, [pay]);
  useEffect(function () {
    numRef.current = number;
  }, [number]);
  useEffect(function () {
    orderIdRef.current = order_id;
  }, [order_id]);
  useEffect(function () {
    modeRef.current = mode;
  }, [mode]);
  useEffect(function () {
    statusRef.current = status;
  }, [status]);
  useEffect(function () {
    netRef.current = net;
  }, [net]);
  useEffect(function () {
    symbolRef.current = symbol;
  }, [symbol]);
  useEffect(function () {
    amountRef.current = amount;
  }, [amount]);
  useEffect(function () {
    eftposRef.current = eftpos;
  }, [eftpos]);
  useEffect(function () {
    clientRef.current = client;
  }, [client]);
  return {
    deviceRef: deviceRef,
    payRef: payRef,
    numRef: numRef,
    orderIdRef: orderIdRef,
    modeRef: modeRef,
    statusRef: statusRef,
    netRef: netRef,
    symbolRef: symbolRef,
    amountRef: amountRef,
    eftposRef: eftposRef,
    clientRef: clientRef,
    dataRef: dataRef
  };
};

/**
 * @title: 通用失败处理
 * @description:  固定步骤 错误处理逻辑
 * @return null
 * @Author: hongbing.wang
 * @Date: 2024-08-12 11:54
 */
export var useFail = function useFail(index) {
  var _useContextData = useContextData(),
    steps = _useContextData.steps,
    custom = _useContextData.custom;
  var _useStoreRef = useStoreRef(),
    modeRef = _useStoreRef.modeRef,
    clientRef = _useStoreRef.clientRef;
  var dispatch = useDispatch();
  var ref = useRef((custom === null || custom === void 0 ? void 0 : custom.retry) || 0);
  useEffect(function () {
    ref.current = (custom === null || custom === void 0 ? void 0 : custom.retry) || 0;
  }, [custom === null || custom === void 0 ? void 0 : custom.retry]);
  return useCallback(function (res) {
    var isMark = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    if (!res || !res.code) {
      // 去往未知处理页面
      dispatch(updateCustom({
        errorStatus: 'unknow' // unknow know
      }));
      dispatch(updateComponent('UnknowFail'));
      return;
    }
    var code = "".concat(res.code);
    console.log('code---->>>>>>>', code);
    if (code === PayStatus.Unknown) {
      // 前往报错页 不可标记
      steps[index].title = locales.getText('page.eftpos-pay.text.failed')(modeRef.current);
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.fail');
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.PayTimeout) {
      // 前往错误页  不可标记状态
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.fail.timeout');
      steps[index].title = locales.getText('page.eftpos-pay.text.failed')(modeRef.current);
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.Timeout) {
      if (isMark) {
        if (clientRef.current === ClientEnum.User) {
          steps[index].status = 'fail';
          steps[index].title = locales.getText('page.eftpos-pay.text.timedout.title');
          steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.eftpos.fail.other');
          steps[index].custom = steps[index].custom ? _objectSpread(_objectSpread({}, steps[index].custom), {}, {
            isPosNetworkError: true
          }) : {
            isPosNetworkError: true
          };
          dispatch(updateSteps(steps));
          dispatch(updateComponent('Fail'));
          dispatch(updateCustom({
            errorStatus: 'know' // unknow know
          }));
          return;
        }
        // 超时 前往标记页
        steps[index].status = 'fail';
        dispatch(updateSteps(steps));
        dispatch(updateState({
          manual: {
            title: locales.getText('page.eftpos-pay.text.timedout.title'),
            text: locales.getText('page.eftpos-pay.text.timedout.text')(modeRef.current)
          }
        }));
        dispatch(updateCustom({
          errorStatus: 'know' // unknow know
        }));
        dispatch(updateComponent('Manual'));
        return;
      }
      // 前往错误页  不可标记状态
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.pay.fail.timeout');
      steps[index].title = locales.getText('page.eftpos-pay.text.failed')(modeRef.current);
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.NoPay) {
      // 如果未查询到支付状态,则重新发起支付
      if (ref.current > Max4004) {
        // 前往错误页  不可标记状态
        dispatch(updateState({
          number: ''
        }));
        dispatch(updateCustom({
          retry: ref.current
        }));
        steps[index].title = locales.getText('page.eftpos-pay.text.failed')(modeRef.current);
        steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.fail');
        steps[index].status = 'fail';
        dispatch(updateSteps(steps));
        dispatch(updateComponent('Fail'));
        return;
      }

      // 重新支付 最多到重新发起2次
      ref.current += 1;
      dispatch(updateState({
        number: ''
      }));
      dispatch(updateCustom({
        retry: ref.current
      }));
      dispatch(updateKey(Date.now()));
      return;
    }
    if (code === PayStatus.PayOtherEftposFailed) {
      // 前往错误页 不可标记状态
      steps[index].title = locales.getText('page.eftpos-pay.text.transaction.fail.terminal.title');
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.fail.terminal');
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.PayCardErrorFailed) {
      // 前往错误页 不可标记状态
      steps[index].title = locales.getText('page.eftpos-pay.text.transaction.fail.cancel.title');
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.fail.cancel');
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.NoNetWork || code === PayStatus.SocketError) {
      // 前往错误页 可标记状态
      steps[index].title = locales.getText('page.eftpos-pay.text.interrupted');
      steps[index].warn = clientRef.current === ClientEnum.Merchant ? "".concat(locales.getText('page.eftpos-pay.text.transaction.eftpos.fail.title'), "\n").concat(locales.getText('page.eftpos-pay.text.transaction.eftpos.fail.power'), "\n").concat(locales.getText('page.eftpos-pay.text.transaction.eftpos.fail.device'), "\n").concat(locales.getText('page.eftpos-pay.text.transaction.eftpos.fail.network'), "\n").concat(locales.getText('page.eftpos-pay.text.transaction.eftpos.fail.if')(modeRef.current)) : locales.getText('page.eftpos-pay.text.transaction.eftpos.fail.other');
      steps[index].status = 'warn';
      steps[index].custom = steps[index].custom ? _objectSpread(_objectSpread({}, steps[index].custom), {}, {
        isPosNetworkError: true
      }) : {
        isPosNetworkError: true
      };
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.PayBankRefuses) {
      // 前往错误页  不可标记状态
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.back.declined.text');
      steps[index].title = locales.getText('page.eftpos-pay.text.transaction.back.declined.title');
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.SignatureDeclined) {
      // 前往错误页  不可标记状态
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.user.declined.text');
      steps[index].title = locales.getText('page.eftpos-pay.text.transaction.user.declined.title');
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.UserConfigError || code === PayStatus.PairingFailure) {
      // 前往错误页  不可标记状态
      steps[index].title = locales.getText('page.eftpos-pay.text.transaction.user.config.title');
      steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.user.config.text');
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }
    if (code === PayStatus.PaymentFailed) {
      // 前往错误页  不可标记状态
      steps[index].title = locales.getText('page.eftpos-pay.text.transaction.fail');
      steps[index].fail = (res === null || res === void 0 ? void 0 : res.message) || locales.getText('page.eftpos-pay.text.failed')(modeRef.current);
      steps[index].status = 'fail';
      dispatch(updateSteps(steps));
      dispatch(updateComponent('Fail'));
      dispatch(updateCustom({
        errorStatus: 'know' // unknow know
      }));
      return;
    }

    // 其他场景前往未知错误页
    steps[index].title = locales.getText('page.eftpos-pay.text.failed')(modeRef.current);
    steps[index].status = 'fail';
    steps[index].fail = locales.getText('page.eftpos-pay.text.transaction.fail');
    dispatch(updateSteps(steps));
    dispatch(updateCustom({
      errorStatus: 'unknow' // unknow know
    }));
    dispatch(updateComponent('UnknowFail'));
  }, [dispatch, index, steps]);
};