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 import_en_US = __toESM(require("../../locales/en-US"));
var import_zh_CN = __toESM(require("../../locales/zh-CN"));
var import_zh_TW = __toESM(require("../../locales/zh-TW"));
var import_ja = __toESM(require("../../locales/ja"));
var import_pt = __toESM(require("../../locales/pt"));
var SCAN_RESULT_EVENT = "nativeScanResult";
var pisellFindLocaleTexts = {
  en: import_en_US.default,
  "zh-CN": import_zh_CN.default,
  "zh-HK": import_zh_TW.default,
  ja: import_ja.default,
  pt: import_pt.default
};
function getPisellFindText(engineContext, key, fallbackKey) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
  const rawLocale = ((_f = (_e = (_d = (_c = (_b = (_a = engineContext == null ? void 0 : engineContext.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.getApp) == null ? void 0 : _c.call(_b)) == null ? void 0 : _d.locales) == null ? void 0 : _e.getLocale) == null ? void 0 : _f.call(_e)) || ((_j = (_i = (_h = (_g = engineContext == null ? void 0 : engineContext.appHelper) == null ? void 0 : _g.utils) == null ? void 0 : _h.storage) == null ? void 0 : _i.get) == null ? void 0 : _j.call(_i, "umi_locale"));
  const locale = import_utils.locales.formatLocale(rawLocale || ((_l = (_k = import_utils.locales).getLocale) == null ? void 0 : _l.call(_k)));
  const localeTexts = pisellFindLocaleTexts[locale];
  return (localeTexts == null ? void 0 : localeTexts[key]) || (fallbackKey ? localeTexts == null ? void 0 : localeTexts[fallbackKey] : void 0) || (0, import_locales.getText)(key) || (fallbackKey ? (0, import_locales.getText)(fallbackKey) : void 0) || key;
}
var PisellFind = (0, import_react.forwardRef)((props, ref) => {
  var _a, _b, _c, _d;
  const {
    triggerType = "button",
    buttonText,
    placeholder,
    closeButtonText,
    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 resolvedButtonText = buttonText ?? getPisellFindText(
    engineContext,
    "pisell2.recordBoard.toolbar.find",
    "pisell-find-button-text"
  );
  const resolvedPlaceholder = placeholder ?? getPisellFindText(
    engineContext,
    "pisell2.recordBoard.toolbar.findPlaceholder",
    "pisell-find-placeholder"
  );
  const resolvedCloseButtonText = closeButtonText ?? getPisellFindText(engineContext, "pisell-lookup-close");
  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;
  const acquireScanFocusRef = (0, import_react.useRef)(void 0);
  const appHelperUtils = (_c = engineContext == null ? void 0 : engineContext.appHelper) == null ? void 0 : _c.utils;
  acquireScanFocusRef.current = typeof (appHelperUtils == null ? void 0 : appHelperUtils.acquireScanFocus) === "function" ? appHelperUtils.acquireScanFocus : typeof ((_d = appHelperUtils == null ? void 0 : appHelperUtils.businessUtils) == null ? void 0 : _d.acquireScanFocus) === "function" ? appHelperUtils.businessUtils.acquireScanFocus : void 0;
  (0, import_react.useEffect)(() => {
    if (!enableScanner) return void 0;
    const onScan = (payload) => {
      var _a2, _b2, _c2;
      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, "scan");
      (_b2 = onSearchRef.current) == null ? void 0 : _b2.call(onSearchRef, data);
      (_c2 = onScannerDataRef.current) == null ? void 0 : _c2.call(onScannerDataRef, payload);
    };
    const acquireScanFocus = acquireScanFocusRef.current;
    if (typeof acquireScanFocus === "function") {
      try {
        const token = acquireScanFocus(onScan);
        return () => {
          var _a2;
          try {
            (_a2 = token == null ? void 0 : token.release) == null ? void 0 : _a2.call(token);
          } catch (e) {
            console.warn("[PisellFind] release scan focus failed:", e);
          }
        };
      } catch (e) {
        console.warn("[PisellFind] acquire scan focus failed, fallback to pubsub:", e);
      }
    }
    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;
    }
    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: resolvedButtonText,
      placeholder: resolvedPlaceholder,
      closeButtonText: resolvedCloseButtonText,
      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
});
