var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/ecocup/components/SearchBar/hooks.ts
var hooks_exports = {};
__export(hooks_exports, {
  useCupScanStatus: () => useCupScanStatus,
  useSearch: () => useSearch,
  useSearchCup: () => useSearchCup,
  useSearchCustomer: () => useSearchCustomer
});
module.exports = __toCommonJS(hooks_exports);
var import_react = require("react");
var import_utils = require("@pisell/utils");
var import_hooks = require("../../hooks");
var import_types = require("../../types");
var import_hooks2 = require("../../hooks");
var useSearchCustomer = () => {
  const { get } = (0, import_hooks.useRequest)();
  const { goalShopId } = (0, import_hooks2.useGetGoalShopInfo)();
  const run = (text) => {
    if (!text) return;
    return get("/shop/wallet/eco/customer", {
      search: text,
      with: ["tags"],
      goal_shop_id: goalShopId
    });
  };
  return {
    run
  };
};
var useSearchCup = () => {
  const { get } = (0, import_hooks.useRequest)();
  const { goalShopId } = (0, import_hooks2.useGetGoalShopInfo)();
  const run = (text) => {
    if (!text) return;
    return get("/shop/wallet/eco", {
      type: "eco",
      code: text,
      with: [
        "customer:id,display_name,cover,phone,country_calling_code,nickname",
        "rentShop:id,name",
        "returnShop:id,name",
        "currentShop:id,name"
      ],
      num: 1,
      goal_shop_id: goalShopId
    });
  };
  return {
    run
  };
};
var useSearch = () => {
  const searchCustomer = useSearchCustomer();
  const searchCup = useSearchCup();
  const [loading, setLoading] = (0, import_react.useState)(false);
  const run = async (params) => {
    var _a, _b;
    const { searchType, searchText = "" } = params;
    const isCupCode = /^EC[a-zA-Z0-9]{8}$/.test(searchText);
    if (searchType === "cup" && !isCupCode) {
      return {
        status: "failed",
        message: import_utils.locales.getText("pisell2.text.ecocup.search-bar.search-tip")
      };
    }
    const type = !!searchType ? searchType : isCupCode ? "cup" : "customer";
    const runFn = type === "cup" ? searchCup.run : searchCustomer.run;
    try {
      setLoading(true);
      const res = await runFn(searchText);
      if ((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.list) {
        return { status: "success", type, data: ((_b = res == null ? void 0 : res.data) == null ? void 0 : _b.list) || [] };
      } else {
        return { status: "failed", type, message: (res == null ? void 0 : res.message) || "" };
      }
    } catch (error) {
      return { status: "failed", type, message: (error == null ? void 0 : error.message) || "" };
    } finally {
      setLoading(false);
    }
  };
  return { loading, run };
};
var useCupScanStatus = () => {
  const shopId = (0, import_hooks.useGetShopId)();
  const getCupScanStatus = (cup, customerId) => {
    if (!cup) {
      return import_types.ECupScanStatus.NOT_FOUND;
    }
    if ((cup == null ? void 0 : cup.status) == import_types.ECupStatus.IN_STOCK) {
      if ((cup == null ? void 0 : cup.current_shop_id) != shopId) {
        return import_types.ECupScanStatus.OTHER_IN_STOCK;
      }
    }
    if ((cup == null ? void 0 : cup.status) == import_types.ECupStatus.RENT) {
      if ((cup == null ? void 0 : cup.customer_id) != customerId) {
        return import_types.ECupScanStatus.OTHER_RENT;
      }
    }
    if ((cup == null ? void 0 : cup.status) == import_types.ECupStatus.MISSING) {
      return import_types.ECupScanStatus.MISSING;
    }
    if ((cup == null ? void 0 : cup.status) == import_types.ECupStatus.BROKEN) {
      return import_types.ECupScanStatus.BROKEN;
    }
  };
  return {
    getCupScanStatus
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useCupScanStatus,
  useSearch,
  useSearchCup,
  useSearchCustomer
});
