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/pay/toC/PaymentMethods/MWCreditCard/Add/index.tsx
var Add_exports = {};
__export(Add_exports, {
  default: () => Add_default
});
module.exports = __toCommonJS(Add_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_model = require("../../../model");
var import_index = require("./index.less");
var formatName = (card_name) => {
  let _name = card_name || "";
  _name = _name.trim();
  if (!_name) {
    return {
      text: "",
      error: import_utils.locales.getText("tocPay.text.enterName")
    };
  }
  _name = _name.replace("-", "");
  _name = _name.replace(/\s+/g, " ");
  let _arr = _name.split(" ");
  if (_arr.length == 0) {
    return {
      text: "",
      error: import_utils.locales.getText("tocPay.text.enterValidName")
    };
  }
  if (_arr.length == 1) {
    _name = _name;
    return {
      text: _name,
      error: ""
    };
  } else {
    _name = _arr[0] + " " + _arr[_arr.length - 1];
    return {
      text: _name,
      error: ""
    };
  }
};
var AddMWCreditCard = (props, ref) => {
  const { onCreate, shopName = "", btnLoading = false } = props;
  const [open, setOpen] = (0, import_react.useState)(false);
  const [form] = import_antd.Form.useForm();
  const { state } = (0, import_react.useContext)(import_model.Context);
  const init = async () => {
    onReset();
    setOpen(true);
  };
  const onCancel = () => {
    setOpen(false);
    onReset();
  };
  const _checkValue = (values) => {
    if (!values.card_number) {
      console.log(import_utils.locales.getText("tocPay.text.enterCardNumber"));
      return;
    }
    if (values.card_number.replace(/\s/g, "").length < 16) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("tocPay.text.enterValidCardNumber"),
        duration: 2
      });
      console.log(import_utils.locales.getText("tocPay.text.enterValidCardNumber"));
      return;
    }
    let { error, text } = formatName(values.card_name);
    if (error) {
      console.log(error);
      return;
    }
    let patt = /^([A-Za-z]+|[A-Za-z,.'&-]+) ([A-Za-z]+|[A-Za-z,.'&-]+)$/;
    if (!patt.test(text)) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("tocPay.text.enterEnglishName"),
        duration: 2
      });
      console.log(import_utils.locales.getText("tocPay.text.enterEnglishName"));
      return;
    }
    if (!values.date) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("tocPay.text.enterExpirationDate"),
        duration: 2
      });
      console.log(import_utils.locales.getText("tocPay.text.enterExpirationDate"));
      return;
    }
    if (!values.card_ccv) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("tocPay.text.enterCVV"),
        duration: 2
      });
      console.log(import_utils.locales.getText("tocPay.text.enterCVV"));
      return;
    }
    if ((values.card_ccv + "").length < 3) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText("tocPay.text.enterValidCVV"),
        duration: 2
      });
      console.log(import_utils.locales.getText("tocPay.text.enterValidCVV"));
      return;
    }
    return true;
  };
  const onReset = () => {
    form.resetFields();
  };
  const onFinish = (values) => {
    const _status = _checkValue(values);
    if (!_status) return;
    onCreate == null ? void 0 : onCreate(values);
  };
  const onSubmit = () => {
    form.submit();
  };
  const close = () => {
    setOpen(false);
  };
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      init,
      onReset,
      onSubmit,
      close
    })
  );
  const okText = (0, import_react.useMemo)(() => {
    var _a, _b, _c;
    let current = (_a = state.payGroup) == null ? void 0 : _a.otherPayment;
    if (current == null ? void 0 : current.surcharge) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, import_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ import_react.default.createElement(import_materials.PisellText.Amount, { value: ((_b = state.payGroup) == null ? void 0 : _b.wait_amount) || 0 }), " +", import_utils.locales.getText("tocPay.text.surcharge"));
    } else {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, import_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ import_react.default.createElement(import_materials.PisellText.Amount, { value: ((_c = state.payGroup) == null ? void 0 : _c.wait_amount) || 0 }));
    }
  }, [state.payGroup]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      title: /* @__PURE__ */ import_react.default.createElement("div", { className: "secure-card-payment-title" }, import_utils.locales.getText("tocPay.text.secureCardPayment"), /* @__PURE__ */ import_react.default.createElement(
        import_materials.Iconfont,
        {
          type: "pisell2-shield-tick",
          className: "secure-card-payment-title-icon"
        }
      )),
      open,
      onCancel,
      cancelVisible: false,
      onOk: onSubmit,
      okText,
      bodyStyle: { padding: 24, display: "flex", flexDirection: "column" },
      okButtonProps: {
        loading: btnLoading,
        style: {
          display: "flex",
          justifyContent: "center",
          alignItems: "center"
        }
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "add-mw-credit-card-wrap" }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellAlert,
      {
        className: "secure-card-tip-alert",
        icon: /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-shield-tick" }),
        showIcon: true,
        message: import_utils.locales.getText("tocPay.text.secureBankInfo")
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form,
      {
        layout: "vertical",
        form,
        onFinish,
        requiredMark: false
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Row, { style: { width: "100%" }, gutter: [10, 0] }, /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 24 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("tocPay.text.cardNumber"),
          name: "card_number",
          rules: [
            {
              required: true,
              message: import_utils.locales.getText("tocPay.text.incorrectCardNumber")
            }
          ]
        },
        /* @__PURE__ */ import_react.default.createElement(import_materials.PisellInput.BankCard, null)
      )), /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 12 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("tocPay.text.expiration"),
          name: "date",
          rules: [
            {
              required: true,
              message: import_utils.locales.getText("tocPay.text.incorrectExpiryDate")
            }
          ]
        },
        /* @__PURE__ */ import_react.default.createElement(import_materials.PisellDatePicker, { type: "input", format: "MM/YY" })
      )), /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 12 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("tocPay.text.securityCode"),
          name: "card_ccv",
          rules: [
            {
              required: true,
              message: import_utils.locales.getText(
                "tocPay.text.incorrectSecurityCode"
              )
            }
          ]
        },
        /* @__PURE__ */ import_react.default.createElement(import_materials.PisellInput.SecurityCode, { maxLength: 3 })
      )), /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 24 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("tocPay.text.cardholderName"),
          name: "card_name",
          rules: [
            {
              required: true,
              pattern: new RegExp(
                /^([A-Za-z]+|[A-Za-z,.'&-]+) ([A-Za-z]+|[A-Za-z,.'&-]+)$/,
                "g"
              ),
              message: import_utils.locales.getText("tocPay.text.enterEnglishName")
            }
          ]
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_materials.PisellInput,
          {
            placeholder: import_utils.locales.getText("tocPay.text.incorrectName")
          }
        )
      ))),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "merchant-authorization-text" }, import_utils.locales.getText("tocPay.text.merchantAuthorizationBefore"), /* @__PURE__ */ import_react.default.createElement("span", { className: "merchant-shop-name" }, shopName), import_utils.locales.getText("tocPay.text.merchantAuthorizationAfter")),
      /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { className: "secure-card-divider" }),
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          name: "is_save",
          valuePropName: "checked",
          initialValue: true,
          className: "save-card-form-item"
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Checkbox, null, /* @__PURE__ */ import_react.default.createElement("span", { className: "save-card-checkbox-text" }, import_utils.locales.getText("tocPay.text.saveCardForNextTime")))
      ),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "save-card-encryption-text" }, import_utils.locales.getText("tocPay.text.saveCardEncryption"))
    ), /* @__PURE__ */ import_react.default.createElement("div", { className: "three-d-secure-section" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "three-d-secure-badge" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-shield-01", className: "three-d-secure-icon" }), /* @__PURE__ */ import_react.default.createElement("span", { className: "three-d-secure-badge-text" }, "3D")), /* @__PURE__ */ import_react.default.createElement("span", { className: "three-d-secure-text" }, import_utils.locales.getText("tocPay.text.threeDSecureProtection"))))
  );
};
var Add_default = (0, import_react.forwardRef)(AddMWCreditCard);
