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/pinVerifyModal/index.tsx
var pinVerifyModal_exports = {};
__export(pinVerifyModal_exports, {
  default: () => pinVerifyModal_default
});
module.exports = __toCommonJS(pinVerifyModal_exports);
var import_react = __toESM(require("react"));
var import_Delete = __toESM(require("@pisell/icon/es/Delete"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_locales = __toESM(require("./locales"));
var import_utils2 = require("../../utils");
var import_index = require("./index.less");
var import_hooks = require("./hooks");
var PinVerifyModal = (props, ref) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(import_locales.default, ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en");
  import_utils2.request.setRequest((_d = (_c = context == null ? void 0 : context.appHelper) == null ? void 0 : _c.utils) == null ? void 0 : _d.request);
  const isTerminal = (_g = (_f = (_e = context == null ? void 0 : context.appHelper) == null ? void 0 : _e.utils) == null ? void 0 : _f.isTerminal) == null ? void 0 : _g.call(_f);
  const platform = (_i = (_h = context == null ? void 0 : context.appHelper) == null ? void 0 : _h.constants) == null ? void 0 : _i.platform;
  const [visible, setVisible] = (0, import_react.useState)(false);
  const modalParamsRef = (0, import_react.useRef)({});
  const [pin, setPin] = (0, import_react.useState)("");
  const { verifyPin, loading, verifyInfo, setVerifyInfo } = (0, import_hooks.usePinVerify)();
  const KeyboardItems = (0, import_react.useMemo)(() => {
    return [
      {
        value: 1,
        label: "1",
        disabled: false,
        type: "text"
      },
      {
        value: 2,
        label: "2",
        disabled: false,
        type: "text"
      },
      {
        value: 3,
        label: "3",
        disabled: false,
        type: "text"
      },
      {
        value: 4,
        label: "4",
        disabled: false,
        type: "text"
      },
      {
        value: 5,
        label: "5",
        disabled: false,
        type: "text"
      },
      {
        value: 6,
        label: "6",
        disabled: false,
        type: "text"
      },
      {
        value: 7,
        label: "7",
        disabled: false,
        type: "text"
      },
      {
        value: 8,
        label: "8",
        disabled: false,
        type: "text"
      },
      {
        value: 9,
        label: "9",
        disabled: false,
        type: "text"
      },
      {
        value: "delete",
        label: /* @__PURE__ */ import_react.default.createElement(import_Delete.default, { className: "virtual-keyboard-input-delete-icon" }),
        disabled: false,
        type: "text"
      },
      {
        value: 0,
        label: "0",
        disabled: false,
        type: "text"
      },
      {
        value: "verify",
        label: import_utils.locales.getText("pisell2.pin.verify.modal.keyboard.verify"),
        disabled: false,
        type: "primary"
      }
    ];
  }, [import_utils.locales]);
  const onOpen = (params) => {
    var _a2;
    modalParamsRef.current = params;
    setVerifyInfo({
      failed_count: (params == null ? void 0 : params.failed_count) || 0,
      check_max_count: (params == null ? void 0 : params.check_max_count) || 0
    });
    setVisible(true);
    (_a2 = props.onOpen) == null ? void 0 : _a2.call(props);
  };
  const onClose = () => {
    var _a2;
    modalParamsRef.current = {};
    setPin("");
    setVerifyInfo(null);
    setVisible(false);
    (_a2 = props.onClose) == null ? void 0 : _a2.call(props);
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: onOpen,
    close: onClose
  }));
  const handleChange = (e) => {
    if (e === "delete") {
      setPin(pin.slice(0, -1));
      return;
    } else if (e === "verify") {
      handleVerify();
      return;
    } else {
      if (pin.length < 4) {
        setPin(pin + e);
      }
    }
  };
  const handleVerify = () => {
    var _a2;
    if (loading) {
      return;
    }
    if (pin.length === 4) {
      const wallet_detail_id = (_a2 = modalParamsRef.current) == null ? void 0 : _a2.wallet_detail_id;
      verifyPin({ wallet_detail_id, pin }, (params) => {
        var _a3;
        (_a3 = props.onSuccess) == null ? void 0 : _a3.call(props, params);
        onClose();
      });
    } else {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("pisell2.pin.verify.modal.keyboard.title"),
        duration: 2
      });
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      platform: isTerminal ? "h5" : void 0,
      visible,
      title: import_utils.locales.getText("pisell2.pin.verify.modal.title"),
      headerDivider: false,
      footer: null,
      onClose,
      maskClosable: false,
      onCancel: onClose,
      destroyOnClose: true,
      mobileModalHeight: isTerminal ? void 0 : "auto"
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-user" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-user-avatar" }, /* @__PURE__ */ import_react.default.createElement(
      "img",
      {
        src: ((_j = modalParamsRef.current.customer) == null ? void 0 : _j.cover) || "https://file.mypisell.com/static/default_product.png",
        alt: "avatar",
        className: "pin-verify-modal-content-user-avatar-img"
      }
    )), /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-user-right" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-user-right-name" }, ((_k = modalParamsRef.current.customer) == null ? void 0 : _k.display_name) || ((_l = modalParamsRef.current.customer) == null ? void 0 : _l.nickname)), /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-user-right-wallet" }, import_utils.locales.getText("pisell2.pin.verify.modal.wallet"), modalParamsRef.current.wallet_code))), /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-keyboard" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-keyboard-title" }, import_utils.locales.getText("pisell2.pin.verify.modal.keyboard.title")), /* @__PURE__ */ import_react.default.createElement(
      import_materials.VirtualKeyboard,
      {
        style: { width: "300px" },
        keyboardProps: {
          keydown: false,
          items: KeyboardItems,
          onChange: handleChange
        },
        virtualKeyInputProps: {
          value: pin,
          onChange: (e) => {
            setPin(e.target.value);
          },
          placeholder: "0000",
          style: {
            height: "45px",
            fontSize: "36px"
          }
        }
      }
    ), (verifyInfo == null ? void 0 : verifyInfo.check_max_count) ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pin-verify-modal-content-keyboard-desc" }, import_utils.locales.getText("pisell2.pin.verify.modal.keyboard.desc"), verifyInfo == null ? void 0 : verifyInfo.remaining_count, "/", verifyInfo == null ? void 0 : verifyInfo.check_max_count) : null))
  );
};
var pinVerifyModal_default = (0, import_react.forwardRef)(PinVerifyModal);
