function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure " + obj); }
import { locales } from '@pisell/utils';
import React, { useEffect, useRef, useCallback, useMemo } from 'react';
import Fail from "./component/fail";
import FailCustom from "./component/fail/failCustom";
import Network from "./component/fail/network";
import UnknowFail from "./component/fail/unknow";
import Header from "./component/header";
import { getPayParams } from "./helper";
import Manual from "./component/manual";
import Signature from "./component/signature";
import { StatusEnum, PayRouteEnum, ClientEnum, EftposEnum } from "./const";
import { Payo, Windcave, Tyro, Linkly, Huifu, AdyenPos, AdyenScan, Mx51 } from "./manufacturer";
import { useContextData, useDispatch, updateStatus, backUpProduction, updateComponent, backUpFree, updateState } from "./store";
import "./index.less";
import payLog from "./utils/payLog";

// 定制组件 用于当前页面无法扩展场景
var Mapping = {
  Fail: Fail,
  Network: Network,
  Manual: Manual,
  Signature: Signature,
  FailCustom: FailCustom,
  UnknowFail: UnknowFail
};
/**
 * @title: 支付组件
 * @description:
 * @Author: hongbing.wang
 * @Date: 2024-08-20 14:59
 */
var Pay = function Pay(_ref) {
  var api = _ref.api,
    onChangeStatus = _ref.onChange,
    className = _ref.className,
    terminal = _ref.terminal,
    net = _ref.net,
    isMobile = _ref.isMobile,
    tyroUrl = _ref.tyroUrl,
    tyroVersion = _ref.tyroVersion,
    _ref$formatAmount = _ref.formatAmount,
    formatAmount = _ref$formatAmount === void 0 ? function (number, precision, symbol) {
      return "".concat(symbol).concat(number);
    } : _ref$formatAmount,
    client = _ref.client;
  var data = useContextData();
  var dataRef = useRef(data);
  var dispatch = useDispatch();
  var ref = useRef(false);
  var statusRef = useRef(data.status);
  var comonentRef = useRef(data.component);
  var backupRef = useRef(data.backup);
  var clearRef = useRef(null);
  useEffect(function () {
    payLog.log('支付组件初始化');
  }, []);
  useEffect(function () {
    comonentRef.current = data.component;
  }, [data.component]);
  useEffect(function () {
    backupRef.current = data.backup;
  }, [data.backup]);
  useEffect(function () {
    dataRef.current = data;
  }, [data]);
  var Comm = Mapping[data.component];

  /**
   * @title: 获取组件状态
   * @Author: hongbing.wang
   * @Date: 2024-08-20 15:00
   */
  var onChange = useCallback(function (status, params, other) {
    if (status === StatusEnum.Success || status === StatusEnum.Fail) {
      dispatch(updateStatus(status));
    }
    onChangeStatus(status, params, other);
  }, [dispatch, onChangeStatus]);

  // 主流程 已完成 的崩溃恢复
  useEffect(function () {
    if (statusRef.current === 'success') {
      var status = statusRef.current;
      var params = getPayParams(dataRef.current, status);
      clearRef.current = setTimeout(function () {
        onChange(status, params);
      }, 1000);
    }
  }, [onChange]);

  // 监控网络状态
  useEffect(function () {
    dispatch(updateState({
      net: net
    }));
    if (!ref.current) {
      ref.current = true;
      if (backupRef.current && net) {
        payLog.log('网络恢复：从备份恢复', {
          hasBackup: true
        });
        var _data = Object.assign({}, (_objectDestructuringEmpty(backupRef.current), backupRef.current));
        dispatch(backUpFree({
          key: Date.now()
        }));
        dispatch(updateState({
          net: net
        }));
        // 主流程 已完成 断网&崩溃  恢复
        if (_data.status === 'success') {
          var status = _data.status;
          var params = getPayParams(dataRef.current, status);
          clearRef.current = setTimeout(function () {
            onChange(status, params);
          }, 1000);
        }
      }
      return;
    }
    if (net) {
      payLog.log('网络恢复', {
        hasBackup: !!backupRef.current
      });
      var _data2 = Object.assign({}, (_objectDestructuringEmpty(backupRef.current), backupRef.current));
      dispatch(backUpFree({
        key: Date.now()
      }));
      dispatch(updateState({
        net: net
      }));
      if (_data2.status === 'success') {
        var _status = _data2.status;
        var _params = getPayParams(dataRef.current, _status);
        clearRef.current = setTimeout(function () {
          onChange(_status, _params);
        }, 1000);
      }
      return;
    }
    payLog.log('网络断开，备份数据');
    dispatch(updateState({
      form: 'Network'
    }));
    dispatch(backUpProduction({}));
    dispatch(updateState({
      net: net,
      status: 'warn'
    }));
    dispatch(updateComponent('Network')); // 前往 Network 页面
  }, [dispatch, net, onChange]);
  useEffect(function () {
    return function () {
      if (clearRef.current) {
        clearTimeout(clearRef.current);
        clearRef.current = null;
      }
    };
  }, []);
  console.log('data =>>>>>> ', data);
  var isShowHeader = useMemo(function () {
    var _isShow = true;
    // 初始状态时不显示header
    if (data.status === StatusEnum.Init) {
      _isShow = false;
    }
    // MX51 状态为 Question 时，不展示 Header 组件
    if (data.eftpos === 'mx51' && data.status === StatusEnum.Question) {
      _isShow = false;
    }
    // AdyenScan 扫码支付在非成功或失败状态时不显示header
    if ([EftposEnum.AdyenAlipay, EftposEnum.AdyenWechat].includes(data.eftpos) && ![StatusEnum.Success, StatusEnum.Fail].includes(data.status)) {
      _isShow = false;
    }
    return _isShow;
  }, [data.status, data.eftpos]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, data.render ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-box ".concat(className),
    style: data.type === 'step' ? {} : {
      display: 'none'
    }
  }, isShowHeader && /*#__PURE__*/React.createElement(Header, {
    status: data.status,
    title: data.title,
    subTitle: data.subTitle
  }), data.eftpos === 'payo' ? /*#__PURE__*/React.createElement(Payo, {
    api: api,
    onChange: onChange
  }, data.warn && data.status !== StatusEnum.Success && client === ClientEnum.Merchant ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-warn"
  }, data.warn) : null, data.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) : null, data.eftpos === 'tyro' ? /*#__PURE__*/React.createElement(Tyro, {
    api: api,
    onChange: onChange,
    key: data.key,
    url: tyroUrl,
    version: tyroVersion
  }) : null, data.eftpos === 'windcave' ? /*#__PURE__*/React.createElement(Windcave, {
    api: api,
    onChange: onChange,
    key: data.key,
    formatAmount: formatAmount
  }) : null, data.eftpos === 'linkly' ? /*#__PURE__*/React.createElement(Linkly, {
    key: data.key,
    api: api,
    onChange: onChange
  }) : null, data.eftpos === 'huifu' ? /*#__PURE__*/React.createElement(Huifu, {
    key: data.key,
    onChange: onChange,
    isNativeApp: isMobile
  }) : null, data.eftpos === EftposEnum.AdyenPos ? /*#__PURE__*/React.createElement(AdyenPos, {
    key: data.key,
    onChange: onChange,
    isNativeApp: isMobile
  }) : null, [EftposEnum.AdyenAlipay, EftposEnum.AdyenWechat].includes(data.eftpos) ? /*#__PURE__*/React.createElement(AdyenScan, {
    key: data.key,
    onChange: onChange,
    isNativeApp: isMobile
  }) : null, data.eftpos === 'mx51' ? /*#__PURE__*/React.createElement(Mx51, {
    key: data.key,
    onChange: onChange
  }) : null) : null, data.type === PayRouteEnum.Unset && Comm ? /*#__PURE__*/React.createElement(Comm, {
    onChange: onChange,
    api: api,
    className: className,
    isMobile: isMobile,
    formatAmount: formatAmount
  }) : null);
};
export default Pay;