import { DataDeleterArgs } from '~/types/interfaces'
import BaseDataManager from '~/services/data/baseDataManager'
import { repositoryHelper } from '~/services/store/repository'
import { hooksDeleter } from '~/services/data/hooks/hookDeleter/_import'
import UrlBuilder from '~/services/connection/urlBuilder'
import { HTTP_METHOD } from '~/types/enums'
import Connection from '~/services/connection/connection'

/**
 * Le DataDeleter a pour rôle de supprimer des enregistrements via l'API
 */
class DataDeleter extends BaseDataManager {
  protected hooks = hooksDeleter;

  /**
   * Exécute la requête
   * @param {DataDeleterArgs} queryArguments
   */
  protected async _invoke (queryArguments: DataDeleterArgs): Promise<any> {
    // build the url according to the url args
    const url = UrlBuilder.build(queryArguments)

    // send the DELETE request to the api
    await Connection.invoke(HTTP_METHOD.DELETE, url, queryArguments)

    // update the store
    if (queryArguments.model) {
      await repositoryHelper.deleteItem(queryArguments.model, queryArguments.id)
    }
  }
}

export default DataDeleter
