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/pisellLookup/components/LookupTrigger.tsx
var LookupTrigger_exports = {};
__export(LookupTrigger_exports, {
  LookupTrigger: () => LookupTrigger
});
module.exports = __toCommonJS(LookupTrigger_exports);
var import_icons = require("@ant-design/icons");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_locales = require("../../../locales");
var import_iconfont = __toESM(require("../../iconfont"));
var import_pisellQRScanner = __toESM(require("../../pisellQRScanner"));
var LookupTriggerComponent = (0, import_react.forwardRef)(
  (props, ref) => {
    var _a;
    const {
      type = "input",
      trigger,
      triggerText,
      triggerIcon,
      buttonText = (0, import_locales.getText)("pisell-lookup-button-text"),
      disabled = false,
      placeholder = (0, import_locales.getText)("pisell-lookup-placeholder"),
      size = "middle",
      showPrefixIcon = false,
      prefixIcon,
      addonBefore,
      showClear = true,
      showConfirmButton = true,
      confirmButtonText = (0, import_locales.getText)("pisell-lookup-confirm-button-text"),
      showScan = true,
      value,
      searchSource = "keyboard",
      loading,
      inputRender,
      onChange,
      onClick,
      onFocus,
      onConfirm,
      onScan,
      onClear,
      onBlur,
      autoFocus = false,
      className,
      style
    } = props;
    const context = (0, import_useEngineContext.default)();
    const { getApp } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
    const inputRef = (0, import_react.useRef)(null);
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        focus: () => {
          var _a2;
          (_a2 = inputRef.current) == null ? void 0 : _a2.focus();
        },
        select: () => {
          var _a2;
          (_a2 = inputRef.current) == null ? void 0 : _a2.select();
        }
      }),
      []
    );
    const [scanVisible, setScanVisible] = (0, import_react.useState)(false);
    const handleChange = (e) => {
      onChange == null ? void 0 : onChange(e.target.value);
    };
    const handleClear = () => {
      onChange == null ? void 0 : onChange("");
      onClear == null ? void 0 : onClear();
    };
    const getAppScanBarcodePlugin = () => {
      if (getApp) {
        const app = getApp();
        const scanBarcode = app == null ? void 0 : app.getPlugin("webScan");
        if (scanBarcode) {
          return scanBarcode;
        }
      }
      return null;
    };
    const appScan = () => {
      const app = getApp();
      try {
        const scanBarcode = getAppScanBarcodePlugin();
        scanBarcode.scan({
          type: "capacitor",
          params: {
            hint: 17
          }
        }).then((res) => {
          onScan == null ? void 0 : onScan(res);
        });
      } catch (err) {
        app.logger.addLog({
          type: "error",
          title: "扫码代码执行异常",
          metadata: { err }
        });
      }
    };
    const handleScanClick = () => {
      if (getAppScanBarcodePlugin()) {
        appScan();
        return;
      }
      setScanVisible(true);
    };
    const handleScanSuccess = (code) => {
      setScanVisible(false);
      onScan == null ? void 0 : onScan(code);
    };
    const handleScanClose = () => {
      setScanVisible(false);
    };
    const handleConfirm = () => {
      onConfirm == null ? void 0 : onConfirm();
    };
    const handleKeyPress = (e) => {
      var _a2;
      if (e.key === "Enter") {
        const currentValue = ((_a2 = e.currentTarget) == null ? void 0 : _a2.value) ?? value ?? "";
        onConfirm == null ? void 0 : onConfirm(currentValue);
      }
    };
    const renderInputTrigger = () => {
      if (inputRender) {
        return inputRender({
          value: value ?? "",
          placeholder,
          disabled,
          size,
          loading,
          onChange: (v) => onChange == null ? void 0 : onChange(v),
          onConfirm: (v) => onConfirm == null ? void 0 : onConfirm(v ?? value ?? ""),
          onClear: handleClear,
          onScan: (code) => onScan == null ? void 0 : onScan(code),
          searchSource,
          onFocus,
          onBlur
        });
      }
      const prefix = showPrefixIcon ? prefixIcon || /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, null) : void 0;
      const isScanResult = searchSource === "scan";
      const suffix = /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lookup-input-suffix" }, showClear && value && /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          type: "link",
          size,
          className: "pisell-lookup-input-cancel",
          onMouseDown: (event) => event.preventDefault(),
          onClick: handleClear
        },
        isScanResult ? (0, import_locales.getText)("pisell-lookup-cancel") || "取消" : (0, import_locales.getText)("pisell-lookup-clear") || "Clear"
      ), showScan && /* @__PURE__ */ import_react.default.createElement(
        import_iconfont.default,
        {
          className: "pisell-lookup-input-scan",
          type: "pisell2-scan",
          onClick: handleScanClick,
          style: {
            fontSize: size === "small" ? 18 : size === "large" ? 26 : 24
          }
        }
      ), showConfirmButton && /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          type: "primary",
          size,
          onClick: handleConfirm,
          className: "pisell-lookup-input-confirm"
        },
        confirmButtonText
      ));
      const inputEl = /* @__PURE__ */ import_react.default.createElement(
        import_antd.Input,
        {
          ref: inputRef,
          value,
          onChange: handleChange,
          onFocus,
          onBlur,
          onPressEnter: handleKeyPress,
          placeholder,
          size,
          disabled,
          prefix,
          suffix,
          addonBefore,
          autoFocus,
          className: `pisell-lookup-trigger-input ${className || ""}`.trim(),
          style
        }
      );
      if (!showConfirmButton) {
        return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lookup-trigger-input pisell-lookup-trigger-input-no-confirm" }, inputEl);
      }
      return inputEl;
    };
    const renderButtonTrigger = () => {
      return /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          type: "primary",
          icon: /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, null),
          onClick,
          disabled,
          size,
          className: `pisell-lookup-trigger-button ${className || ""}`,
          style
        },
        buttonText
      );
    };
    const renderIconTrigger = () => {
      const icon = triggerIcon || /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, { style: { fontSize: 20 } });
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: `pisell-lookup-trigger-icon ${className || ""}`,
          onClick: disabled ? void 0 : onClick,
          style: {
            cursor: disabled ? "not-allowed" : "pointer",
            opacity: disabled ? 0.5 : 1,
            ...style
          }
        },
        icon
      );
    };
    const renderTextTrigger = () => {
      const text = triggerText || "搜索";
      return /* @__PURE__ */ import_react.default.createElement(
        "span",
        {
          className: `pisell-lookup-trigger-text ${className || ""}`,
          onClick: disabled ? void 0 : onClick,
          style: {
            cursor: disabled ? "not-allowed" : "pointer",
            opacity: disabled ? 0.5 : 1,
            ...style
          }
        },
        text
      );
    };
    const renderCustomTrigger = () => {
      if (!trigger) return null;
      if ((0, import_react.isValidElement)(trigger)) {
        return (0, import_react.cloneElement)(trigger, {
          onClick: disabled ? void 0 : onClick,
          style: {
            cursor: disabled ? "not-allowed" : "pointer",
            opacity: disabled ? 0.5 : 1,
            ...trigger.props.style || {}
          }
        });
      }
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: `pisell-lookup-trigger-custom ${className || ""}`,
          onClick: disabled ? void 0 : onClick,
          style: {
            cursor: disabled ? "not-allowed" : "pointer",
            opacity: disabled ? 0.5 : 1,
            ...style
          }
        },
        trigger
      );
    };
    const renderTrigger = () => {
      if (trigger) {
        return renderCustomTrigger();
      }
      switch (type) {
        case "input":
          return renderInputTrigger();
        case "button":
          return renderButtonTrigger();
        case "icon":
          return renderIconTrigger();
        case "text":
          return renderTextTrigger();
        default:
          return renderInputTrigger();
      }
    };
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, renderTrigger(), showScan && /* @__PURE__ */ import_react.default.createElement(
      import_pisellQRScanner.default,
      {
        visible: scanVisible,
        onClose: handleScanClose,
        onScan: handleScanSuccess
      }
    ));
  }
);
LookupTriggerComponent.displayName = "LookupTrigger";
var LookupTrigger = LookupTriggerComponent;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LookupTrigger
});
