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/ecocup/components/SearchBar/index.tsx
var SearchBar_exports = {};
__export(SearchBar_exports, {
  default: () => SearchBar_default
});
module.exports = __toCommonJS(SearchBar_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_index = require("./index.less");
var import_searchSection = __toESM(require("../../../../pro/comprehensiveSearch/components/searchSection"));
var import_hooks = require("../../hooks");
var import_const = require("./const");
var import_hooks2 = require("./hooks");
var import_CupStatusModal = __toESM(require("../CupStatusModal"));
var import_types = require("../../types");
var import_dayjs = __toESM(require("dayjs"));
var SearchBar = (props) => {
  var _a;
  const {
    scanListenerKey,
    size = "large",
    placeholder,
    onSearch,
    searchType,
    onSearchResult,
    customerId,
    onCupStatusValidate
  } = props;
  const [value, setValue] = (0, import_react.useState)("");
  const [searchResult, setSearchResult] = (0, import_react.useState)(null);
  const containerRef = (0, import_react.useRef)(null);
  const { addListener, removeListener, setActiveListener } = (0, import_hooks.useScanListener)();
  const searchHook = (0, import_hooks2.useSearch)();
  const { getCupScanStatus } = (0, import_hooks2.useCupScanStatus)();
  const cupStatusModal = (0, import_hooks.useModal)("ecocup-cup-status-modal");
  const shopId = (0, import_hooks.useGetShopId)();
  const onSelectResult = (0, import_ahooks.useMemoizedFn)(
    (type, item) => {
      setSearchResult(null);
      setValue("");
      onSearchResult == null ? void 0 : onSearchResult({ type, data: item });
    }
  );
  const scanSearchCallback = (0, import_ahooks.useMemoizedFn)((str) => {
    console.log("scanSearchCallback>>>>>>>", str);
    if (onSearch) {
      onSearch(str || "");
      return;
    }
    if (!str) {
      setValue("");
      setSearchResult(null);
      return;
    }
    searchHook.run({ searchType, searchText: str }).then((res) => {
      var _a2, _b, _c;
      if ((res == null ? void 0 : res.status) === "success") {
        if ((res == null ? void 0 : res.type) === "cup") {
          const _cupData = ((_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2[0]) || null;
          const scanStatus = getCupScanStatus(_cupData, customerId);
          console.log(scanStatus, _cupData, shopId, "sjdhfjj");
          if (onCupStatusValidate) {
            if (scanStatus && scanStatus === import_types.ECupScanStatus.OTHER_RENT) {
              onCupStatusValidate == null ? void 0 : onCupStatusValidate({
                data: _cupData || { code: str }
              });
              setValue("");
              return;
            }
          }
          if (scanStatus) {
            cupStatusModal.open({
              data: {
                scanStatus,
                cupData: _cupData || { code: str }
              }
            });
            return;
          }
        }
        const dataLength = ((_b = res == null ? void 0 : res.data) == null ? void 0 : _b.length) || 0;
        if (dataLength === 1) {
          setValue("");
          setSearchResult(null);
          onSelectResult(res == null ? void 0 : res.type, (_c = res == null ? void 0 : res.data) == null ? void 0 : _c[0]);
          return;
        }
        if (dataLength === 0 && (res == null ? void 0 : res.type) === "customer") {
          (0, import_materials.PisellToast)({
            content: import_utils.locales.getText(
              "pisell2.text.ecocup.toast.fail-no-match-customer"
            )
          });
          setSearchResult(null);
          return;
        }
        setSearchResult(res == null ? void 0 : res.data);
      } else {
        if (res == null ? void 0 : res.message) {
          (0, import_materials.PisellToast)({ content: res == null ? void 0 : res.message });
        }
        setSearchResult(null);
      }
    });
  });
  (0, import_react.useEffect)(() => {
    addListener(scanListenerKey, (result) => {
      setValue("");
      setSearchResult(null);
      scanSearchCallback((result == null ? void 0 : result.value) || "");
    });
    return () => {
      removeListener(scanListenerKey);
    };
  }, [scanListenerKey, scanSearchCallback]);
  (0, import_react.useEffect)(() => {
    const handleClickOutside = (event) => {
      const target = event.target;
      if (!containerRef.current || !target) {
        return;
      }
      if (!containerRef.current.contains(target) && (searchResult == null ? void 0 : searchResult.length) > 1) {
        setSearchResult(null);
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [searchResult]);
  return /* @__PURE__ */ import_react.default.createElement("div", { ref: containerRef, className: "ecocup-cup-search-bar" }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: { flex: 1 },
      onClick: () => {
        console.log("onClick>>>>>>>", scanListenerKey);
        setActiveListener(scanListenerKey);
      }
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_searchSection.default,
      {
        value,
        onChange: setValue,
        loading: searchHook.loading,
        style: {
          flex: 1,
          height: size === "large" ? "60px" : "48px",
          padding: 0
        },
        placeholder: placeholder || import_utils.locales.getText("pisell2.text.cup-project.search-bar.placeholder"),
        searchButtonProps: {
          placement: "inside",
          style: size === "large" ? {
            height: 48,
            padding: 8,
            minWidth: 160
          } : {
            height: 32,
            padding: 8,
            minWidth: 120
          }
        },
        cameraButtonProps: {
          icon: ["pisell2-camera-01", "pisell2-qr-code-02"]
        },
        onSearch: () => scanSearchCallback(value)
      }
    )
  ), (searchResult == null ? void 0 : searchResult.length) > 1 ? /* @__PURE__ */ import_react.default.createElement("div", { className: "ecocup-cup-search-bar-result" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "ecocup-cup-search-bar-result-header" }, (_a = import_const.SEARCH_BAR_RESULT_HEADER_CONFIG) == null ? void 0 : _a.map((item) => /* @__PURE__ */ import_react.default.createElement("div", { key: item.key }, import_utils.locales.getText(item.label)))), /* @__PURE__ */ import_react.default.createElement("div", { className: "ecocup-cup-search-bar-result-content" }, searchResult == null ? void 0 : searchResult.map((item) => {
    var _a2, _b;
    const {
      nickname,
      display_name,
      create_time,
      country_calling_code,
      phone,
      email,
      tags
    } = item;
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: item.id,
        className: "ecocup-cup-search-bar-result-content-item",
        onClick: () => onSelectResult("customer", item)
      },
      /* @__PURE__ */ import_react.default.createElement("div", null, display_name || nickname || "-"),
      /* @__PURE__ */ import_react.default.createElement("div", null, create_time ? (0, import_dayjs.default)(create_time).format("DD/MM/YYYY") : "-"),
      /* @__PURE__ */ import_react.default.createElement("div", null, phone ? `(${country_calling_code}) ${phone}` : "-"),
      /* @__PURE__ */ import_react.default.createElement("div", null, email || "-"),
      /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("span", { className: "ecocup-cup-search-bar-result-content-item-membership" }, ((_a2 = tags == null ? void 0 : tags.find) == null ? void 0 : _a2.call(tags, (v) => (v == null ? void 0 : v.name) == "admin")) ? "Admin" : ((_b = tags == null ? void 0 : tags.find) == null ? void 0 : _b.call(tags, (v) => (v == null ? void 0 : v.name) == "business")) ? "Business" : "Regular"))
    );
  }))) : null, cupStatusModal.visible && /* @__PURE__ */ import_react.default.createElement(import_CupStatusModal.default, { ...cupStatusModal.modalProps }));
};
var SearchBar_default = SearchBar;
