import { LowCodeModuleParser, IEvaledModule, LowcodeContext } from './base';

export class LifeCycleParser implements LowCodeModuleParser {
  lifeCycle: any;

  onStart(context: LowcodeContext) {
    this.lifeCycle = {};
  }

  parseModule(pageId: string, mod: IEvaledModule) {
    const lifeCycleObj = mod.exports?.default || mod.exports;

    if (typeof lifeCycleObj === 'object') {
      this.lifeCycle[pageId] = lifeCycleObj;
    } else {
      throw new Error('lifecycle must export an object');
    }
  }

  onEnd(context: LowcodeContext) {
    context.lifeCycle = this.lifeCycle;
  }
}
