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/PisellContactBrief/components/ContactFormModal.tsx
var ContactFormModal_exports = {};
__export(ContactFormModal_exports, {
  ContactFormModal: () => ContactFormModal
});
module.exports = __toCommonJS(ContactFormModal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_pisellSingleLineText = require("../../pisellSingleLineText");
var import_pisellPhone = require("../../pisellPhone");
var import_pisellEmail = require("../../pisellEmail");
var FIRSTNAME_FIELD = "firstname";
var LASTNAME_FIELD = "lastname";
var PHONE_FIELD = "phone";
var EMAIL_FIELD = "email";
function splitName(name) {
  if (!name || !name.trim()) return ["", ""];
  const idx = name.trim().indexOf(" ");
  if (idx <= 0) return [name.trim(), ""];
  return [name.trim().slice(0, idx), name.trim().slice(idx + 1)];
}
function joinName(first, last) {
  return [first, last].map((s) => s == null ? void 0 : s.trim()).filter(Boolean).join(" ");
}
function ContactFormModal({
  visible,
  onCancel,
  onSubmit,
  mode,
  initialValues,
  extraFields = []
}) {
  const [form] = import_antd.Form.useForm();
  (0, import_react.useEffect)(() => {
    if (visible) {
      const [firstname, lastname] = splitName(initialValues == null ? void 0 : initialValues.name);
      const values = {
        firstname,
        lastname,
        name: (initialValues == null ? void 0 : initialValues.name) ?? "",
        phone: (initialValues == null ? void 0 : initialValues.phone) ?? "",
        email: (initialValues == null ? void 0 : initialValues.email) ?? ""
      };
      if (initialValues && extraFields.length > 0) {
        extraFields.forEach((f) => {
          values[f.key] = initialValues[f.key] ?? "";
        });
      }
      form.setFieldsValue(values);
    }
  }, [visible, initialValues, extraFields, form]);
  const handleOk = async () => {
    try {
      const raw = await form.validateFields();
      const name = joinName(raw.firstname ?? "", raw.lastname ?? "");
      const values = {
        name,
        phone: raw.phone,
        email: raw.email
      };
      extraFields.forEach((f) => {
        values[f.key] = raw[f.key];
      });
      onSubmit(values);
      form.resetFields();
      onCancel();
    } catch {
    }
  };
  const handleCancel = () => {
    form.resetFields();
    onCancel();
  };
  const title = mode === "create" ? import_utils.locales.getText("pisell.contact.brief.modal.title.create") : import_utils.locales.getText("pisell.contact.brief.modal.title.edit");
  const okText = import_utils.locales.getText("pisell.contact.brief.modal.save");
  const cancelText = import_utils.locales.getText("pisell.contact.brief.modal.cancel");
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Modal,
    {
      title,
      open: visible,
      onOk: handleOk,
      onCancel: handleCancel,
      okText,
      cancelText,
      destroyOnClose: true,
      width: 480,
      className: "pisell-contact-brief-modal"
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", preserve: false, className: "pisell-contact-brief-modal-form" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-contact-brief-modal-form-row" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        name: FIRSTNAME_FIELD,
        label: import_utils.locales.getText("pisell.contact.brief.modal.field.firstname"),
        rules: [{ required: true, message: import_utils.locales.getText("pisell.contact.brief.modal.field.required") }],
        className: "pisell-contact-brief-modal-form-item-half"
      },
      /* @__PURE__ */ import_react.default.createElement(import_pisellSingleLineText.PisellSingleLineText, { placeholder: "", mode: "edit" })
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        name: LASTNAME_FIELD,
        label: import_utils.locales.getText("pisell.contact.brief.modal.field.lastname"),
        className: "pisell-contact-brief-modal-form-item-half"
      },
      /* @__PURE__ */ import_react.default.createElement(import_pisellSingleLineText.PisellSingleLineText, { placeholder: "", mode: "edit" })
    )), /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { name: PHONE_FIELD, label: import_utils.locales.getText("pisell.contact.brief.modal.field.phone") }, /* @__PURE__ */ import_react.default.createElement(
      import_pisellPhone.PisellPhone,
      {
        placeholder: import_utils.locales.getText("pisell.contact.brief.modal.field.phone.placeholder"),
        mode: "edit"
      }
    )), /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { name: EMAIL_FIELD, label: import_utils.locales.getText("pisell.contact.brief.modal.field.email") }, /* @__PURE__ */ import_react.default.createElement(
      import_pisellEmail.PisellEmail,
      {
        placeholder: import_utils.locales.getText("pisell.contact.brief.modal.field.email.placeholder"),
        mode: "edit"
      }
    )), extraFields.map((f) => /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { key: f.key, name: f.key, label: f.label }, /* @__PURE__ */ import_react.default.createElement(import_pisellSingleLineText.PisellSingleLineText, { placeholder: "", mode: "edit" }))))
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ContactFormModal
});
