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/salesSdk/context/SalesSdkCustomerContext.tsx
var SalesSdkCustomerContext_exports = {};
__export(SalesSdkCustomerContext_exports, {
  SalesSdkCustomerContext: () => SalesSdkCustomerContext,
  SalesSdkCustomerProvider: () => SalesSdkCustomerProvider
});
module.exports = __toCommonJS(SalesSdkCustomerContext_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_SalesSdkContext = require("./SalesSdkContext");
var SalesSdkCustomerContext = (0, import_react.createContext)(null);
SalesSdkCustomerContext.displayName = "SalesSdkCustomerContext";
var DEFAULT_PAGINATION = {
  page: 1,
  pageSize: 20,
  total: 0,
  totalPages: 0
};
var EMPTY_CUSTOMER_LIST = [];
var EMPTY_SEARCH_PARAMS = {};
var SalesSdkCustomerProvider = ({ children }) => {
  var _a;
  const sales = (0, import_react.useContext)(import_SalesSdkContext.SalesSdkContext);
  const bookingTicket = (sales == null ? void 0 : sales.bookingTicket) ?? null;
  const customerModule = ((_a = bookingTicket == null ? void 0 : bookingTicket.store) == null ? void 0 : _a.customer) ?? null;
  const [selected, setSelected] = (0, import_react.useState)(null);
  const [list, setList] = (0, import_react.useState)(EMPTY_CUSTOMER_LIST);
  const [pagination, setPagination] = (0, import_react.useState)(DEFAULT_PAGINATION);
  const [hasMore, setHasMore] = (0, import_react.useState)(false);
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [loadingMore, setLoadingMore] = (0, import_react.useState)(false);
  const [searchParams, setSearchParams] = (0, import_react.useState)(EMPTY_SEARCH_PARAMS);
  (0, import_react.useEffect)(() => {
    var _a2, _b, _c;
    if (!bookingTicket) {
      setSelected(null);
      setList(EMPTY_CUSTOMER_LIST);
      setPagination(DEFAULT_PAGINATION);
      setHasMore(false);
      setLoading(false);
      setLoadingMore(false);
      setSearchParams(EMPTY_SEARCH_PARAMS);
      return;
    }
    try {
      setSelected(((_a2 = bookingTicket.getOrderCustomerSnapshot) == null ? void 0 : _a2.call(bookingTicket)) ?? null);
      const initialList = ((_b = bookingTicket.getCustomers) == null ? void 0 : _b.call(bookingTicket)) ?? EMPTY_CUSTOMER_LIST;
      setList(initialList);
      const initialPagination = ((_c = bookingTicket.getCustomerPaginationInfo) == null ? void 0 : _c.call(bookingTicket)) ?? DEFAULT_PAGINATION;
      setPagination(initialPagination);
    } catch (e) {
      console.warn("[SalesSdk] CustomerProvider 初始同步失败", e);
    }
  }, [bookingTicket]);
  (0, import_react.useEffect)(() => {
    if (!customerModule || typeof customerModule.effectsOn !== "function") return;
    const offs = [];
    offs.push(
      customerModule.effectsOn("onCustomerListUpdate", (state) => {
        if (Array.isArray(state == null ? void 0 : state.customerList)) setList(state.customerList);
        if (typeof (state == null ? void 0 : state.hasMore) === "boolean") setHasMore(state.hasMore);
        if (typeof (state == null ? void 0 : state.loading) === "boolean") setLoading(state.loading);
        if (typeof (state == null ? void 0 : state.loadingMore) === "boolean") setLoadingMore(state.loadingMore);
        if ((state == null ? void 0 : state.searchParams) && typeof state.searchParams === "object") {
          setSearchParams(state.searchParams);
        }
        if (typeof (state == null ? void 0 : state.total) === "number" && typeof (state == null ? void 0 : state.currentPage) === "number" && typeof (state == null ? void 0 : state.pageSize) === "number") {
          setPagination({
            page: state.currentPage,
            pageSize: state.pageSize,
            total: state.total,
            totalPages: Math.ceil(state.total / Math.max(state.pageSize, 1)) || 0
          });
        }
      })
    );
    offs.push(
      customerModule.effectsOn("onPaginationChange", (info) => {
        if (info && typeof info === "object") setPagination(info);
      })
    );
    return () => {
      for (const off of offs) {
        try {
          off();
        } catch {
        }
      }
    };
  }, [customerModule]);
  (0, import_react.useEffect)(() => {
    if (!bookingTicket) return;
    const off = bookingTicket.effectsOn(
      "onOrderCustomerChange",
      (payload) => {
        setSelected((payload == null ? void 0 : payload.snapshot) ?? null);
      }
    );
    return () => {
      try {
        off();
      } catch {
      }
    };
  }, [bookingTicket]);
  (0, import_react.useEffect)(() => {
    if (!bookingTicket) return;
    const off = bookingTicket.effectsOn("onSalesOrderLoaded", () => {
      var _a2;
      try {
        setSelected(((_a2 = bookingTicket.getOrderCustomerSnapshot) == null ? void 0 : _a2.call(bookingTicket)) ?? null);
      } catch {
      }
    });
    return () => {
      try {
        off();
      } catch {
      }
    };
  }, [bookingTicket]);
  const select = (0, import_ahooks.useMemoizedFn)((customer) => {
    var _a2;
    if (!bookingTicket) {
      console.warn("[SalesSdk] Customer.select: bookingTicket 未就绪");
      return;
    }
    (_a2 = bookingTicket.setOrderCustomer) == null ? void 0 : _a2.call(bookingTicket, customer);
  });
  const loadList = (0, import_ahooks.useMemoizedFn)(
    async (params) => {
      if (!bookingTicket) throw new Error("[SalesSdk] Customer: bookingTicket 未初始化");
      return bookingTicket.getCustomerList(params);
    }
  );
  const search = (0, import_ahooks.useMemoizedFn)(async (keyword) => {
    if (!bookingTicket) throw new Error("[SalesSdk] Customer: bookingTicket 未初始化");
    return bookingTicket.resetAndLoadCustomers({
      search: keyword
    });
  });
  const loadMore = (0, import_ahooks.useMemoizedFn)(async () => {
    if (!bookingTicket) throw new Error("[SalesSdk] Customer: bookingTicket 未初始化");
    return bookingTicket.loadMoreCustomers();
  });
  const changePage = (0, import_ahooks.useMemoizedFn)(
    async (page, pageSize) => {
      if (!bookingTicket) throw new Error("[SalesSdk] Customer: bookingTicket 未初始化");
      return bookingTicket.changeCustomerPage(page, pageSize);
    }
  );
  const addToFirst = (0, import_ahooks.useMemoizedFn)((customer) => {
    bookingTicket == null ? void 0 : bookingTicket.addCustomerToFirst(customer);
  });
  const clear = (0, import_ahooks.useMemoizedFn)(() => {
    bookingTicket == null ? void 0 : bookingTicket.clearCustomers();
  });
  const loadById = (0, import_ahooks.useMemoizedFn)((id) => {
    if (!bookingTicket) return null;
    return bookingTicket.getCustomerById(id);
  });
  const value = (0, import_react.useMemo)(
    () => ({
      selected,
      list,
      pagination,
      hasMore,
      loading,
      loadingMore,
      searchParams,
      select,
      loadList,
      search,
      loadMore,
      changePage,
      addToFirst,
      clear,
      loadById
    }),
    [
      selected,
      list,
      pagination,
      hasMore,
      loading,
      loadingMore,
      searchParams,
      select,
      loadList,
      search,
      loadMore,
      changePage,
      addToFirst,
      clear,
      loadById
    ]
  );
  return /* @__PURE__ */ import_react.default.createElement(SalesSdkCustomerContext.Provider, { value }, children);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SalesSdkCustomerContext,
  SalesSdkCustomerProvider
});
