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/ticketBooking/hooks/useSync.ts
var useSync_exports = {};
__export(useSync_exports, {
  useSync: () => useSync
});
module.exports = __toCommonJS(useSync_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_sync = require("../sync");
var import_useRuntime = require("./useRuntime");
var import_IsActiveContext = require("../../ticketBooking/context/IsActiveContext");
function useSync(store) {
  var _a, _b, _c, _d;
  const context = (0, import_useEngineContext.default)();
  const app = (_c = (_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.getApp) == null ? void 0 : _c.call(_b);
  const isActive = (0, import_IsActiveContext.useIsActive)();
  const currentModule = (0, import_react.useRef)(Math.random());
  const { runtime, isSecondaryScreen } = ((_d = import_useRuntime.useRuntime) == null ? void 0 : _d()) || { runtime: null, isSecondaryScreen: false };
  console.log("runtimeruntime", runtime);
  console.log(currentModule.current, isActive, "currentModule isActive");
  const { run: debouncedSend } = (0, import_ahooks.useDebounceFn)(
    async (key) => {
      var _a2, _b2, _c2;
      try {
        console.log(key, isActive, "key1234");
        if (key === "date") return;
        if (isSecondaryScreen || !isActive) return;
        console.log("[useSync] 主屏 防抖发送数据", store.getState());
        const formattedData = (0, import_sync.formatSyncData)(store.getState());
        await ((_c2 = (_b2 = (_a2 = app == null ? void 0 : app.getPlugin) == null ? void 0 : _a2.call(app, "unifiedCommunication")) == null ? void 0 : _b2.sdk) == null ? void 0 : _c2.publishData({
          target: { type: "secondary-screen" },
          data: {
            type: "sync_card",
            data: formattedData
          },
          meta: {}
        }));
        console.log("[useSync] 主屏 防抖发送数据成功", formattedData);
        app == null ? void 0 : app.logger.addLog({
          type: "info",
          title: "发送信息到副屏成功"
        });
      } catch (error) {
        console.log("[useSync] 主屏 防抖发送数据失败", error);
        app == null ? void 0 : app.logger.addLog({
          type: "error",
          title: "发送信息到副屏失败"
        });
      }
    },
    { wait: 1e3, leading: true, trailing: true }
  );
  const { run: debouncedMessage } = (0, import_ahooks.useDebounceFn)(
    async (event) => {
      var _a2, _b2, _c2, _d2;
      console.log("[useSync] 副屏 接收到的数据", event);
      const envelope = event.data;
      const payload = (_b2 = (_a2 = envelope.payload) == null ? void 0 : _a2.data) == null ? void 0 : _b2.data;
      const type = (_d2 = (_c2 = envelope.payload) == null ? void 0 : _c2.data) == null ? void 0 : _d2.type;
      if (type !== "sync_card") {
        return;
      }
      const mergeData = (0, import_sync.mergeSyncData)(store.getState(), payload);
      console.log("[useSync] 副屏 格式化后的数据", mergeData);
      store.setState(mergeData);
    },
    { wait: 500 }
  );
  (0, import_react.useEffect)(() => {
    if (isSecondaryScreen || !isActive) return;
    console.log("[useSync] 主屏 开始监听 store 变更");
    const fn = store.subscribe(debouncedSend);
    return () => {
      fn();
    };
  }, [store, runtime, debouncedSend, isActive]);
  (0, import_react.useEffect)(() => {
    var _a2, _b2, _c2;
    if (!isSecondaryScreen) return;
    const handleMessage = (event) => {
      debouncedMessage(event);
    };
    const listenerHandle = (_c2 = (_b2 = (_a2 = app == null ? void 0 : app.getPlugin) == null ? void 0 : _a2.call(app, "unifiedCommunication")) == null ? void 0 : _b2.sdk) == null ? void 0 : _c2.onMessage(handleMessage);
    return () => {
      var _a3;
      if (listenerHandle && typeof listenerHandle.then === "function") {
        listenerHandle.then((handle) => {
          var _a4;
          return (_a4 = handle == null ? void 0 : handle.remove) == null ? void 0 : _a4.call(handle);
        }).catch(() => {
        });
      } else {
        (_a3 = listenerHandle == null ? void 0 : listenerHandle.remove) == null ? void 0 : _a3.call(listenerHandle);
      }
    };
  }, [store, isSecondaryScreen, isActive, app]);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useSync
});
