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/webPosLogin/components/SelectStore/index.tsx
var SelectStore_exports = {};
__export(SelectStore_exports, {
  default: () => SelectStore_default
});
module.exports = __toCommonJS(SelectStore_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_index = require("./index.less");
var DEFAULT_STORE_ICON = "https://pisell2-dev.s3.cn-north-1.amazonaws.com.cn/static/default-store-icon.png";
var getStoreName = (store, locale = "en") => {
  var _a;
  const name = (_a = store == null ? void 0 : store.tenant) == null ? void 0 : _a.name;
  if (!name) return "";
  return name[locale] || name.original || "";
};
var matchStore = (store, keyword, locale) => {
  var _a;
  if (!keyword.trim()) return true;
  const lowerKeyword = keyword.toLowerCase();
  const name = (_a = store == null ? void 0 : store.tenant) == null ? void 0 : _a.name;
  const searchFields = [
    name == null ? void 0 : name.en,
    name == null ? void 0 : name["zh-CN"],
    name == null ? void 0 : name["zh-HK"],
    name == null ? void 0 : name.original,
    store == null ? void 0 : store.nickname
  ].filter(Boolean);
  return searchFields.some(
    (field) => field == null ? void 0 : field.toLowerCase().includes(lowerKeyword)
  );
};
var SelectStore = (0, import_react.memo)((props) => {
  const {
    storeList = [],
    locale = "en",
    loading = false,
    lastUsedStoreId,
    onSelect
  } = props;
  const [searchKeyword, setSearchKeyword] = (0, import_react.useState)("");
  const [selectingId, setSelectingId] = (0, import_react.useState)(null);
  const filteredStoreList = (0, import_react.useMemo)(() => {
    if (!searchKeyword.trim()) return storeList;
    return storeList.filter((store) => matchStore(store, searchKeyword, locale));
  }, [storeList, searchKeyword, locale]);
  const handleSearchChange = (0, import_react.useCallback)((e) => {
    setSearchKeyword(e.target.value);
  }, []);
  const handleSelectStore = (0, import_react.useCallback)(async (store) => {
    if (selectingId) return;
    setSelectingId(store.id);
    await (onSelect == null ? void 0 : onSelect(store));
    setSelectingId(null);
  }, [selectingId, onSelect]);
  const renderStoreItem = (store) => {
    var _a;
    const isLastUsed = lastUsedStoreId === store.id;
    const isSelecting = selectingId === store.id;
    const storeName = getStoreName(store, locale);
    const storeIcon = ((_a = store == null ? void 0 : store.tenant) == null ? void 0 : _a.logo) || DEFAULT_STORE_ICON;
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: store.id,
        className: `select-store__item ${isSelecting ? "select-store__item--selecting" : ""}`,
        onClick: () => handleSelectStore(store)
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store__item-icon" }, /* @__PURE__ */ import_react.default.createElement(import_materials.Image, { width: 48, height: 48, src: storeIcon, alt: storeName, fallbackType: "image", preview: false })),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store__item-content" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "select-store__item-name" }, storeName)),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store__item-extra" }, isSelecting ? /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { size: "small" }) : isLastUsed ? /* @__PURE__ */ import_react.default.createElement("span", { className: "select-store__item-tag" }, import_utils.locales.getText("webpos-login-last-used")) : null)
    );
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store__search" }, /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellInput,
    {
      placeholder: import_utils.locales.getText("webpos-login-search"),
      prefix: /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, { style: { color: "#999" } }),
      value: searchKeyword,
      onChange: handleSearchChange,
      allowClear: true,
      size: "large"
    }
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store__list" }, loading ? /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store__loading" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { size: "large" })) : filteredStoreList.length === 0 ? /* @__PURE__ */ import_react.default.createElement("div", { className: "select-store__empty" }, /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellEmpty,
    {
      description: import_utils.locales.getText("webpos-login-select-store-empty")
    }
  )) : filteredStoreList.map(renderStoreItem)));
});
SelectStore.displayName = "SelectStore";
var SelectStore_default = SelectStore;
