import { onUnmounted, onBeforeUnmount, onMounted } from "vue";
/* empty css                                                          */
/* empty css                                                              */
/* empty css                                                                */
/* empty css                                                                */
/* empty css                                                                */
/* empty css                                                               */
/* empty css                                                               */
import "../utils/formulajs/functionCore.mjs";
import { useEventFlow } from "../utils/eventFlow/eventFlowHandler.mjs";
/* empty css                                       */
/* empty css                                               */
/* empty css                                                     */
/* empty css                                                */
/* empty css                                               */
/* empty css                                                  */
/* empty css                                                 */
/* empty css                                                            */
/* empty css                                                  */
/* empty css                                                */
/* empty css                                                 */
/* empty css                                                              */
/* empty css                                                                    */
function usePageEvents(getOptions) {
  if (typeof getOptions !== "function") {
    const staticOptions = getOptions;
    getOptions = () => staticOptions;
  }
  const executeEvent = async (eventName) => {
    const options = getOptions();
    const { pageEventConfig, instance, formData, localConfig, components, fields, initSearchData } = options;
    if (!pageEventConfig || !Array.isArray(pageEventConfig) || pageEventConfig.length === 0) {
      console.log(`页面事件 ${eventName} 未配置或配置未加载完成，跳过执行`);
      return;
    }
    const eventConfig = pageEventConfig.find((e) => e.name === eventName);
    if (!eventConfig || !eventConfig.eventFlow) {
      console.log(`页面事件 ${eventName} 未找到事件流配置，跳过执行`);
      return;
    }
    const { getEventHandlers: createEventHandlers } = useEventFlow({
      fields: fields || [],
      instance,
      localConfig: localConfig || {},
      components: components || {},
      metadata: {
        initSearchData
      }
    });
    const handler = createEventHandlers(
      {},
      formData || {},
      [eventConfig]
    );
    const eventKeys = Object.keys(handler);
    if (eventKeys.length > 0) {
      console.log(`执行页面事件 ${eventName}`);
      await handler[eventKeys[0]]();
    }
  };
  const handleCreated = (checkConfigLoaded) => {
    console.log("🚀 页面 onCreated 事件处理器已注册（将在配置加载完成后触发）");
  };
  const handleMounted = (checkConfigLoaded) => {
    onMounted(() => {
      console.log("🚀 页面 onMounted 事件触发");
      executeEvent("onMounted");
    });
  };
  const handleBeforeUnmount = () => {
    onBeforeUnmount(() => {
      console.log("🧹 页面 onBeforeUnmount 事件触发");
      executeEvent("onBeforeUnmount");
    });
  };
  const handleUnmounted = () => {
    onUnmounted(() => {
      console.log("✅ 页面 onUnmounted 事件触发");
      executeEvent("onUnmounted");
    });
  };
  return {
    handleCreated,
    handleMounted,
    handleBeforeUnmount,
    handleUnmounted,
    executeEvent
    // 导出 executeEvent 方法，方便在配置加载完成后立即触发 onCreated
  };
}
export {
  usePageEvents
};
