import { Injectable } from '@angular/core';
import { BotService } from '../Services/bot.service';
import { Keyword, Share } from '../Views/bot-settings/bot-settings.component';

@Injectable({
  providedIn: 'root',
})
export class HelperService {
  constructor(private botService: BotService) {}

  /**
   * Function to delay loading of webpage for a smoother user experience
   * @param  {number} ms
   */
  delay(ms: number) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }

  /**
   * Function to format share data
   * @returns Share
   */
  formatShareData(): Share[] {
    var shares: Share[] = [];
    this.botService.getSymbols().subscribe((result) => {
      var data = JSON.parse(result);
      for (let i = 0; i < data.data.length; i++) {
        shares.push({
          isin: data.data[i].isin,
        });
      }
    });
    return shares;
  }
  /**
   * @returns Keyword
   * Function to format keyword data
   */
  formatKeywordsData(): Keyword[] {
    var keywords: Keyword[] = [];
    this.botService.getKeywords().subscribe((result) => {
      var data = JSON.parse(result);
      for (let i = 0; i < data.data.length; i++) {
        keywords.push({
          name: data.data[i].keyword,
        });
      }
    });
    return keywords;
  }
}
