import { EventLife } from "../enums/eventEnum.mjs";
import * as index from "../../node_modules/.pnpm/echarts@5.4.3/node_modules/echarts/index.mjs";
const components = {};
const npmPkgs = { echarts: index };
const useLifeHandler = (chartConfig) => {
  if (!chartConfig.events)
    return {};
  const baseEvent = {};
  for (const key in chartConfig.events.baseEvent) {
    const fnStr = chartConfig.events.baseEvent[key];
    if (fnStr) {
      baseEvent[key] = generateBaseFunc(fnStr);
    }
  }
  const events = chartConfig.events.advancedEvents || {};
  const lifeEvents = {
    [EventLife.VNODE_BEFORE_MOUNT](e) {
      components[chartConfig.id] = e.component;
      const fnStr = (events[EventLife.VNODE_BEFORE_MOUNT] || "").trim();
      generateFunc(fnStr, e);
    },
    [EventLife.VNODE_MOUNTED](e) {
      const fnStr = (events[EventLife.VNODE_MOUNTED] || "").trim();
      generateFunc(fnStr, e);
    }
  };
  return { ...baseEvent, ...lifeEvents };
};
function generateBaseFunc(fnStr) {
  try {
    return new Function(`
      return (
        async function(components,mouseEvent){
          ${fnStr}
        }
      )`)().bind(void 0, components);
  } catch (error) {
    console.error(error);
  }
}
function generateFunc(fnStr, e) {
  try {
    Function(`
      "use strict";
      return (
        async function(e, components, node_modules){
          const {${Object.keys(npmPkgs).join()}} = node_modules;
          ${fnStr}
        }
      )`)().bind(e == null ? void 0 : e.component)(e, components, npmPkgs);
  } catch (error) {
    console.error(error);
  }
}
export {
  generateBaseFunc,
  npmPkgs,
  useLifeHandler
};
