var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/windcave/receiptAction.tsx
var receiptAction_exports = {};
__export(receiptAction_exports, {
  ReceiptAction: () => ReceiptAction,
  default: () => receiptAction_default
});
module.exports = __toCommonJS(receiptAction_exports);
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_const = require("../const");
var import_store = require("../store");
var import_hooks = require("../hooks");
var import_const2 = require("./const");
var ReceiptAction = ({
  api,
  onChange
}) => {
  const dispatch = (0, import_store.useDispatch)();
  const { custom, mode } = (0, import_store.useContextData)();
  const { numRef } = (0, import_hooks.useStoreRef)();
  const listRef = (0, import_react.useRef)(custom == null ? void 0 : custom.list);
  const [receiptLoading, setReceiptLoading] = (0, import_react.useState)(
    (custom == null ? void 0 : custom.receiptLoading) || false
  );
  const unloadRef = (0, import_react.useRef)(false);
  (0, import_react.useEffect)(() => {
    dispatch(
      (0, import_store.updateCustom)({
        receiptLoading
      })
    );
  }, [receiptLoading]);
  const receiptPage = (0, import_react.useMemo)(
    () => (custom == null ? void 0 : custom.step) !== import_const2.StepStatusEnum.Receipt ? "" : (custom == null ? void 0 : custom.receiptPage) || "receipt",
    [custom == null ? void 0 : custom.step, custom == null ? void 0 : custom.receiptPage]
  );
  (0, import_react.useEffect)(() => {
    listRef.current = custom == null ? void 0 : custom.list;
  }, [custom == null ? void 0 : custom.list]);
  const onAccept = () => {
    dispatch((0, import_store.updateStatus)(import_const.StatusEnum.Loading));
    dispatch(
      (0, import_store.updateCustom)({
        list: listRef.current.map(
          (item) => item.index === import_const2.StepStatusEnum.Receipt ? { ...item, status: "resolve" } : item
        )
      })
    );
    dispatch(
      (0, import_store.updateState)({
        title: import_utils.locales.getText("page.eftpos-pay.text.processing")
      })
    );
    setReceiptLoading(true);
    api.update({
      type: "YES",
      number: numRef.current
    }).then((res) => {
      if (unloadRef.current) {
        return res;
      }
      if (res && `${res.code}` === "200") {
        setReceiptLoading(false);
        const current = listRef.current.map((item) => {
          if (Number(item == null ? void 0 : item.index) < import_const2.StepStatusEnum.Wait) {
            return {
              ...item,
              status: "resolve"
            };
          }
          return item;
        });
        const nextStep = (0, import_const2.getStepMapping)(import_const2.StepStatusEnum.Wait, { mode });
        if (nextStep) {
          current.push(nextStep);
          if (nextStep.type === import_const2.StemTypeEnum.Passive) {
            dispatch((0, import_store.updateStatus)(import_const.StatusEnum.Question));
          }
          dispatch(
            (0, import_store.updateState)({
              title: nextStep.index < import_const2.StepStatusEnum.Receipt ? import_utils.locales.getText("page.eftpos-pay.text.loading")(mode) : nextStep.index === import_const2.StepStatusEnum.Receipt ? import_utils.locales.getText("page.eftpos-pay.text.waiting.signature") : import_utils.locales.getText("page.eftpos-pay.text.loading")(mode)
            })
          );
        }
        dispatch(
          (0, import_store.updateCustom)({
            step: import_const2.StepStatusEnum.Wait,
            list: current
          })
        );
        onChange(true);
        return res;
      }
      return Promise.reject(res);
    }).catch(() => {
      if (unloadRef.current) {
        return;
      }
      dispatch(
        (0, import_store.updateCustom)({
          list: listRef.current.map(
            (item) => item.index === import_const2.StepStatusEnum.Receipt ? { ...item, status: "pedding" } : item
          )
        })
      );
      dispatch(
        (0, import_store.updateState)({
          title: import_utils.locales.getText("page.eftpos-pay.text.waiting.signature")
          // 等待签名
        })
      );
      dispatch((0, import_store.updateStatus)(import_const.StatusEnum.Question));
      setReceiptLoading(false);
      onChange(false);
    });
  };
  const onBack = () => {
    dispatch(
      (0, import_store.updateState)({
        title: import_utils.locales.getText("page.eftpos-pay.text.waiting.signature"),
        // ''), // 等待签名
        status: import_const.StatusEnum.Question
      })
    );
    dispatch((0, import_store.updateCustom)({ receiptPage: "receipt" }));
  };
  const onConfirm = () => {
    dispatch(
      (0, import_store.updateState)({
        title: import_utils.locales.getText("page.eftpos-pay.text.processing"),
        status: import_const.StatusEnum.Loading
      })
    );
    dispatch(
      (0, import_store.updateCustom)({
        list: listRef.current.map(
          (item) => item.index === import_const2.StepStatusEnum.Receipt ? { ...item, status: "reject" } : item
        ),
        receiptPage: "receipt"
      })
    );
    setReceiptLoading(true);
    api.update({
      type: "NO",
      number: numRef.current
    }).then((res) => {
      if (unloadRef.current) {
        return res;
      }
      setReceiptLoading(false);
      if (res && `${res.code}` === "200") {
        const current = listRef.current.map((item) => {
          if (Number(item == null ? void 0 : item.index) === import_const2.StepStatusEnum.Receipt) {
            return {
              ...item,
              resolve: item.reject,
              status: "resolve"
            };
          }
          if (Number(item == null ? void 0 : item.index) < import_const2.StepStatusEnum.Wait) {
            return {
              ...item,
              status: "resolve"
            };
          }
          return item;
        });
        const nextStep = (0, import_const2.getStepMapping)(import_const2.StepStatusEnum.Wait, { mode });
        if (nextStep) {
          nextStep.pedding = import_utils.locales.getText(
            "page.eftpos-pay.text.waiting.decline.signature"
          );
          if (nextStep.type === import_const2.StemTypeEnum.Passive) {
            dispatch((0, import_store.updateStatus)(import_const.StatusEnum.Question));
          }
          dispatch(
            (0, import_store.updateState)({
              title: nextStep.index < import_const2.StepStatusEnum.Receipt ? import_utils.locales.getText("page.eftpos-pay.text.loading")(mode) : nextStep.index === import_const2.StepStatusEnum.Receipt ? import_utils.locales.getText("page.eftpos-pay.text.waiting.signature") : import_utils.locales.getText("page.eftpos-pay.text.loading")(mode)
            })
          );
          current.push(nextStep);
        }
        dispatch(
          (0, import_store.updateCustom)({
            step: import_const2.StepStatusEnum.Wait,
            list: current
          })
        );
        onChange(true);
        return res;
      }
      return Promise.reject(res);
    }).catch(() => {
      if (unloadRef.current) {
        return;
      }
      onChange(false);
      setReceiptLoading(false);
      dispatch(
        (0, import_store.updateCustom)({
          list: listRef.current.map(
            (item) => item.index === import_const2.StepStatusEnum.Receipt ? { ...item, status: "pedding" } : item
          )
        })
      );
      dispatch(
        (0, import_store.updateState)({
          title: import_utils.locales.getText("page.eftpos-pay.text.waiting.signature")
          // 等待签名
        })
      );
      dispatch((0, import_store.updateStatus)(import_const.StatusEnum.Question));
      setReceiptLoading(false);
    });
  };
  const onDecline = () => {
    dispatch(
      (0, import_store.updateState)({
        title: import_utils.locales.getText("page.eftpos-pay.text.signature.title"),
        // 'page.eftpos-pay.text.waiting.signature'), // 等待签名
        status: import_const.StatusEnum.Warn,
        custom: {
          ...custom,
          receiptPage: "again"
        }
      })
    );
  };
  (0, import_react.useEffect)(
    () => () => {
      unloadRef.current = true;
    },
    []
  );
  if (receiptLoading) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        block: true,
        className: "pisell-lowcode__eft-pay-step-item-btn",
        disabled: true
      },
      import_utils.locales.getText("page.eftpos-pay.text.leave")
    ));
  }
  if (receiptPage === "receipt") {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        block: true,
        className: "pisell-lowcode__eft-pay-step-item-btn",
        onClick: onDecline
      },
      import_utils.locales.getText("page.eftpos-pay.text.decline")
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        block: true,
        type: "primary",
        className: "pisell-lowcode__eft-pay-step-item-btn",
        onClick: onAccept
      },
      import_utils.locales.getText("page.eftpos-pay.text.accept")
    ));
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      block: true,
      className: "pisell-lowcode__eft-pay-step-item-btn",
      onClick: onBack
    },
    import_utils.locales.getText("page.eftpos-pay.text.back")
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      block: true,
      className: "pisell-lowcode__eft-pay-step-item-btn",
      type: "primary",
      onClick: onConfirm,
      disabled: receiptLoading
    },
    import_utils.locales.getText("page.eftpos-pay.text.confirm.decline")
  ));
};
var Action = ({
  index,
  onCancel,
  disabled
}) => {
  const dispatch = (0, import_store.useDispatch)();
  const { custom, mode } = (0, import_store.useContextData)();
  if (index < import_const2.StepStatusEnum.Application) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        block: true,
        className: "pisell-lowcode__eft-pay-step-item-btn",
        onClick: onCancel,
        disabled
      },
      import_utils.locales.getText("page.eftpos-pay.text.cancel")
    ));
  }
  if (index >= import_const2.StepStatusEnum.Application && index < import_const2.StepStatusEnum.Receipt) {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        block: true,
        className: "pisell-lowcode__eft-pay-step-item-btn",
        disabled: true
      },
      import_utils.locales.getText("page.eftpos-pay.text.leave")
    ));
  }
  if (index === import_const2.StepStatusEnum.Receipt) {
    return null;
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { block: true, className: "pisell-lowcode__eft-pay-step-item-btn", disabled: true }, import_utils.locales.getText("page.eftpos-pay.text.leave")));
};
var receiptAction_default = Action;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ReceiptAction
});
