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/plus/clinetSearch/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_antd = require("antd");
var import_ahooks = require("ahooks");
var import_PhoneField = __toESM(require("./PhoneField"));
var import_GenderField = __toESM(require("./GenderField"));
var import_utils = require("@pisell/utils");
var import_serve = require("../serve");
var import_utils2 = require("../../../utils");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_index = require("./index.less");
var import_compensatedPisellContainer = __toESM(require("../../../pro/compensatedPisellContainer"));
var import_responsive = require("../../../responsive");
var AddClient = (0, import_react.forwardRef)((props, ref) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  import_utils2.request.setRequest((_b = (_a = context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.request);
  const { onChange, phone, containerProps, variant } = props;
  const isPhoneVariant = variant === "phone";
  const [form] = import_antd.Form.useForm();
  const useIsChecked = import_antd.Form.useWatch("is_checked", form);
  (0, import_react.useEffect)(() => {
    form.setFieldsValue({ phone: { code: "+61", phone } });
  }, [phone]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    reset: () => {
      form.resetFields();
    }
  }));
  const _addClient = (0, import_ahooks.useRequest)(import_serve.addCustomer, {
    manual: true,
    onBefore: (params) => {
      var _a2, _b2;
      try {
        const { getApp } = ((_a2 = context.appHelper) == null ? void 0 : _a2.utils) || {};
        const app = getApp();
        if (app) {
          (_b2 = app.logger) == null ? void 0 : _b2.addLog({
            type: "info",
            title: "Add Client",
            metadata: {
              time: Date.now(),
              data: params
            }
          });
        }
      } catch (error) {
        console.error(error);
      }
    },
    onSuccess: (res, param) => {
      var _a2;
      if (res.code !== 200 || !res.data) {
        import_antd.message.error(res.message);
        return;
      }
      onChange({ ...(_a2 = param[0]) == null ? void 0 : _a2.customer, id: res.data.customer_id }, useIsChecked);
    }
  });
  const onFinish = (values) => {
    var _a2;
    const { phone: phone2, ...rest } = values;
    if (!values.first_name && !values.last_name && !values.email && !((_a2 = values == null ? void 0 : values.phone) == null ? void 0 : _a2.phone)) {
      import_antd.message.info(import_utils.locales.getText("pisell2.text.add-client.confirm"));
      return;
    }
    _addClient.run({
      customer: {
        accepts_marketing: 0,
        register_channel: "pos",
        tax_exempt: 1,
        ...rest,
        first_name: rest.first_name || "",
        last_name: rest.last_name || "",
        note: rest.note || "",
        email: rest.email || "",
        country_calling_code: phone2.code || "",
        phone: phone2.phone || ""
      },
      address: {
        first_name: "",
        last_name: "",
        company: "",
        address1: "",
        address2: "",
        city: "",
        country_code: "AU",
        province_code: "ACT",
        phone: "",
        country: "澳大利亚",
        province: "Australian Capital Territory",
        country_calling_code: "+61"
      },
      tags: []
    });
  };
  const renderContent = () => {
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form,
      {
        form,
        onFinish,
        layout: "vertical",
        initialValues: {
          is_checked: 0,
          phone: {
            code: "+61",
            phone: ""
          }
        },
        size: "large"
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Row, { gutter: [16, 0] }, /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 12 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("pisell2.text.firstname"),
          name: "first_name"
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_antd.Input,
          {
            placeholder: import_utils.locales.getText("pisell2.text.firstname"),
            size: "large"
          }
        )
      )), /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 12 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("pisell2.text.lastname"),
          name: "last_name"
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_antd.Input,
          {
            placeholder: import_utils.locales.getText("pisell2.text.lastname"),
            size: "large"
          }
        )
      )), /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 12 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("pisell2.text.gender"),
          name: "gender"
        },
        /* @__PURE__ */ import_react.default.createElement(import_GenderField.default, 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("pisell2.text.birthday"),
          name: "birthday"
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_antd.DatePicker,
          {
            style: { width: "100%" },
            size: "large",
            format: "DD/MM/YYYY",
            inputReadOnly: true,
            placeholder: import_utils.locales.getText("pisell2.text.birthday"),
            placement: "bottomLeft"
          }
        )
      )), /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { span: 12 }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: import_utils.locales.getText("pisell2.text.phone"),
          name: "phone"
        },
        /* @__PURE__ */ import_react.default.createElement(import_PhoneField.default, 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("pisell2.text.email"),
          name: "email",
          rules: [
            {
              pattern: /^([a-zA-Z0-9_\-\.+])+@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,63})+$/,
              message: import_utils.locales.getText("pisell2.text.email-error-message")
            }
          ]
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_antd.Input,
          {
            placeholder: import_utils.locales.getText("pisell2.text.email"),
            size: "large",
            type: "email"
          }
        )
      )), /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { hidden: true, name: "is_checked" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Input, null)))
    );
  };
  const renderFooter = () => {
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "select-customer-right-panel-add-client__footer" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        size: "large",
        style: { flex: 1 },
        onClick: () => {
          form.setFieldsValue({ is_checked: 0 });
          form.submit();
        },
        loading: _addClient.loading && !useIsChecked
      },
      import_utils.locales.getText("pisell2.text.save")
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        type: "primary",
        size: "large",
        style: { flex: 1 },
        onClick: () => {
          form.setFieldsValue({ is_checked: 1 });
          form.submit();
        },
        loading: _addClient.loading && useIsChecked
      },
      import_utils.locales.getText("pisell2.text.save-and-select")
    ));
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_compensatedPisellContainer.default,
    {
      renderType: isPhoneVariant ? "drawer" : "modal",
      showHandlerBar: isPhoneVariant,
      onHandlerBarClick: containerProps.onClose || containerProps.onCancel,
      props: isPhoneVariant ? {
        ...containerProps,
        placement: "bottom",
        height: "auto",
        style: {
          maxHeight: "95dvh"
        },
        width: "100%",
        closable: false,
        bodyStyle: {
          padding: "24px 24px 0"
        },
        footer: renderFooter(),
        destroyOnClose: true,
        onClose: containerProps.onClose || containerProps.onCancel,
        maskClosable: false
      } : {
        ...containerProps,
        bodyStyle: {
          padding: "24px 24px 0"
        },
        footer: renderFooter(),
        destroyOnClose: true,
        maskClosable: false
      }
    },
    renderContent()
  );
});
AddClient.displayName = "AddClient";
var Add_default = (0, import_responsive.withResponsive)(AddClient);
