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/ticketBooking/hooks/pisellos/useCustomer.ts
var useCustomer_exports = {};
__export(useCustomer_exports, {
  useCustomer: () => useCustomer
});
module.exports = __toCommonJS(useCustomer_exports);
var import_react = require("react");
var import_bookingTicket = require("./bookingTicket");
var import_useEngineContext = require("../../../../hooks/useEngineContext");
var import_ahooks = require("ahooks");
var import_useActiveExecute = require("./useActiveExecute");
var useCustomer = (props) => {
  const bookingTicket = (0, import_bookingTicket.useBookingTicket)();
  const pisellos = (0, import_useEngineContext.usePisellOS)();
  const { executeIfActive } = (0, import_useActiveExecute.useActiveExecute)();
  const [customers, setCustomers] = (0, import_react.useState)([]);
  const [selectedCustomer, setSelectedCustomer] = (0, import_react.useState)(
    null
  );
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [error, setError] = (0, import_react.useState)(null);
  const [pagination, setPagination] = (0, import_react.useState)({
    page: 1,
    pageSize: 20,
    total: 0
  });
  const [hasMore, setHasMore] = (0, import_react.useState)(false);
  const [loadingMore, setLoadingMore] = (0, import_react.useState)(false);
  (0, import_react.useEffect)(() => {
    var _a;
    if (!bookingTicket) {
      return;
    }
    const customerModule = (_a = bookingTicket == null ? void 0 : bookingTicket.store) == null ? void 0 : _a.customer;
    const wrappedOnCustomerListUpdate = executeIfActive((state) => {
      setCustomers(state.customerList);
      setHasMore(state.hasMore);
      setPagination((prev) => ({
        ...prev,
        total: state.total,
        page: state.currentPage,
        pageSize: state.pageSize
      }));
    });
    const wrappedOnCustomerSelected = executeIfActive((customer) => {
      var _a2;
      setSelectedCustomer(customer);
      (_a2 = props.changeCustomerToGlobalState) == null ? void 0 : _a2.call(props, customer);
    });
    const wrappedOnPaginationChange = executeIfActive((paginationInfo) => {
      setPagination(paginationInfo);
    });
    const unlisten1 = customerModule.effectsOn("onCustomerListUpdate", wrappedOnCustomerListUpdate);
    const unlisten2 = customerModule.effectsOn("onCustomerSelected", wrappedOnCustomerSelected);
    const unlisten3 = customerModule.effectsOn(
      "onPaginationChange",
      wrappedOnPaginationChange
    );
    return () => {
      unlisten1();
      unlisten2();
      unlisten3();
    };
  }, []);
  const loadCustomers = (0, import_ahooks.useMemoizedFn)(
    async (params) => {
      try {
        setLoading(true);
        await bookingTicket.getCustomerList(params);
      } catch (err) {
        setError(err == null ? void 0 : err.message);
      }
      setLoading(false);
    }
  );
  const initLoad = (0, import_ahooks.useMemoizedFn)(async (keyword = "", formId) => {
    try {
      setLoading(true);
      await bookingTicket.resetAndLoadCustomers({
        search: keyword || void 0,
        relation_form_id: formId || void 0
      });
    } catch (error2) {
      console.error("初始化加载失败:", error2);
    }
    setLoading(false);
  });
  const loadMore = (0, import_ahooks.useMemoizedFn)(async () => {
    if (!hasMore || loadingMore) return;
    setLoadingMore(true);
    setLoading(true);
    try {
      await bookingTicket.loadMoreCustomers();
    } catch (error2) {
      console.error("加载更多失败:", error2);
    }
    setLoading(false);
    setLoadingMore(false);
  });
  const changePage = (0, import_ahooks.useMemoizedFn)(async (page, pageSize) => {
    try {
      setLoading(true);
      await bookingTicket.changeCustomerPage(page, pageSize);
    } catch (err) {
      setError(err.message);
    }
    setLoading(false);
  });
  const selectCustomer = (0, import_ahooks.useMemoizedFn)((customer) => {
    var _a, _b;
    (_a = bookingTicket == null ? void 0 : bookingTicket.setActiveCustomer) == null ? void 0 : _a.call(bookingTicket, customer);
    (_b = props.changeCustomerToGlobalState) == null ? void 0 : _b.call(props, customer);
  });
  const addCustomerToFirst = (0, import_ahooks.useMemoizedFn)((customer) => {
    var _a;
    (_a = bookingTicket == null ? void 0 : bookingTicket.addCustomerToFirst) == null ? void 0 : _a.call(bookingTicket, customer);
  });
  return {
    // 客户列表
    customers,
    // 选中的客户
    selectedCustomer,
    // 加载状态
    loading,
    // 错误信息
    error,
    // 分页信息
    pagination,
    // 加载客户列表
    loadCustomers,
    // 切换分页
    changePage,
    // 选择客户
    selectCustomer,
    // 新增客户到第一个
    addCustomerToFirst,
    // 初始化滚动加载
    initLoad,
    // 更多数据
    hasMore,
    // 加载更多
    loadMore
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useCustomer
});
