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/Sales/Holder/index.tsx
var Holder_exports = {};
__export(Holder_exports, {
  HolderContext: () => HolderContext,
  HolderProvider: () => HolderProvider,
  default: () => Holder_default,
  useHolder: () => useHolder
});
module.exports = __toCommonJS(Holder_exports);
var import_react = __toESM(require("react"));
var import_SalesModel = require("../../../businessModel/SalesModel");
var import_serve = require("./serve");
var import_ahooks = require("ahooks");
var import_Cart = require("../Cart");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_Customer = require("../Customer");
var defaultState = {};
var HolderContext = (0, import_react.createContext)(
  void 0
);
var HolderProvider = ({ children }) => {
  var _a, _b;
  const { getUserInfo } = (0, import_Customer.useCustomer)();
  const { data, setData, getCurrentData } = (0, import_SalesModel.useSales)();
  const {
    createCartItemFromHolder,
    addHolderToCartItem,
    deleteCartItemByHolder
  } = (0, import_Cart.useCart)();
  const [open, setOpen] = (0, import_react.useState)(false);
  const context = (0, import_useEngineContext.default)();
  const utils = context.appHelper.utils || {};
  const holderData = (0, import_react.useMemo)(() => {
    return data.holderData || {};
  }, [data]);
  const setHolderData = (newHolderData) => {
    setData({
      holderData: {
        ...getCurrentData().holderData,
        ...newHolderData
      }
    });
  };
  const getHolderList = (0, import_ahooks.useRequest)(import_serve.getHolderListApi, {
    manual: true,
    onSuccess: (res) => {
      var _a2, _b2, _c;
      const list = [];
      (_c = (_b2 = (_a2 = res.data) == null ? void 0 : _a2.list) == null ? void 0 : _b2.forEach) == null ? void 0 : _c.call(_b2, (v) => {
        const selectList = selectedHolderIds || [];
        if (!selectList.includes(v.form_record_id)) {
          list.push(createHolderItem(v));
        }
      });
      setHolderData({
        holderList: list
      });
    }
  });
  const createHolder = (0, import_ahooks.useRequest)(import_serve.createHolderApi, {
    manual: true
  });
  const refreshHolder = () => {
    var _a2, _b2, _c, _d, _e, _f, _g;
    const customerId = (_a2 = getUserInfo()) == null ? void 0 : _a2.id;
    getHolderList.run({
      form_id: (_f = (_e = (_d = (_c = (_b2 = data.initData) == null ? void 0 : _b2.boardSetting) == null ? void 0 : _c.config) == null ? void 0 : _d.basic) == null ? void 0 : _e.appointment_subject) == null ? void 0 : _f.form_id,
      shop_id: (_g = data.initData) == null ? void 0 : _g.shop_id,
      customer_id: customerId
    });
  };
  const updateHolderList = (holderItem) => {
    var _a2;
    if (holderItem) {
      const holderList = ((_a2 = getCurrentData().holderData) == null ? void 0 : _a2.holderList) || [];
      setHolderData({
        holderList: [createHolderItem(holderItem), ...holderList]
      });
    }
  };
  const createHolderItem = (item) => {
    return {
      id: item.form_record_id,
      key: item.form_record_id,
      name: item.main_field,
      label: item.main_field,
      avatar: item.customer_cover,
      _originData: item
    };
  };
  const selectHolder = (holderItem, type) => {
    var _a2;
    if (type === "addToCart") {
      addHolderToCartItem(holderItem);
    } else if (type === "createCartItem") {
      createCartItemFromHolder(holderItem);
    }
    setHolderData({
      currentHolderItem: holderItem
    });
    const selectedHolderItems = ((_a2 = data.holderData) == null ? void 0 : _a2.selectedHolderItems) || [];
    const res = selectedHolderItems.find((v) => v.id === holderItem.id);
    if (res === void 0) {
      setHolderData({
        selectedHolderItems: [...selectedHolderItems, holderItem]
      });
    }
  };
  const onHolderOpen = () => {
    setOpen(true);
    refreshHolder();
  };
  const onOpenCreateHolder = ({
    form_title,
    callback
  }) => {
    var _a2, _b2, _c, _d, _e;
    utils.action({
      type: "pisell1.form",
      data: {
        form_id: (_e = (_d = (_c = (_b2 = (_a2 = data.initData) == null ? void 0 : _a2.boardSetting) == null ? void 0 : _b2.config) == null ? void 0 : _c.basic) == null ? void 0 : _d.appointment_subject) == null ? void 0 : _e.form_id,
        form_title,
        isV2API: true
      },
      callback
    });
  };
  const onHolderClose = () => {
    setOpen(false);
  };
  const deleteHolderById = (holderId) => {
    var _a2, _b2;
    const newSelectedHolderItems = (_b2 = (_a2 = getCurrentData().holderData) == null ? void 0 : _a2.selectedHolderItems) == null ? void 0 : _b2.filter(
      (v) => v.id !== holderId
    );
    setHolderData({ selectedHolderItems: newSelectedHolderItems });
    deleteCartItemByHolder(holderId);
  };
  const selectedHolderIds = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c;
    return (_c = (_b2 = (_a2 = data.holderData) == null ? void 0 : _a2.selectedHolderItems) == null ? void 0 : _b2.map) == null ? void 0 : _c.call(_b2, (v) => {
      return v.id;
    });
  }, [(_a = data.holderData) == null ? void 0 : _a.selectedHolderItems]);
  const defaultSelectedHolder = (0, import_react.useMemo)(() => {
    var _a2;
    const lists = ((_a2 = data.holderData) == null ? void 0 : _a2.holderList) || [];
    return (lists == null ? void 0 : lists[0]) || {};
  }, [(_b = data.holderData) == null ? void 0 : _b.holderList]);
  const contextValue = {
    holderData,
    getHolderList,
    selectHolder,
    open,
    onOpenCreateHolder,
    onHolderOpen,
    onHolderClose,
    createHolder,
    refreshHolder,
    deleteHolderById,
    selectedHolderIds,
    updateHolderList,
    defaultSelectedHolder
  };
  return /* @__PURE__ */ import_react.default.createElement(HolderContext.Provider, { value: contextValue }, children);
};
var useHolder = () => {
  const context = import_react.default.useContext(HolderContext);
  if (!context) {
    throw new Error("useHolder must be used within a HolderProvider");
  }
  return context;
};
var Holder_default = {
  defaultState
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  HolderContext,
  HolderProvider,
  useHolder
});
