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/index.tsx
var clinetSearch_exports = {};
__export(clinetSearch_exports, {
  default: () => clinetSearch_default
});
module.exports = __toCommonJS(clinetSearch_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_dayjs = __toESM(require("dayjs"));
var import_InfiniteScroll = __toESM(require("./InfiniteScroll"));
var import_comprehensiveSearch = __toESM(require("../../pro/comprehensiveSearch"));
var import_clientName = __toESM(require("../clientName"));
var import_Add = __toESM(require("./Add"));
var import_locales = __toESM(require("./locales"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_useTranslationOriginal = __toESM(require("../../hooks/useTranslationOriginal"));
var import_index = require("./index.less");
var ClientSearch = (0, import_react.forwardRef)(
  ({
    onRowClick,
    resultProps,
    onAddClient,
    hasMore,
    onLoadMore,
    state,
    ...props
  }, ref) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const [modalVisible, setModalVisible] = (0, import_react.useState)(false);
    const comprehensiveSearchRef = (0, import_react.useRef)(null);
    const addClientRef = (0, import_react.useRef)(null);
    const scrollContainerRef = (0, import_react.useRef)(null);
    const translationOriginal = (0, import_useTranslationOriginal.default)();
    const context = (0, import_useEngineContext.default)();
    import_utils.locales.init(
      import_locales.default,
      ((_c = (_b = (_a = context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.storage) == null ? void 0 : _c.get("umi_locale")) || "en"
    );
    const appointmentSubject = ((_f = (_e = (_d = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _d.config) == null ? void 0 : _e.basic) == null ? void 0 : _f.appointment_subject) || {};
    const translation = (_h = (_g = context == null ? void 0 : context.appHelper) == null ? void 0 : _g.utils) == null ? void 0 : _h.translationShop;
    const handleAdd = () => {
      setModalVisible(true);
    };
    const handleModalCancel = () => {
      var _a2;
      setModalVisible(false);
      (_a2 = addClientRef.current) == null ? void 0 : _a2.reset();
    };
    const renderListHeader = () => {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "client-search-list-header" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "header-item", style: { width: 180 } }, import_utils.locales.getText("pisell2.text.client-search.name")), /* @__PURE__ */ import_react.default.createElement("div", { className: "header-item", style: { width: 110 } }, import_utils.locales.getText("pisell2.text.client-search.register-time")), /* @__PURE__ */ import_react.default.createElement("div", { className: "header-item", style: { width: 125 } }, import_utils.locales.getText("pisell2.text.client-search.membership")), /* @__PURE__ */ import_react.default.createElement("div", { className: "header-item", style: { width: 180 } }, import_utils.locales.getText("pisell2.text.client-search.contact-info")), /* @__PURE__ */ import_react.default.createElement("div", { className: "header-item", style: { width: 200 } }, translation(appointmentSubject == null ? void 0 : appointmentSubject.form_name) || import_utils.locales.getText("pisell2.text.client-search.holder")));
    };
    const renderResults = (searchResults) => {
      return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, (searchResults == null ? void 0 : searchResults.length) ? renderListHeader() : null, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          ref: scrollContainerRef,
          id: "client-search_infinite-scroll",
          style: {
            height: "calc(100% - 40px)",
            overflowY: "auto",
            overflowX: "hidden",
            display: "flex"
          }
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_InfiniteScroll.default,
          {
            dataLength: searchResults.length,
            next: () => {
              console.log("loading more");
              onLoadMore == null ? void 0 : onLoadMore();
            },
            hasMore: hasMore || false,
            style: { width: "100%" },
            loader: /* @__PURE__ */ import_react.default.createElement(
              "div",
              {
                style: {
                  textAlign: "center",
                  padding: "20px",
                  color: "#999"
                }
              },
              import_utils.locales.getText("pisell2.text.loading")
            ),
            endMessage: /* @__PURE__ */ import_react.default.createElement("div", { style: { textAlign: "center", color: "#999" } }, (searchResults == null ? void 0 : searchResults.length) ? import_utils.locales.getText("pisell2.text.no-more-data") : null),
            scrollableTarget: "client-search_infinite-scroll",
            scrollThreshold: "180px"
          },
          /* @__PURE__ */ import_react.default.createElement(
            import_antd.List,
            {
              dataSource: searchResults,
              renderItem: (item, index) => {
                var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i, _j;
                const membershipText = (_b2 = (_a2 = item.latest_wallet_detail) == null ? void 0 : _a2.wallet) == null ? void 0 : _b2.name;
                return /* @__PURE__ */ import_react.default.createElement(
                  import_antd.List.Item,
                  {
                    key: item.id,
                    className: "client-search-list-item"
                  },
                  /* @__PURE__ */ import_react.default.createElement(
                    "div",
                    {
                      className: "list-item-content",
                      onClick: () => onRowClick == null ? void 0 : onRowClick(item, index || 0),
                      style: { cursor: "pointer" }
                    },
                    /* @__PURE__ */ import_react.default.createElement("div", { className: "item-cell", style: { width: 180 } }, /* @__PURE__ */ import_react.default.createElement(import_clientName.default, { dataSource: item }), /* @__PURE__ */ import_react.default.createElement("div", null, item.phone || "-"), /* @__PURE__ */ import_react.default.createElement(
                      "div",
                      {
                        style: {
                          overflow: "hidden",
                          textOverflow: "ellipsis",
                          whiteSpace: "nowrap"
                        }
                      },
                      item.email || "-"
                    )),
                    /* @__PURE__ */ import_react.default.createElement("div", { className: "item-cell", style: { width: 110 } }, (0, import_dayjs.default)(item.created_at).format("YYYY-MM-DD") || "-"),
                    /* @__PURE__ */ import_react.default.createElement("div", { className: "item-cell", style: { width: 125 } }, membershipText && (translationOriginal == null ? void 0 : translationOriginal(membershipText)) ? /* @__PURE__ */ import_react.default.createElement(
                      import_antd.Tag,
                      {
                        color: "gold",
                        style: {
                          overflow: "hidden",
                          whiteSpace: "nowrap",
                          textOverflow: "ellipsis",
                          width: "auto",
                          maxWidth: "100%",
                          display: "inline-block",
                          borderRadius: 16,
                          marginRight: 0
                        }
                      },
                      translationOriginal == null ? void 0 : translationOriginal(membershipText)
                    ) : "-"),
                    /* @__PURE__ */ import_react.default.createElement("div", { className: "item-cell", style: { width: 180 } }, /* @__PURE__ */ import_react.default.createElement(import_clientName.default, { dataSource: (_c2 = item == null ? void 0 : item.contacts_info) == null ? void 0 : _c2.value }), ((_f2 = (_e2 = (_d2 = item == null ? void 0 : item.contacts_info) == null ? void 0 : _d2.value) == null ? void 0 : _e2.phone) == null ? void 0 : _f2.phone) || "-", /* @__PURE__ */ import_react.default.createElement(
                      "div",
                      {
                        style: {
                          overflow: "hidden",
                          textOverflow: "ellipsis",
                          whiteSpace: "nowrap"
                        }
                      },
                      ((_h2 = (_g2 = item == null ? void 0 : item.contacts_info) == null ? void 0 : _g2.value) == null ? void 0 : _h2.email) || "-"
                    )),
                    /* @__PURE__ */ import_react.default.createElement("div", { className: "item-cell", style: { width: 200 } }, ((_i = item == null ? void 0 : item.form_record) == null ? void 0 : _i.length) > 0 ? (_j = item == null ? void 0 : item.form_record) == null ? void 0 : _j.map((itemForm) => {
                      return /* @__PURE__ */ import_react.default.createElement(
                        "div",
                        {
                          key: itemForm == null ? void 0 : itemForm.id,
                          style: {
                            overflow: "hidden",
                            textOverflow: "ellipsis",
                            whiteSpace: "nowrap"
                          }
                        },
                        (itemForm == null ? void 0 : itemForm.main_field) || "-"
                      );
                    }) : "-")
                  )
                );
              }
            }
          )
        )
      ));
    };
    const handleAddClient = (record, isChecked) => {
      onAddClient == null ? void 0 : onAddClient(record, isChecked);
      handleModalCancel();
    };
    (0, import_react.useImperativeHandle)(ref, () => ({
      resetScrollPosition: () => {
        if (scrollContainerRef.current) {
          scrollContainerRef.current.scrollTop = 0;
        }
      }
    }));
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
      import_comprehensiveSearch.default,
      {
        ...props,
        ref: comprehensiveSearchRef,
        renderType: "modal",
        onAdd: handleAdd,
        resultDisplay: renderResults
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      import_Add.default,
      {
        containerProps: {
          className: "pisell-client-search__new-client-modal",
          title: import_utils.locales.getText("pisell2.text.create-new-client"),
          open: modalVisible,
          onCancel: handleModalCancel,
          width: 680
        },
        onChange: handleAddClient,
        ref: addClientRef
      }
    ));
  }
);
var clinetSearch_default = ClientSearch;
