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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/* eslint-disable no-nested-ternary */
import { locales } from '@pisell/utils';
import { Button } from 'antd';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { StatusEnum } from "../const";
import { useContextData, useDispatch, updateState, updateCustom, updateStatus } from "../store";
import { useStoreRef } from "../hooks";
import { StemTypeEnum, StepStatusEnum, getStepMapping } from "./const";

/**
 * @title: 按钮操作区
 * @description: 仅包含签名
 * @Author: hongbing.wang
 * @Date: 2024-08-20 12:33
 */
export var ReceiptAction = function ReceiptAction(_ref) {
  var api = _ref.api,
    onChange = _ref.onChange;
  var dispatch = useDispatch();
  var _useContextData = useContextData(),
    custom = _useContextData.custom,
    mode = _useContextData.mode;
  var _useStoreRef = useStoreRef(),
    numRef = _useStoreRef.numRef;
  var listRef = useRef(custom === null || custom === void 0 ? void 0 : custom.list);
  var _useState = useState((custom === null || custom === void 0 ? void 0 : custom.receiptLoading) || false),
    _useState2 = _slicedToArray(_useState, 2),
    receiptLoading = _useState2[0],
    setReceiptLoading = _useState2[1];
  var unloadRef = useRef(false);
  useEffect(function () {
    dispatch(updateCustom({
      receiptLoading: receiptLoading
    }));
  }, [receiptLoading]);
  var receiptPage = useMemo(function () {
    return (custom === null || custom === void 0 ? void 0 : custom.step) !== StepStatusEnum.Receipt ? '' : (custom === null || custom === void 0 ? void 0 : custom.receiptPage) || 'receipt';
  }, [custom === null || custom === void 0 ? void 0 : custom.step, custom === null || custom === void 0 ? void 0 : custom.receiptPage]);
  useEffect(function () {
    listRef.current = custom === null || custom === void 0 ? void 0 : custom.list;
  }, [custom === null || custom === void 0 ? void 0 : custom.list]);

  /**
   * @title: 确认签名
   * @description: 确认签名 不阻碍-查询
   * @return null
   * @Author: hongbing.wang
   * @Date: 2024-08-12 11:30
   */
  var onAccept = function onAccept() {
    // 确认签名
    dispatch(updateStatus(StatusEnum.Loading));
    dispatch(updateCustom({
      list: listRef.current.map(function (item) {
        return item.index === StepStatusEnum.Receipt ? _objectSpread(_objectSpread({}, item), {}, {
          status: 'resolve'
        }) : item;
      })
    }));
    dispatch(updateState({
      title: locales.getText('page.eftpos-pay.text.processing')
    }));
    setReceiptLoading(true);
    api.update({
      type: 'YES',
      number: numRef.current
    }).then(function (res) {
      if (unloadRef.current) {
        return res;
      }
      if (res && "".concat(res.code) === '200') {
        setReceiptLoading(false);
        var current = listRef.current.map(function (item) {
          if (Number(item === null || item === void 0 ? void 0 : item.index) < StepStatusEnum.Wait) {
            return _objectSpread(_objectSpread({}, item), {}, {
              status: 'resolve'
            });
          }
          return item;
        });
        var nextStep = getStepMapping(StepStatusEnum.Wait, {
          mode: mode
        });
        if (nextStep) {
          current.push(nextStep);
          if (nextStep.type === StemTypeEnum.Passive) {
            dispatch(updateStatus(StatusEnum.Question));
          }
          dispatch(updateState({
            title: nextStep.index < StepStatusEnum.Receipt ? locales.getText('page.eftpos-pay.text.loading')(mode) : nextStep.index === StepStatusEnum.Receipt ? locales.getText('page.eftpos-pay.text.waiting.signature') // 等待签名
            : locales.getText('page.eftpos-pay.text.loading')(mode)
          }));
        }
        dispatch(updateCustom({
          step: StepStatusEnum.Wait,
          list: current
        }));
        onChange(true);
        return res;
      }
      return Promise.reject(res);
    }).catch(function () {
      if (unloadRef.current) {
        return;
      }
      dispatch(updateCustom({
        list: listRef.current.map(function (item) {
          return item.index === StepStatusEnum.Receipt ? _objectSpread(_objectSpread({}, item), {}, {
            status: 'pedding'
          }) : item;
        })
      }));
      dispatch(updateState({
        title: locales.getText('page.eftpos-pay.text.waiting.signature') // 等待签名
      }));
      dispatch(updateStatus(StatusEnum.Question));
      setReceiptLoading(false);
      onChange(false);
    });
  };

  /**
   * @title: 后退
   * @description: 
   * @Author: hongbing.wang
   * @Date: 2024-08-20 12:34
   */
  var onBack = function onBack() {
    dispatch(updateState({
      title: locales.getText('page.eftpos-pay.text.waiting.signature'),
      // ''), // 等待签名
      status: StatusEnum.Question
    }));
    dispatch(updateCustom({
      receiptPage: 'receipt'
    }));
  };

  /**
   * @title: 取消签名
   * @description: 取消签名 不阻碍-状态查询
   * @return null
   * @Author: hongbing.wang
   * @Date: 2024-08-12 11:30
   */
  var onConfirm = function onConfirm() {
    // 取消签名
    dispatch(updateState({
      title: locales.getText('page.eftpos-pay.text.processing'),
      status: StatusEnum.Loading
    }));
    dispatch(updateCustom({
      list: listRef.current.map(function (item) {
        return item.index === StepStatusEnum.Receipt ? _objectSpread(_objectSpread({}, item), {}, {
          status: 'reject'
        }) : item;
      }),
      receiptPage: 'receipt'
    }));
    setReceiptLoading(true);
    api.update({
      type: 'NO',
      number: numRef.current
    }).then(function (res) {
      if (unloadRef.current) {
        return res;
      }
      setReceiptLoading(false);
      if (res && "".concat(res.code) === '200') {
        // 取消接口
        var current = listRef.current.map(function (item) {
          if (Number(item === null || item === void 0 ? void 0 : item.index) === StepStatusEnum.Receipt) {
            return _objectSpread(_objectSpread({}, item), {}, {
              resolve: item.reject,
              status: 'resolve'
            });
          }
          if (Number(item === null || item === void 0 ? void 0 : item.index) < StepStatusEnum.Wait) {
            return _objectSpread(_objectSpread({}, item), {}, {
              status: 'resolve'
            });
          }
          return item;
        });
        var nextStep = getStepMapping(StepStatusEnum.Wait, {
          mode: mode
        });
        if (nextStep) {
          nextStep.pedding = locales.getText('page.eftpos-pay.text.waiting.decline.signature');
          if (nextStep.type === StemTypeEnum.Passive) {
            dispatch(updateStatus(StatusEnum.Question));
          }
          dispatch(updateState({
            title: nextStep.index < StepStatusEnum.Receipt ? locales.getText('page.eftpos-pay.text.loading')(mode) : nextStep.index === StepStatusEnum.Receipt ? locales.getText('page.eftpos-pay.text.waiting.signature') // 等待签名
            : locales.getText('page.eftpos-pay.text.loading')(mode)
          }));
          current.push(nextStep);
        }
        dispatch(updateCustom({
          step: StepStatusEnum.Wait,
          list: current
        }));
        onChange(true);
        return res;
      }
      return Promise.reject(res);
    }).catch(function () {
      if (unloadRef.current) {
        return;
      }
      onChange(false);
      setReceiptLoading(false);
      dispatch(updateCustom({
        list: listRef.current.map(function (item) {
          return item.index === StepStatusEnum.Receipt ? _objectSpread(_objectSpread({}, item), {}, {
            status: 'pedding'
          }) : item;
        })
      }));
      dispatch(updateState({
        title: locales.getText('page.eftpos-pay.text.waiting.signature') // 等待签名
      }));
      dispatch(updateStatus(StatusEnum.Question));
      setReceiptLoading(false);
    });
  };

  /**
   * @title: 前往取消签名
   * @description: 前往取消签名确认流程
   * @Author: hongbing.wang
   * @Date: 2024-08-20 12:40
   */
  var onDecline = function onDecline() {
    dispatch(updateState({
      title: locales.getText('page.eftpos-pay.text.signature.title'),
      // 'page.eftpos-pay.text.waiting.signature'), // 等待签名
      status: StatusEnum.Warn,
      custom: _objectSpread(_objectSpread({}, custom), {}, {
        receiptPage: 'again'
      })
    }));
  };
  useEffect(function () {
    return function () {
      unloadRef.current = true;
    };
  }, []);
  // 在 签名操作状态(确认/取消) 时 取消按钮
  if (receiptLoading) {
    return /*#__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')));
  }
  // 在 签名 常规操作项
  if (receiptPage === 'receipt') {
    return /*#__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')));
  }

  // 在 取消签名 操作项目
  return /*#__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: onBack
  }, locales.getText('page.eftpos-pay.text.back')), /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    type: "primary",
    onClick: onConfirm,
    disabled: receiptLoading
  }, locales.getText('page.eftpos-pay.text.confirm.decline')));
};

/**
 * @title: 按钮操作区
 * @description: 不包含签名
 * @Author: hongbing.wang
 * @Date: 2024-08-20 12:32
 */
var Action = function Action(_ref2) {
  var index = _ref2.index,
    onCancel = _ref2.onCancel,
    disabled = _ref2.disabled;
  var dispatch = useDispatch();
  var _useContextData2 = useContextData(),
    custom = _useContextData2.custom,
    mode = _useContextData2.mode;

  /**
   * @title: 前往手动标记页
   * @Author: hongbing.wang
   * @Date: 2024-08-20 12:18
   */
  // const onManually = () => {
  //   dispatch(
  //     updateState({
  //       manual: {
  //         title: locales.getText('page.eftpos-pay.text.request.title')(mode),
  //         text: locales.getText('page.eftpos-pay.text.request.text')(mode),
  //       },
  //     })
  //   );
  //   dispatch(updateComponent('Manual', true));
  // };

  // 在选择卡片应用之前 可取消支付
  if (index < StepStatusEnum.Application) {
    return /*#__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: disabled
    }, locales.getText('page.eftpos-pay.text.cancel')));
  }

  // 在选择卡片应用,和签名之间,不可进行用户操作
  if (index >= StepStatusEnum.Application && index < StepStatusEnum.Receipt) {
    return /*#__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')));
  }

  // 签名期间 渲染 ReceiptAction 组件
  if (index === StepStatusEnum.Receipt) {
    return null;
  }

  // 其他场景 可前往 标记签名页
  return /*#__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')));
};
export default Action;