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/productSelect/index.tsx
var productSelect_exports = {};
__export(productSelect_exports, {
  default: () => productSelect_default
});
module.exports = __toCommonJS(productSelect_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_SearchLg = __toESM(require("@pisell/icon/es/SearchLg"));
var import_utils2 = require("../booking/utils");
var import_TabProduct = __toESM(require("./components/TabProduct"));
var import_locales = __toESM(require("./locales"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_useToken = require("./hooks/useToken");
var import_index = require("./index.less");
var index = ({
  dataSource = [],
  showSearch = true,
  scrollDom,
  style = {},
  token: customToken,
  onChange,
  selectedItems,
  productListSetting = {},
  ...rest
}) => {
  var _a, _b;
  const searchRef = (0, import_react.useRef)(null);
  const [keyword, setKeyword] = (0, import_react.useState)("");
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(
    import_locales.default,
    ((_b = (_a = context.appHelper.utils) == null ? void 0 : _a.storage) == null ? void 0 : _b.get("umi_locale")) || "en"
  );
  const _dataSource = (0, import_react.useMemo)(() => {
    let _list = dataSource || [];
    const isIncludes = (str) => {
      return str.toLowerCase().indexOf(keyword.toLowerCase()) !== -1;
    };
    return (0, import_utils2.formatProductListByCategory)(
      _list,
      keyword ? (item) => isIncludes(item.title) : void 0
    );
  }, [keyword, dataSource]);
  const token = (0, import_useToken.useToken)(customToken);
  const cssVars = (0, import_react.useMemo)(() => (0, import_useToken.genCSSVars)(token), [token]);
  const mergedStyle = (0, import_react.useMemo)(() => {
    const baseStyle = {
      ...cssVars,
      ...style
    };
    if (!scrollDom) return baseStyle;
    if (scrollDom !== ".pisell-product-select-wrapper") {
      return {
        ...baseStyle,
        overflow: "unset",
        height: "unset"
      };
    }
    return baseStyle;
  }, [cssVars, style, scrollDom]);
  const disabled = (0, import_react.useMemo)(() => {
    return !!(productListSetting == null ? void 0 : productListSetting.list_max_length) && Array.isArray(selectedItems) && (selectedItems == null ? void 0 : selectedItems.length) > 0;
  }, [productListSetting, selectedItems]);
  const _onChange = (0, import_react.useCallback)((value) => {
    if (disabled) return;
    onChange == null ? void 0 : onChange(value);
  }, [disabled, onChange]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(`pisell-product-select-wrapper`, {
        disabled
      }),
      style: mergedStyle
    },
    showSearch ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-product-select-search-wrapper" }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.Input,
      {
        ref: searchRef,
        prefix: /* @__PURE__ */ import_react.default.createElement(import_SearchLg.default, null),
        style: { height: 44 },
        placeholder: import_utils.locales.getText("pisell2.product-select.search"),
        allowClear: true,
        value: keyword,
        onChange: (e) => {
          setKeyword(e.target.value);
        }
      }
    )) : null,
    /* @__PURE__ */ import_react.default.createElement(
      import_TabProduct.default,
      {
        list: _dataSource,
        scrollDom: scrollDom || ".pisell-product-select-wrapper",
        ...productListSetting,
        ...rest,
        onChange: _onChange
      }
    )
  );
};
var productSelect_default = index;
