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/pisellInformationEntry/index.tsx
var pisellInformationEntry_exports = {};
__export(pisellInformationEntry_exports, {
  default: () => pisellInformationEntry_default
});
module.exports = __toCommonJS(pisellInformationEntry_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_usePisellConfig = __toESM(require("../pisell-config-provider/hooks/usePisellConfig"));
var import_pisellCard = __toESM(require("../pisellCard"));
var import_iconfont = __toESM(require("../iconfont"));
var import_constants = require("./constants");
var import_Scan = __toESM(require("./Scan"));
var import_button = __toESM(require("../button"));
var import_Input = __toESM(require("./Input"));
var import_utils = require("./utils");
var import_useTerminalPeripherals = __toESM(require("./hooks/useTerminalPeripherals"));
var import_index = require("./index.less");
var CardButton = (props) => {
  const { icon, children, ...others } = props;
  return /* @__PURE__ */ import_react.default.createElement(import_pisellCard.default, { ...others }, /* @__PURE__ */ import_react.default.createElement("div", null, icon), /* @__PURE__ */ import_react.default.createElement("div", null, children));
};
var PisellInformationEntry = (props) => {
  const {
    platform: propsPlatform,
    title,
    entryModes = [],
    searchDescribe,
    onChange
  } = props;
  const config = (0, import_usePisellConfig.default)();
  const { platform } = config;
  const [currentType, setCurrentType] = (0, import_react.useState)(
    null
  );
  const { peripheralsStatus } = (0, import_useTerminalPeripherals.default)(onChange);
  const [availableEntryModes, setAvailableEntryModes] = (0, import_react.useState)(entryModes);
  (0, import_react.useEffect)(() => {
    document.body.id = "body";
  }, []);
  (0, import_react.useEffect)(() => {
    const getEntryModes = async () => {
      let available = entryModes;
      if (!await (0, import_utils.hasCamera)()) {
        available = entryModes.filter((item) => item.type !== "scanCode");
      }
      available = available.filter((item) => {
        if (item.type === "scanner") {
          return peripheralsStatus.scanner !== "none";
        }
        if (item.type === "nfc") {
          return peripheralsStatus.nfc !== "none";
        }
        return true;
      });
      if (available.length === 1) {
        setCurrentType(available[0].type);
      }
      setAvailableEntryModes(available);
    };
    getEntryModes();
  }, [entryModes, peripheralsStatus]);
  const handleChange = (val) => {
    onChange == null ? void 0 : onChange(val);
  };
  const handleEntryClick = (type) => {
    if (import_constants.passiveModeType.includes(type)) {
      return;
    }
    setCurrentType(type);
  };
  const { isHasInput, isHasNfc, isHasScanner, isHasScanCode } = (0, import_react.useMemo)(() => {
    let isHasScanCode2 = false;
    let isHasInput2 = false;
    let isHasScanner2 = false;
    let isHasNfc2 = false;
    availableEntryModes.forEach((item) => {
      if (item.type === "scanCode") {
        isHasScanCode2 = true;
      }
      if (item.type === "input") {
        isHasInput2 = true;
      }
      if (item.type === "scanner") {
        isHasScanner2 = true;
      }
      if (item.type === "nfc") {
        isHasNfc2 = true;
      }
    });
    return {
      isHasScanCode: isHasScanCode2,
      isHasInput: isHasInput2,
      isHasScanner: isHasScanner2,
      isHasNfc: isHasNfc2
    };
  }, [availableEntryModes]);
  const isShowEntryModes = (0, import_react.useMemo)(() => {
    if (!currentType) {
      return true;
    }
    if (availableEntryModes.length === 1 && import_constants.passiveModeType.includes(availableEntryModes[0].type)) {
      return true;
    }
    return false;
  }, [currentType, availableEntryModes]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-information-entry-wrap" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-information-entry-title" }, /* @__PURE__ */ import_react.default.createElement("span", null, title), currentType && isHasScanCode && isHasInput && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-information-entry-title-action-wrap" }, isHasScanCode && /* @__PURE__ */ import_react.default.createElement(
    import_button.default,
    {
      size: "large",
      type: currentType === "scanCode" ? "primary" : "default",
      icon: /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: import_constants.entryModeTextIcon["scanCode"].icon }),
      onClick: () => handleEntryClick("scanCode")
    }
  ), isHasInput && /* @__PURE__ */ import_react.default.createElement(
    import_button.default,
    {
      size: "large",
      type: currentType === "input" ? "primary" : "default",
      icon: /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: import_constants.entryModeTextIcon["input"].icon }),
      onClick: () => handleEntryClick("input")
    }
  ))), isShowEntryModes && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-information-entry-modes" }, availableEntryModes.map((item) => {
    const detail = import_constants.entryModeTextIcon[item.type];
    let icon = detail.icon;
    if (["scanner", "nfc"].includes(item.type)) {
      const disconnect = import_constants.entryModeErrorTextIcon[item.type];
      icon = peripheralsStatus[item.type] === "connected" ? detail.icon : disconnect.icon;
    }
    return /* @__PURE__ */ import_react.default.createElement(
      CardButton,
      {
        bordered: !import_constants.passiveModeType.includes(item.type),
        icon: /* @__PURE__ */ import_react.default.createElement(
          import_iconfont.default,
          {
            type: icon,
            className: "pisell-card-button-icon"
          }
        ),
        className: (0, import_classnames.default)("pisell-information-entry-item", {
          ["pisell-entry-ready"]: import_constants.passiveModeType.includes(item.type)
        }),
        onClick: () => handleEntryClick(item.type)
      },
      item.name || detail.text
    );
  })), /* @__PURE__ */ import_react.default.createElement("div", null, currentType === "scanCode" && /* @__PURE__ */ import_react.default.createElement(import_Scan.default, { onChange: handleChange, onCancel: () => setCurrentType(null) }), currentType === "input" && /* @__PURE__ */ import_react.default.createElement(import_Input.default, { onChange: handleChange, searchDescribe })));
};
var pisellInformationEntry_default = PisellInformationEntry;
