import { APIBase } from '@dvn/core/api/apiBase';
import { Collection } from './models';
function generateQueryParams(query = {}, skip = 0, sort = '', limit = 25) {
  const params = Object.assign(Object.assign({}, query), { skip,
    limit });
  if (sort && sort != '') {
    params.sort = sort;
  }
  return params;
}
export class MentornityApi {
  constructor(endpoint, defaultHeaders = {}) {
    // region common
    this.create = (collection, body) => this.api.post(collection, body);
    this.find = (collection, params) => this.api.get(`${collection}`, params);
    this.get = (collection, id, params) => this.api.get(`${collection}/${id}`, params);
    this.update = (collection, id, body) => this.api.put(`${collection}/${id}`, body);
    this.delete = (collection, id) => this.api.delete(`${collection}/${id}`);
    // endregion
    // region session
    /**
     * Used to gets session data. Returns only white label info if there is no account.
     *
     * @param domain of the current web site. (used to determine the white label)
     */
    this.getSessionData = (domain) => this.api.get(`sessionData?domain=${domain}`);
    // endregion
    // region organisation
    this.getOrganisation = (organisationId) => this.api.get(`organisations/${organisationId}`);
    this.getOrganisationConfig = (organisationId) => this.api.get(`organisations/${organisationId}/config`);
    this.getOrganisationMeetingGoalConfig = (organisationId) => this.api.get(`organisations/${organisationId}/meetingGoalConfig`);
    this.getViewConfigs = (organisationId) => this.api.get(`organisations/${organisationId}/view-configs`);
    this.getViewConfig = (organisationId, viewName) => this.api.get(`organisations/${organisationId}/view-configs/${viewName}`);
    this.createViewConfig = (viewConfigWrapper) => this.api.post('view-configs', viewConfigWrapper);
    this.updateViewConfig = (id, viewConfigWrapper) => this.api.put(`view-configs/${id}`, viewConfigWrapper);
    // endregion
    this.createInvitation = (data) => this.api.post('invitations', data);
    // region members
    this.getMentees = (organisationId, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${organisationId}/mentees`, generateQueryParams(query, skip, sort, limit));
    this.getMentors = (organisationId, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${organisationId}/mentors`, generateQueryParams(query, skip, sort, limit));
    this.getMentorshipOfUser = (organisationId, userId) => this.api.get(`organisations/${organisationId}/mentors/${userId}/mentorship`);
    this.getMembershipApplicationForm = (organisationId, role) => this.api.get(`organisations/${organisationId}/${role}/applications/form`);
    this.createMembershipApplication = (organisationId, role, data) => this.api.post(`organisations/${organisationId}/${role}/applications`, data);
    // endregion
    // region metadatas
    this.getMetadataOfUser = (organisationId, role, userId) => this.api.get(`organisations/${organisationId}/${role}/${userId}/metadata`);
    // endregion
    // region subscriptions
    this.getOrganisations = (query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations`, generateQueryParams(query, skip, sort, limit));
    this.createSubscription = (subscription) => this.api.post(`subscriptions`, subscription);
    this.followUnfollowSubscription = (subscriptionId, action) => this.api.post(`subscriptions/${subscriptionId}/${action}`, {});
    // endregion
    // region subscriptions
    this.getSubscriptionsOfMentors = (id, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${id}/mentors`, generateQueryParams(Object.assign(Object.assign({}, query), { appendSubscriptions: true }), skip, sort, limit));
    this.completeSubscription = (subscriptionId) => this.api.put(`${Collection.Subscriptions}/${subscriptionId}`, { status: 'completed' });
    this.deleteSubscription = (subscriptionId) => this.api.delete(`${Collection.Subscriptions}/${subscriptionId}`);
    // endregion
    this.getMeetingsInMeetingGoal = (id, query = {}, skip = 0, sort = '', limit) => this.api.get(`organisations/${id}/meetingsInMeetingGoal`, generateQueryParams(query, skip, sort, limit));
    this.createForm = (data) => this.api.post('forms', data);
    this.api = new APIBase(endpoint, defaultHeaders);
  }
  cloneOrganisation(body) {
    return this.api.post('organisations/clone', body);
  }
  importUser(id, userType, body) {
    return this.api.post(`organisations/${id}/${userType}/import`, body);
  }
  removeOrganisationAdmin(userId, body) {
    return this.api.post(`users/${userId}/recallRole`, body);
  }
  changeOrganisationDomain({ domain, organisationId }) {
    return this.api.put(`organisations/${organisationId}/domain`, { domain });
  }
}
