text: "use strict";
  
  const Base = require("./Base");
  const Endpoints = require("../rest/Endpoints");
  const Collection = require("../util/Collection");
  const SlashCommand = require("./SlashCommand");
  
  class SlashCommandHandler extends Base {
  
      constructor(client, commands) {
          super(client);
          this.client = client;
          this.slashCommands = new Map(); //Switch to Collection once SlashCommand struct is made.
          this.commands = commands;
          this.client.on('ready', this.startHandler.bind(this));
      }
  
      async startHandler() {
          let getCurrentSlashCommands = await this.client.requestHandler.request("GET", Endpoints.GLOBAL_COMMANDS(this.client.user.id), true);
          //if(slashCommands.length > 0) this.startUpDelete();
  
          if(!this.commands) {
              getCurrentSlashCommands.forEach(command => {
                  setTimeout(() => {
                      this.deleteSlashCommand(command.id);
                  }, 400)
              });
          }
          this.commands.forEach(command => {
              let index = getCurrentSlashCommands.map(x => x.name).indexOf(command.name);
              let findCommand = getCurrentSlashCommands[index];
  
              if(!findCommand) {
                  return this.createSlashCommand(command);
              } else if(findCommand && findCommand.name !== command.name || findCommand.description !== command.description) {
                  return this.editSlashCommand(findCommand, command);
              } else if(findCommand && findCommand.name === command.name && findCommand.description == command.description) {
                  let slash = new SlashCommand(command);
                  this.slashCommands.set(slash.name, slash);
              }
          });
          await this.startUpDelete();
      }
  
  
      async createSlashCommand(options){
          if(this.slashCommands.get(options.name)) return this.editSlashCommand(this.slashCommands.get(options.name), options);
          let slashCommand = new SlashCommand(options);
          this.slashCommands.set(slashCommand.name, slashCommand);
          let cmd = await this.client.requestHandler.request("POST", Endpoints.GLOBAL_COMMANDS(this.client.user.id), true, slashCommand);
          this.slashCommands.set(slashCommand.name, { id: cmd.id });
          return;
      }
  
      editSlashCommand(oldCommand, newCommand) {
  
          let slashCommand = new SlashCommand(newCommand);
  
          if(this.slashCommands.get(oldCommand.name)) {
              if(oldCommand.name !== slashCommand.name) {
                  this.slashCommands.get(oldCommand.name).set(slashCommand);
                  this.slashCommands.set(slashCommand.name, oldCommand);
                  this.slashCommands.delete(oldCommand.name);
                  return this.client.requestHandler.request("PATCH", Endpoints.GLOBAL_COMMANDS(this.client.user.id, this.slashCommands.get(slashCommand.name).id), true, slashCommand);
              } else {
                  this.slashCommands.set(oldCommand.name, slashCommand);
                  return this.client.requestHandler.request("PATCH", Endpoints.GLOBAL_COMMANDS(this.client.user.id, this.slashCommands.get(slashCommand.name).id), true, this.slashCommands.get(oldCommand.name));
              }
          }
      }
  
      async deleteSlashCommand(command) {
          //let slashCommand = this.slashCommands.get(command);
          return await this.client.requestHandler.request("DELETE", Endpoints.GLOBAL_COMMANDS(this.client.user.id, command.id), true);
      }
  
      async startUpDelete() {
          let globalCommands = await this.client.requestHandler.request("GET", Endpoints.GLOBAL_COMMANDS(this.client.user.id), true);
  
          globalCommands.forEach(command => {
              if(this.slashCommands.get(command.name)) return;
              setTimeout(() => {
                  this.client.requestHandler.request("DELETE", Endpoints.GLOBAL_COMMANDS(this.client.user.id, command.id), true);
              }, 300);
          });
  
      }
  
  }
  
  module.exports = SlashCommandHandler;
  