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/productExtension/fields/SelectProducts/index.tsx
var SelectProducts_exports = {};
__export(SelectProducts_exports, {
  default: () => SelectProducts_default
});
module.exports = __toCommonJS(SelectProducts_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_selectModal = __toESM(require("../../../selectModal"));
var import_server = require("./server");
var import_ProductItem = __toESM(require("./ProductItem"));
var import_ProductItemPreview = __toESM(require("./ProductItemPreview"));
var import_index = require("./index.less");
var import_utils = require("@pisell/utils");
var SelectProducts = ({ value, onChange }) => {
  const [listMap, setListMap] = (0, import_react.useState)({});
  const _getProductList = (0, import_ahooks.useRequest)(import_server.getProductList, {
    manual: true,
    onSuccess: (res) => {
      let obj = {};
      res.forEach((d) => {
        obj[d.id] = d;
      });
      setListMap(obj);
    }
  });
  const _value = (0, import_react.useMemo)(() => {
    if (value && value.length) {
      return value.map((d) => listMap[d.product_id] || d);
    }
    return value;
  }, [value, listMap]);
  (0, import_react.useEffect)(() => {
    _getProductList.run({
      skip: 1,
      num: 500,
      with: ["category"],
      open_quotation: 1,
      exclude_extension_type: ["event_item"],
      status: "published"
    });
  }, []);
  const onRemove = (item) => {
    onChange == null ? void 0 : onChange(
      (value || []).filter((d) => (d.id || d.product_id) !== item.id)
    );
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_selectModal.default,
    {
      value: _value,
      loading: _getProductList.loading,
      onChange: (arr) => {
        onChange == null ? void 0 : onChange(
          arr.map((d) => {
            return {
              product_collection_id: 0,
              product_id: d.id || d.product_id,
              product_variant_id: 0
            };
          })
        );
      },
      dataSource: _getProductList.data,
      renderValueItem: (item, onSelect) => {
        return /* @__PURE__ */ import_react.default.createElement(
          import_ProductItemPreview.default,
          {
            key: item == null ? void 0 : item.id,
            item,
            onClick: onSelect,
            onRemove
          }
        );
      },
      renderOptionItem: (item, onSelect) => {
        return /* @__PURE__ */ import_react.default.createElement(import_ProductItem.default, { key: item == null ? void 0 : item.id, item, onClick: onSelect });
      },
      buttonProps: {
        showSub: !!(value == null ? void 0 : value.length),
        title: import_utils.locales.getText("pisell2.text.product-extension.add-product"),
        subTitle: import_utils.locales.getText(
          "pisell2.text.product-extension.add-other-product"
        )
      },
      drawerProps: {
        width: 500,
        title: import_utils.locales.getText("pisell2.text.product-extension.add-product"),
        rootClassName: "select-product-drawer"
      },
      tabProductProps: {
        warpClassName: "select-product-warp",
        stickyTop: 188,
        scrollDom: ".select-product-drawer .pisell-lowcode-drawer-body"
      }
    }
  );
};
var SelectProducts_default = SelectProducts;
