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 React, { useEffect, useRef } from 'react';
import { Button } from 'antd';
import { locales } from '@pisell/utils';
import { useContextData, useDispatch, updateCustom, updateState, updateNextStep } from "../store";
import { useFail, useStoreRef } from "../hooks";
import useEngineContext from "../../../hooks/useEngineContext";
import { request } from "../../../utils";
import Step from "../component/step/step";
import { StatusEnum } from "../const";
import { useLinklyCancel, useLinklyPayOrRefund, useTimeResultQuery, useLinklySignature } from "./hooks";
import { LinklyActionStatus } from "./const";
import { getPayParams } from "../helper";
var Linkly = function Linkly(_ref) {
  var api = _ref.api,
    onChange = _ref.onChange;
  // 设置request
  var context = useEngineContext();
  var utils = context.appHelper.utils || {};
  request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
  var _useContextData = useContextData(),
    custom = _useContextData.custom,
    status = _useContextData.status;
  var _useStoreRef = useStoreRef(),
    statusRef = _useStoreRef.statusRef,
    netRef = _useStoreRef.netRef,
    modeRef = _useStoreRef.modeRef,
    amountRef = _useStoreRef.amountRef,
    orderIdRef = _useStoreRef.orderIdRef,
    numRef = _useStoreRef.numRef,
    deviceRef = _useStoreRef.deviceRef,
    payRef = _useStoreRef.payRef;
  var dispatch = useDispatch();
  var stepRef = useRef(custom === null || custom === void 0 ? void 0 : custom.step);
  var fail = useFail(0);
  var failRef = useRef(fail);
  var initRef = useRef(false);
  var controllerRef = useRef();
  var unloadRef = useRef(false);
  var clearRef = useRef();

  // 检查是否可以继续处理接口返回结果，无网 或 已卸载 则不进行处理
  var handleCheck = function handleCheck() {
    return !(unloadRef.current || !netRef.current);
  };
  var handleLinklySuccess = function handleLinklySuccess(res) {
    var resData = (res === null || res === void 0 ? void 0 : res.data) || {};
    var receipt = Array.isArray(resData === null || resData === void 0 ? void 0 : resData.receipt) ? resData.receipt : [resData.receipt];
    receipt = receipt.filter(function (item) {
      return typeof item === 'string';
    }).filter(Boolean);
    dispatch(updateNextStep());
    // 将所有步骤状态设为完成 resolve
    dispatch(updateCustom({
      card_reader_surcharge: (resData === null || resData === void 0 ? void 0 : resData.card_reader_surcharge) || 0,
      receipt: receipt
    }));

    // 更新支付状态与文案
    dispatch(updateState({
      status: 'success',
      title: locales.getText('page.eftpos-pay.text.successful')(modeRef.current)
    }));
    clearRef.current = setTimeout(function () {
      var _deviceRef$current;
      var params = getPayParams({
        mode: modeRef.current,
        amount: amountRef.current,
        order_id: orderIdRef.current,
        number: numRef.current,
        device: deviceRef.current,
        pay: payRef.current,
        custom: {
          card_reader_surcharge: (resData === null || resData === void 0 ? void 0 : resData.card_reader_surcharge) || 0,
          receipt: receipt
        }
      }, 'success');
      onChange('success', _objectSpread(_objectSpread({}, params), {}, {
        receipt: (_deviceRef$current = deviceRef.current) !== null && _deviceRef$current !== void 0 && (_deviceRef$current = _deviceRef$current.setting) !== null && _deviceRef$current !== void 0 && _deviceRef$current.receipts ? [] : receipt
      }));
    }, 1000);
  };
  var _useLinklyPayOrRefund = useLinklyPayOrRefund(handleCheck),
    payOrRefund = _useLinklyPayOrRefund.payOrRefund;
  var _useLinklyCancel = useLinklyCancel(handleCheck),
    linklyCancel = _useLinklyCancel.linklyCancel;
  var _useTimeResultQuery = useTimeResultQuery(api, handleCheck, handleLinklySuccess),
    resultQuery = _useTimeResultQuery.run,
    stopResultQuery = _useTimeResultQuery.stop;
  var _useLinklySignature = useLinklySignature(handleCheck),
    linklySignature = _useLinklySignature.linklySignature;
  useEffect(function () {
    stepRef.current = custom === null || custom === void 0 ? void 0 : custom.step;
  }, [custom === null || custom === void 0 ? void 0 : custom.step]);
  useEffect(function () {
    failRef.current = fail;
  }, [fail]);
  useEffect(function () {
    if (statusRef.current === StatusEnum.Success || statusRef.current === StatusEnum.Fail) {
      return;
    }
    if (initRef.current) {
      return;
    }
    initRef.current = true;

    // 存在支付流水号
    if (numRef.current) {
      resultQuery();
      return;
    }
    payOrRefund(resultQuery, handleLinklySuccess);
  }, []);
  useEffect(function () {
    return function () {
      var _controllerRef$curren;
      (_controllerRef$curren = controllerRef.current) === null || _controllerRef$curren === void 0 || _controllerRef$curren.abort();
      unloadRef.current = true;
      if (clearRef.current) {
        clearTimeout(clearRef.current);
      }
    };
  }, []);

  /**
   * 取消支付
   */
  var onCancel = function onCancel() {
    stopResultQuery();
    linklyCancel(resultQuery);
  };

  /**
   * 拒绝签名
   */
  var onDecline = function onDecline() {
    stopResultQuery();
    linklySignature(2, resultQuery);
  };

  /**
   * 接受签名
   */
  var onAccept = function onAccept() {
    stopResultQuery();
    linklySignature(3, resultQuery);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-body"
  }, ((custom === null || custom === void 0 ? void 0 : custom.list) || []).map(function (item) {
    return /*#__PURE__*/React.createElement(Step, {
      key: item.index,
      pedding: item.pedding,
      resolve: item.resolve,
      reject: item.reject,
      status: item.status
    });
  }), status === StatusEnum.Loading ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip"
  }, locales.getText('page.eftpos-pay.text.processing')) : null, status === StatusEnum.Question ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip"
  }, locales.getText('page.eftpos-pay.text.receipt.again.title')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip"
  }, locales.getText('page.eftpos-pay.text.receipt.again.text'))) : null, status === StatusEnum.Success ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip"
  }, locales.getText('page.eftpos-pay.text.transaction.end')) : null), (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === LinklyActionStatus.Cancel ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item-btns"
  }, /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onCancel,
    disabled: !!(custom !== null && custom !== void 0 && custom.buttonDisabled)
  }, locales.getText('page.eftpos-pay.text.cancel'))) : null, (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === LinklyActionStatus.CancelWaiting || (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === LinklyActionStatus.SignatureWaiting ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item-btns"
  }, /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    disabled: true
  }, locales.getText('page.eftpos-pay.text.leave'))) : null, (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === LinklyActionStatus.Signature ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item-btns"
  }, /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onDecline
  }, locales.getText('page.eftpos-pay.text.decline')), /*#__PURE__*/React.createElement(Button, {
    block: true,
    type: "primary",
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onAccept
  }, locales.getText('page.eftpos-pay.text.accept'))) : null);
};
export default Linkly;