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

export class HandlerParser implements LowCodeModuleParser {
  handler: any;

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

  parseModule(pageId: string, mod: IEvaledModule) {
    if (mod.name === '____index____') return;

    const [, , folder] = mod.path?.split('/');
    if (folder) {
      this.handler[pageId] = this.handler[pageId] || {};
      this.handler[pageId][folder] = mod.exports.default;
    } else this.handler[pageId] = mod.exports.default;
  }

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