import { Injectable, Injector } from '@angular/core';
import { ActionHandler } from './action-handler.interface';
import { FEATURE_ACTION_HANDLERS, ROOT_ACTION_HANDLERS } from './tokens';

@Injectable()
export class CommandService {
  constructor(private injector: Injector) {}

  async handleCommand<T>(command: string, data?: T): Promise<T> {
    const actions = this.getActions(command);

    for (const action of actions) {
      const handler = this.getActionHandler(action);
      if (!handler) {
        console.error('CommandService.handleCommand', 'Action Handler does not exist', { action });
        throw new Error('Action Handler does not exist');
      }
      console.log('handle command', handler, data);
      data = await handler.handler(data);
    }
    return data;
  }

  getActions(command: string) {
    return command?.split('|') || [];
  }

  getActionHandler(action: string): ActionHandler {
    const featureActionHandlers: ActionHandler[] = this.injector.get(FEATURE_ACTION_HANDLERS, []);
    const rootActionHandlers: ActionHandler[] = this.injector.get(ROOT_ACTION_HANDLERS, []);

    return [...featureActionHandlers, ...rootActionHandlers].find((handler) => handler.action === action);
  }
}
