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/SelectDevice/index.tsx
var SelectDevice_exports = {};
__export(SelectDevice_exports, {
  default: () => SelectDevice_default
});
module.exports = __toCommonJS(SelectDevice_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 DeviceIcon = () => /* @__PURE__ */ import_react.default.createElement("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, /* @__PURE__ */ import_react.default.createElement("rect", { x: "4", y: "3", width: "16", height: "18", rx: "2", stroke: "currentColor", strokeWidth: "2" }), /* @__PURE__ */ import_react.default.createElement("line", { x1: "4", y1: "7", x2: "20", y2: "7", stroke: "currentColor", strokeWidth: "2" }), /* @__PURE__ */ import_react.default.createElement("line", { x1: "4", y1: "17", x2: "20", y2: "17", stroke: "currentColor", strokeWidth: "2" }), /* @__PURE__ */ import_react.default.createElement("circle", { cx: "12", cy: "19.5", r: "0.5", fill: "currentColor" }));
var matchDevice = (device, keyword) => {
  if (!keyword.trim()) return true;
  const lowerKeyword = keyword.toLowerCase();
  const searchFields = [
    device == null ? void 0 : device.name,
    device == null ? void 0 : device.description,
    device == null ? void 0 : device.number
  ].filter(Boolean);
  return searchFields.some(
    (field) => field == null ? void 0 : field.toLowerCase().includes(lowerKeyword)
  );
};
var getConfirmText = (device) => {
  var _a;
  const deviceName = (device == null ? void 0 : device.name) || "";
  const clientName = ((_a = device == null ? void 0 : device.client) == null ? void 0 : _a.name) || "";
  const template = import_utils.locales.getText("webpos-login-device-takeover-confirm");
  return template(deviceName, clientName);
};
var SelectDevice = (0, import_react.memo)((props) => {
  const {
    deviceList = [],
    locale = "en",
    loading = false,
    lastUsedDeviceId,
    onSelect,
    confirmOkText,
    confirmCancelText
  } = props;
  const [searchKeyword, setSearchKeyword] = (0, import_react.useState)("");
  const [selectingId, setSelectingId] = (0, import_react.useState)(null);
  const [confirmVisible, setConfirmVisible] = (0, import_react.useState)(false);
  const [pendingDevice, setPendingDevice] = (0, import_react.useState)(null);
  const filteredDeviceList = (0, import_react.useMemo)(() => {
    if (!searchKeyword.trim()) return deviceList;
    return deviceList.filter((device) => matchDevice(device, searchKeyword));
  }, [deviceList, searchKeyword]);
  const handleSearchChange = (0, import_react.useCallback)((e) => {
    setSearchKeyword(e.target.value);
  }, []);
  const handleSelectDevice = (0, import_react.useCallback)((device) => {
    if (selectingId) return;
    if (device.client) {
      setPendingDevice(device);
      setConfirmVisible(true);
      return;
    }
    setSelectingId(device.id);
    onSelect == null ? void 0 : onSelect(device);
  }, [selectingId, onSelect]);
  const handleConfirmSelect = (0, import_react.useCallback)(() => {
    if (!pendingDevice) return;
    setConfirmVisible(false);
    setSelectingId(pendingDevice.id);
    onSelect == null ? void 0 : onSelect(pendingDevice);
    setPendingDevice(null);
  }, [pendingDevice, onSelect]);
  const handleCancelConfirm = (0, import_react.useCallback)(() => {
    setConfirmVisible(false);
    setPendingDevice(null);
  }, []);
  const okText = confirmOkText || import_utils.locales.getText("webpos-login-device-takeover-confirm-ok");
  const cancelText = confirmCancelText || import_utils.locales.getText("webpos-login-device-takeover-confirm-cancel");
  const renderDeviceItem = (device) => {
    const isLastUsed = lastUsedDeviceId === device.id;
    const isSelecting = selectingId === device.id;
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: device.id,
        className: `select-device__item ${isSelecting ? "select-device__item--selecting" : ""}`,
        onClick: () => handleSelectDevice(device)
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "select-device__item-icon" }, /* @__PURE__ */ import_react.default.createElement(DeviceIcon, null)),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "select-device__item-content" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "select-device__item-name" }, device.name), device.description && /* @__PURE__ */ import_react.default.createElement("span", { className: "select-device__item-desc" }, device.description)),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "select-device__item-extra" }, isSelecting ? /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { size: "small" }) : isLastUsed ? /* @__PURE__ */ import_react.default.createElement("span", { className: "select-device__item-tag" }, import_utils.locales.getText("webpos-login-last-used")) : null)
    );
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "select-device" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "select-device__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-device__list" }, loading ? /* @__PURE__ */ import_react.default.createElement("div", { className: "select-device__loading" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { size: "large" })) : filteredDeviceList.length === 0 ? /* @__PURE__ */ import_react.default.createElement("div", { className: "select-device__empty" }, /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellEmpty,
    {
      description: import_utils.locales.getText("webpos-login-select-device-empty")
    }
  )) : filteredDeviceList.map(renderDeviceItem)), /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal.Information,
    {
      title: "",
      describe: pendingDevice ? getConfirmText(pendingDevice) : "",
      open: confirmVisible,
      okText,
      cancelText,
      okVisible: true,
      cancelVisible: true,
      closable: true,
      footerDivider: true,
      destroyOnClose: true,
      onOk: handleConfirmSelect,
      onCancel: handleCancelConfirm
    }
  ));
});
SelectDevice.displayName = "SelectDevice";
var SelectDevice_default = SelectDevice;
