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/booking/materiels/contacts/index.tsx
var contacts_exports = {};
__export(contacts_exports, {
  default: () => contacts_default
});
module.exports = __toCommonJS(contacts_exports);
var import_react = __toESM(require("react"));
var import_PlusCircle = __toESM(require("@pisell/icon/es/PlusCircle"));
var import_DotsVertical = __toESM(require("@pisell/icon/es/DotsVertical"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_useComponents = __toESM(require("../../../../hooks/useComponents"));
var import_locales = __toESM(require("../../locales"));
var import_index = require("./index.less");
var import_locales2 = require("../../../../utils/locales");
import_utils.locales.init(import_locales.default, (0, import_locales2.getLocale)());
var _name = (item) => {
  var _a, _b;
  if (item == null ? void 0 : item.display_name) {
    return (_a = item == null ? void 0 : item.display_name) == null ? void 0 : _a.trim();
  }
  if (item == null ? void 0 : item.nickname) {
    return (_b = item == null ? void 0 : item.nickname) == null ? void 0 : _b.trim();
  }
  if ((item == null ? void 0 : item.first_name) || (item == null ? void 0 : item.last_name)) {
    return (item == null ? void 0 : item.first_name) + " " + (item == null ? void 0 : item.last_name);
  }
  return "-";
};
var Contacts = (props) => {
  const [state, setState] = (0, import_ahooks.useControllableValue)(props, {
    defaultValue: []
  });
  const operationType = (0, import_react.useRef)(null);
  const currentRef = (0, import_react.useRef)(null);
  const contactRef = (0, import_react.useRef)(null);
  const [ContactModal] = (0, import_useComponents.default)({
    components: ["ContactModal"]
  });
  const menus = (0, import_react.useMemo)(() => {
    let list = [
      { key: "edit", label: import_utils.locales.getText("pisell2.text.edit-profile") },
      { key: "remove", label: import_utils.locales.getText("pisell2.text.remove-contact") }
    ];
    return list;
  }, []);
  const handleEditContact = (item, index) => {
    var _a;
    operationType.current = "edit";
    currentRef.current = index;
    (_a = contactRef == null ? void 0 : contactRef.current) == null ? void 0 : _a.open({
      detail: item
    });
  };
  const handleRemoveContact = (item, index) => {
    setState((v) => (v || []).filter((_, i) => i !== index));
  };
  const menuChange = (key, item, index) => {
    let maps = {
      edit: handleEditContact,
      remove: handleRemoveContact
    };
    maps[key](item, index);
  };
  const handleAddClick = () => {
    var _a;
    operationType.current = "add";
    (_a = contactRef == null ? void 0 : contactRef.current) == null ? void 0 : _a.open({
      detail: null,
      event_id: 0
    });
  };
  const handleChange = (value) => {
    if (operationType.current === "add") {
      setState((v) => [...v || [], value]);
    } else if (operationType.current === "edit") {
      setState(
        (v) => (v || []).map((item, index) => {
          if (index === currentRef.current) {
            return value;
          }
          return item;
        })
      );
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts-title" }, import_utils.locales.getText("pisell2.text.contacts")), !!(state == null ? void 0 : state.length) && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts-value-wrap" }, (state || []).map((item, index) => {
    var _a;
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts-item", key: item.id }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts-item-left" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts-name" }, _name(item)), item.email && /* @__PURE__ */ import_react.default.createElement("div", null, item.email), (item == null ? void 0 : item.phone) ? /* @__PURE__ */ import_react.default.createElement("div", null, (item == null ? void 0 : item.country_calling_code) ? `(${((_a = item == null ? void 0 : item.country_calling_code) == null ? void 0 : _a.includes("+")) ? "" : "+"}${item == null ? void 0 : item.country_calling_code})` : "", " ", item == null ? void 0 : item.phone) : null), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts-item-menu" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Dropdown,
      {
        menu: {
          items: menus,
          onClick: (e) => menuChange(e.key, item, index)
        },
        placement: "bottomRight"
      },
      /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
        import_DotsVertical.default,
        {
          style: { fontSize: 24, color: "#475467" }
        }
      ))
    ))), index !== (state == null ? void 0 : state.length) - 1 && /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, null));
  })), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__contacts-add", onClick: handleAddClick }, /* @__PURE__ */ import_react.default.createElement(import_PlusCircle.default, { style: { fontSize: 24 } }), /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-lowcode__contacts-add-text" }, import_utils.locales.getText("pisell2.text.add-contacts"))), ContactModal && /* @__PURE__ */ import_react.default.createElement(
    ContactModal,
    {
      ref: contactRef,
      type: "order",
      showRemoveBtn: false,
      onChange: handleChange
    }
  ));
};
var contacts_default = Contacts;
