"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const eventFlowParser = require("../eventFlowParser.js");
async function executeEventFlow(params) {
  const {
    eventFlow,
    fields = [],
    instance = null,
    localConfig = {},
    components = {},
    onSuccess,
    onError,
    row,
    item,
    metadata
  } = params;
  if (!eventFlow) {
    return;
  }
  try {
    const parser = eventFlowParser.createEventFlowParser({
      eventFlow,
      fields,
      data: row,
      instance,
      localConfig,
      components,
      metadata
    });
    const result = await parser.start();
    console.log("事件流执行完成:", result);
    parser.destroy();
    if (onSuccess) {
      onSuccess(result, row, item);
    }
    return result;
  } catch (error) {
    console.error("事件流执行失败:", error);
    if (onError) {
      onError(error, row, item);
    }
    throw error;
  }
}
function createEventFlowHandler(options = {}) {
  const {
    fields = [],
    instance = null,
    onSuccess,
    onError,
    localConfig = {},
    components = {},
    metadata = {}
  } = options;
  return async function handleEvent(row, item, eventConfig) {
    if (eventConfig && eventConfig.eventFlow) {
      return await executeEventFlow({
        eventFlow: eventConfig.eventFlow,
        fields,
        instance,
        localConfig,
        components,
        onSuccess,
        onError,
        row,
        item,
        metadata: { ...metadata, item }
      });
    }
  };
}
function getEventHandlers(item, row, eventHandler, eventConfigs) {
  const handlers = {};
  if (!eventConfigs) {
    return handlers;
  }
  eventConfigs.forEach((eventConfig) => {
    if (!eventConfig.name) return;
    const eventName = eventConfig.name.toLowerCase();
    handlers[eventName] = () => eventHandler(row, item, eventConfig);
  });
  return handlers;
}
function useEventFlow(options = {}) {
  const handleEvent = createEventFlowHandler(options);
  return {
    handleEvent,
    getEventHandlers: (item, row, eventConfig) => getEventHandlers(item, row, handleEvent, eventConfig)
  };
}
exports.createEventFlowHandler = createEventFlowHandler;
exports.executeEventFlow = executeEventFlow;
exports.getEventHandlers = getEventHandlers;
exports.useEventFlow = useEventFlow;
