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/pisellFind/index.tsx
var pisellFind_exports = {};
__export(pisellFind_exports, {
  PisellFind: () => PisellFind,
  SCAN_RESULT_EVENT: () => SCAN_RESULT_EVENT
});
module.exports = __toCommonJS(pisellFind_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_pisellLookup = require("../pisellLookup");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_locales = require("../../locales");
var SCAN_RESULT_EVENT = "nativeScanResult";
var PisellFind = (0, import_react.forwardRef)((props, ref) => {
  var _a, _b;
  const {
    triggerType = "button",
    buttonText = (() => {
      const key = "pisell2.recordBoard.toolbar.find";
      const u = import_utils.locales.getText(key);
      return u && u !== key ? u : (0, import_locales.getText)(key) || (0, import_locales.getText)("pisell-find-button-text");
    })(),
    placeholder = (() => {
      const key = "pisell2.recordBoard.toolbar.findPlaceholder";
      const u = import_utils.locales.getText(key);
      return u && u !== key ? u : (0, import_locales.getText)(key) || (0, import_locales.getText)("pisell-find-placeholder");
    })(),
    showConfirmButton = false,
    showHistory = true,
    historyKey = "pisell_find_history",
    enableScanner = false,
    onScannerData,
    onSearch,
    ...restProps
  } = props;
  const lookupRef = (0, import_react.useRef)(null);
  const onScannerDataRef = (0, import_react.useRef)(onScannerData);
  const onSearchRef = (0, import_react.useRef)(onSearch);
  (0, import_react.useEffect)(() => {
    onScannerDataRef.current = onScannerData;
  }, [onScannerData]);
  (0, import_react.useEffect)(() => {
    onSearchRef.current = onSearch;
  }, [onSearch]);
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      open: () => {
        var _a2;
        return (_a2 = lookupRef.current) == null ? void 0 : _a2.open();
      },
      close: () => {
        var _a2;
        return (_a2 = lookupRef.current) == null ? void 0 : _a2.close();
      },
      getKeyword: () => {
        var _a2;
        return ((_a2 = lookupRef.current) == null ? void 0 : _a2.getKeyword()) ?? "";
      },
      setKeyword: (v) => {
        var _a2;
        return (_a2 = lookupRef.current) == null ? void 0 : _a2.setKeyword(v);
      }
    }),
    []
  );
  const engineContext = (0, import_useEngineContext.default)();
  const getAppRef = (0, import_react.useRef)(void 0);
  getAppRef.current = typeof ((_b = (_a = engineContext == null ? void 0 : engineContext.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.getApp) === "function" ? engineContext.appHelper.utils.getApp : void 0;
  (0, import_react.useEffect)(() => {
    if (!enableScanner) return void 0;
    const getApp = getAppRef.current;
    if (typeof getApp !== "function") return void 0;
    const app = getApp();
    const pubsub = app == null ? void 0 : app.pubsub;
    if (!pubsub || typeof pubsub.subscribe !== "function") {
      return void 0;
    }
    const onScan = (payload) => {
      var _a2, _b2, _c;
      const data = payload == null ? void 0 : payload.data;
      if (typeof data !== "string" || data.length === 0) {
        return;
      }
      (_a2 = lookupRef.current) == null ? void 0 : _a2.setKeyword(data);
      (_b2 = onSearchRef.current) == null ? void 0 : _b2.call(onSearchRef, data);
      (_c = onScannerDataRef.current) == null ? void 0 : _c.call(onScannerDataRef, payload);
    };
    try {
      pubsub.subscribe(SCAN_RESULT_EVENT, onScan);
    } catch (e) {
      console.warn("[PisellFind] subscribe scanner failed:", e);
      return void 0;
    }
    return () => {
      var _a2;
      try {
        (_a2 = pubsub.unsubscribe) == null ? void 0 : _a2.call(pubsub, SCAN_RESULT_EVENT, onScan);
      } catch (e) {
        console.warn("[PisellFind] unsubscribe scanner failed:", e);
      }
    };
  }, [enableScanner]);
  return /* @__PURE__ */ import_react.default.createElement(
    import_pisellLookup.PisellLookup,
    {
      ref: lookupRef,
      triggerType,
      buttonText,
      placeholder,
      showConfirmButton,
      showHistory,
      historyKey,
      onSearch,
      ...restProps
    }
  );
});
PisellFind.displayName = "PisellFind";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PisellFind,
  SCAN_RESULT_EVENT
});
